Click here to Skip to main content
15,887,923 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I need to resize bitmap from local_member_buffer_1 to prgb and make 2 images in prgb
This both are RGB 24 bit per pixel buffers.

Here is the new variant of code:
{
				HBITMAP handle_bit_map_source = NULL;
				handle_bit_map_source = CreateBitmap(
					cxImage, 
					cyImage, 
					1,
					24, 
					local_member_buffer_1);
				if(handle_bit_map_source==NULL)
				{
					goto stereo_horizontal_finish;
				}
				
				HBITMAP handle_bit_map_resulting = (HBITMAP)CopyImage
					(
					handle_bit_map_source,
					IMAGE_BITMAP,
					cxImage/2,
					cyImage/2,
					LR_COPYDELETEORG
					);
				if(handle_bit_map_resulting==NULL)
				{
					DeleteObject(handle_bit_map_source);
					goto stereo_horizontal_finish;
				}
				ZeroMemory(local_member_buffer_0,cxImage/2*cyImage/2*sizeof(RGBTRIPLE));
				LONG local_bytes_copied = GetBitmapBits(handle_bit_map_resulting, cxImage/2*cyImage/2*sizeof(RGBTRIPLE), local_member_buffer_0);
				if (local_bytes_copied!=cxImage/2*cyImage/2*sizeof(RGBTRIPLE))
				{
					DeleteObject(handle_bit_map_resulting);
					goto stereo_horizontal_finish;
				}
				ZeroMemory(prgb,cxImage*cyImage*sizeof(RGBTRIPLE));
				//	Ëåâîå èçîáðàæåíèå
				for(local_counter_width=0;local_counter_width<cxImage/2;local_counter_width++)
				{
					for(int local_counter=0;local_counter<cyImage/2;local_counter++)
					{						
						prgb[local_counter_width+(local_counter+cyImage/4)*cxImage].rgbtRed
							= 
							local_member_buffer_0[local_counter_width+local_counter*cxImage/2].rgbtRed
							;
						prgb[local_counter_width+(local_counter+cyImage/4)*cxImage].rgbtGreen
							= 
							local_member_buffer_0[local_counter_width+local_counter*cxImage/2].rgbtGreen
							;
						prgb[local_counter_width+(local_counter+cyImage/4)*cxImage].rgbtBlue
							= 
							local_member_buffer_0[local_counter_width+local_counter*cxImage/2].rgbtBlue
							;
					}
				}
				//	Ïðàâîå èçîáðàæåíèå
				for(local_counter_width=0;local_counter_width<cxImage/2;local_counter_width++)
				{
					for(int local_counter=0;local_counter<cyImage/2;local_counter++)
					{						
						prgb[local_counter_width+cxImage/2+(local_counter+cyImage/4)*cxImage].rgbtRed
							= 
							local_member_buffer_0[local_counter_width+local_counter*cxImage/2].rgbtRed
							;
						prgb[local_counter_width+cxImage/2+(local_counter+cyImage/4)*cxImage].rgbtGreen
							= 
							local_member_buffer_0[local_counter_width+local_counter*cxImage/2].rgbtGreen
							;
						prgb[local_counter_width+cxImage/2+(local_counter+cyImage/4)*cxImage].rgbtBlue
							= 
							local_member_buffer_0[local_counter_width+local_counter*cxImage/2].rgbtBlue
							;
					}
				}
				DeleteObject(handle_bit_map_resulting);
//				memcpy(prgb,local_member_buffer_2,cxImage*cyImage*sizeof(RGBTRIPLE));
			}
stereo_horizontal_finish:


I get image with zeros after GetBitmapBits.

What is wrong here?
Posted
Updated 20-Apr-11 3:08am
v7
Comments
[no name] 20-Apr-11 10:02am    
I know that CopyImage does not work.
Guyverthree 20-Apr-11 10:43am    
GetLastError(); can be your best friend in this case :)
[no name] 20-Apr-11 11:46am    
There i no error. But buffer contains only zero values.
Guyverthree 20-Apr-11 11:49am    
try dumping the memory at local_member_buffer_1 and see if the image that you are copying is empty.
[no name] 20-Apr-11 11:53am    
There is real image coming from video film (it is film frame).

Why dont you clip the bitmap bits directly?
C#
int             cxDest = cxImage/2;
int             cyDest = cyImage/2;
int             bpls = ((cxImage*sizeof(RGBTRIPLE))+3)&~3;
int             bpld = ((cxDest *sizeof(RGBTRIPLE))+3)&~3;
unsigned char*  lps = (unsigned char*)local_member_buffer_1 + (bpls*(cyImage-cyDest-1));
unsigned char*  lpd = (unsigned char*)prgb + (bpld*(cyDest-1));
unsigned char*  lpe = lps - (bpls*cyDest);

for(;lps>lpe;lps-=bpls,lpd-=bpld)
{
  memcpy(lpd,lps,sizeof(RGBTRIPLE)*cxDest);
}

Regards.

[edit]
i suppose you want to create a stereo image with same image left and right:
C#
int             cxDest = cxSource/2;
int             cyDest = cySource;
int             bpls = ((cxSource*3)+3)&~3;
int             bpld = ((cxDest  *3)+3)&~3;
unsigned char*  lps = local_member_buffer_1;
unsigned char*  lpd = prgb;
unsigned char*  lpe = local_member_buffer_1+ + (bpls*cySource);
for(;lps<lpe;lps+=bpls,lpd+=bpld)
{
  // copy each line to left and right
  memcpy(lpd,lps,sizeof(RGBTRIPLE)*cxSource);
  memcpy(lpd+(sizeof(RGBTRIPLE)*cxSource),lps,sizeof(RGBTRIPLE)*cxSource);
}


Good luck.

Kozlov Sergey:
Yes. But it resizes only height.
I need also width.

[edit]
with shrink original pixel, this is a little bit larger:
C#
int             cxDest = cxSource;
int             cyDest = cySource/2;
int             bpls = ((cxSource*3)+3)&~3;
int             bpld = ((cxDest  *3)+3)&~3;
unsigned char*  lps = local_member_buffer_1;
unsigned char*  lpd = prgb;
unsigned char*  lpe = local_member_buffer_1+ + (bpls*cySource);
unsigned char*  lppd;
unsigned char*  lppe;
unsigned char*  lpps1;
unsigned char*  lpps2;
typedef struct
{
  static void shrink4x4(RGBTRIPLE* a,RGBTRIPLE* b,RGBTRIPLE* c,RGBTRIPLE* d,RGBTRIPLE* r)
  {
    r->rgbtRed   = ((unsigned int)a->rgbtRed +
                    (unsigned int)b->rgbtRed +
                    (unsigned int)c->rgbtRed +
                    (unsigned int)d->rgbtRed)>>2;
    r->rgbtGreen = ((unsigned int)a->rgbtGreen +
                    (unsigned int)b->rgbtGreen +
                    (unsigned int)c->rgbtGreen +
                    (unsigned int)d->rgbtGreen)>>2;
    r->rgbtBlue  = ((unsigned int)a->rgbtBlue +
                    (unsigned int)b->rgbtBlue +
                    (unsigned int)c->rgbtBlue +
                    (unsigned int)d->rgbtBlue)>>2;
  }
}_;
for(;lps<lpe;lps+=bpls*2,lpd+=bpld)
{
  lppe = lpd + sizeof(RGBTRIPLE)*(cxDest/2);
  for(lppd=lpd,lpps1=lps,lpps2=lps+bpls;lppd<lppe;lppd+=sizeof(RGBTRIPLE))
  {
    _::shrink4x4
    (
      (RGBTRIPLE*)lpps1,
      (RGBTRIPLE*)(lpps1+sizeof(RGBTRIPLE)),
      (RGBTRIPLE*)lpps2,
      (RGBTRIPLE*)(lpps2+sizeof(RGBTRIPLE)),
      (RGBTRIPLE*)lppd
    );
    lpps1 += sizeof(RGBTRIPLE)+sizeof(RGBTRIPLE);
    lpps2 += sizeof(RGBTRIPLE)+sizeof(RGBTRIPLE);
  }
  lppe = lpd + sizeof(RGBTRIPLE)*(cxDest);
  for(lpps1=lps,lpps2=lps+bpls;lppd<lppe;lppd+=sizeof(RGBTRIPLE))
  {
    _::shrink4x4
    (
      (RGBTRIPLE*)lpps1,
      (RGBTRIPLE*)(lpps1+sizeof(RGBTRIPLE)),
      (RGBTRIPLE*)lpps2,
      (RGBTRIPLE*)(lpps2+sizeof(RGBTRIPLE)),
      (RGBTRIPLE*)lppd
    );
    lpps1 += sizeof(RGBTRIPLE)+sizeof(RGBTRIPLE);
    lpps2 += sizeof(RGBTRIPLE)+sizeof(RGBTRIPLE);
  }
}
 
Share this answer
 
v4
Comments
[no name] 21-Apr-11 2:55am    
Thanks. But it makes 2 copies of left bottom corner (half in width) and puts it on bottom. Can you improve this or tell me how can i improve it. I need to resize image and get 2 resized images in center.
[no name] 21-Apr-11 2:58am    
I am not sure, but it seems to me, that this code resizes height, but there is something wrong with width.
mbue 21-Apr-11 3:07am    
I dont understand: do you want to clip from the center of the original bitmap? which height and width you want to use? do you want to stretch (resize) the bitmap? i need some explanations for that.
if you want to define a clip rectange, the code needs some changes.
at this time the code should clip from top-left the half size of original.
Regards.
[no name] 21-Apr-11 3:16am    
Look at the picture in the answer section.
[no name] 21-Apr-11 3:41am    
Yes. I make "stereo" (not real, but two pictures). After that i can move this two pictures. I have updated answer (cxDest = cxSource/2;). But also i need to resize width.
Hello,

I think you need to use CreateCompatibleBitmap instead of CreateBitmap, in order to SelectObject.

Also try the CopyImage function.
http://msdn.microsoft.com/en-us/library/ms648031(VS.85).aspx[^]

It may be useful for you...

Good Luck!
 
Share this answer
 
Comments
[no name] 20-Apr-11 10:02am    
CopyImage does not work. Can you see why?
Indeed, You need to use CreateCompatibleBitmap so that it uses the device context settings rather than trying to figure them out yourself. Other wise the select will fail as it cannot select the object due to the context being incorrect.
 
Share this answer
 
----------
----------
| i1 | i2 |
----------
----------

<pre lang="cs">case 29:
        /*//*/
        //  Stereo Horizontal
            {
                ZeroMemory(local_member_buffer_0,cxImage/2*cyImage/2*sizeof(RGBTRIPLE));
                ZeroMemory(prgb,cxImage*cyImage*sizeof(RGBTRIPLE));
                int local_destination_cx = cxImage/2;
                int local_destination_cy = cyImage/2;
                for(int local_counter_height=0;local_counter_height<local_destination_cy;local_counter_height++)
                {
                    for(int local_counter_width=0;local_counter_width<local_destination_cx;local_counter_width++)
                    {
                        local_member_buffer_0[local_counter_width + local_destination_cx*local_counter_height].rgbtRed
                            =
                            local_member_buffer_1[local_counter_width*2 + cxImage*local_counter_height*2].rgbtRed;
                        local_member_buffer_0[local_counter_width + local_destination_cx*local_counter_height].rgbtGreen
                            =
                            local_member_buffer_1[local_counter_width*2 + cxImage*local_counter_height*2].rgbtGreen;
                        local_member_buffer_0[local_counter_width + local_destination_cx*local_counter_height].rgbtBlue
                            =
                            local_member_buffer_1[local_counter_width*2 + cxImage*local_counter_height*2].rgbtBlue;
                    }
                }

                //  Ëåâîå èçîáðàæåíèå
                for(local_counter_width=0;local_counter_width<cxImage/2;local_counter_width++)
                {
                    for(int local_counter=0;local_counter<cyImage/2;local_counter++)
                    {
                        prgb[local_counter_width+(local_counter+cyImage/4)*cxImage].rgbtRed
                            =
                            local_member_buffer_0[local_counter_width+local_counter*cxImage/2].rgbtRed
                            ;
                        prgb[local_counter_width+(local_counter+cyImage/4)*cxImage].rgbtGreen
                            =
                            local_member_buffer_0[local_counter_width+local_counter*cxImage/2].rgbtGreen
                            ;
                        prgb[local_counter_width+(local_counter+cyImage/4)*cxImage].rgbtBlue
                            =
                            local_member_buffer_0[local_counter_width+local_counter*cxImage/2].rgbtBlue// + 0xff
                            ;
                    }
                }

                //  Ïðàâîå èçîáðàæåíèå
                for(local_counter_width=0;local_counter_width<cxImage/2;local_counter_width++)
                {
                    for(int local_counter=0;local_counter<cyImage/2;local_counter++)
                    {
                        prgb[local_counter_width+cxImage/2+(local_counter+cyImage/4)*cxImage].rgbtRed
                            =
                            local_member_buffer_0[local_counter_width+local_counter*cxImage/2].rgbtRed// + 0xff
                            ;
                        prgb[local_counter_width+cxImage/2+(local_counter+cyImage/4)*cxImage].rgbtGreen
                            =
                            local_member_buffer_0[local_counter_width+local_counter*cxImage/2].rgbtGreen
                            ;
                        prgb[local_counter_width+cxImage/2+(local_counter+cyImage/4)*cxImage].rgbtBlue
                            =
                            local_member_buffer_0[local_counter_width+local_counter*cxImage/2].rgbtBlue
                            ;
                    }
                }
            }
        break;


 
Share this answer
 
v3
Comments
[no name] 21-Apr-11 4:11am    
This code drops each odd line and row to get half width and half height image.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900