HBITMAP create_hbitmap_from_gdiplus_bitmap(Gdiplus::Bitmap * bitmap_ptr)
    {
        BITMAP bm;
        Gdiplus::Rect rect;
        Gdiplus::BitmapData bmpdata;
        HBITMAP hBitmap;

        rect.X = rect.Y = 0;
        rect.Width = bitmap_ptr->GetWidth();
        rect.Height = bitmap_ptr->GetHeight();

        if (bitmap_ptr->LockBits(&rect, Gdiplus::ImageLockModeRead, PixelFormat32bppPARGB, &bmpdata) != Gdiplus::Ok)
        {
            // Error
            return NULL;
        }

        bm.bmType       = 0;
        bm.bmWidth      = bmpdata.Width;
        bm.bmHeight     = bmpdata.Height;
        bm.bmWidthBytes = bmpdata.Stride;
        bm.bmPlanes     = 1;
        bm.bmBitsPixel  = 32;
        bm.bmBits       = bmpdata.Scan0;

        hBitmap = CreateBitmapIndirect(&bm);
        bitmap_ptr->UnlockBits(&bmpdata);
        return hBitmap;
    }
void CAtmoGradients::SetActiveZone(int zone)
{
	if(!m_ZoneRadios || (zone<0)) return;


	CAtmoZoneDefinition *azone = m_pConfig->getZoneDefinition( zone );
	if(!azone) return;

	m_active_zone = zone;

	int *destWeight = new int[CAP_WIDTH * CAP_HEIGHT];

	azone->UpdateWeighting(destWeight, m_pConfig->getLiveView_WidescreenMode(), m_edge_weight);

	BITMAP bmp;
	int byte = (CAP_WIDTH * 4);
	if(byte & 1) 
		byte++;

	unsigned char *pixel_buffer = new unsigned char[byte * CAP_HEIGHT];
	unsigned char *tmp = pixel_buffer;
	for(int y = 0; y < CAP_HEIGHT; y++) 
	{
		tmp = pixel_buffer + y * byte;
		for(int x = 0; x < CAP_WIDTH; x++) 
		{
			*tmp = 0; tmp++; // R
			*tmp = (unsigned char)destWeight[y * CAP_WIDTH + x]; tmp++; // G
			*tmp = 0; tmp++; // B
			*tmp = 0; tmp++; // A
		}
	}

	bmp.bmType = 0;
	bmp.bmBitsPixel = 32;
	bmp.bmHeight = CAP_HEIGHT;
	bmp.bmWidth  = CAP_WIDTH;
	bmp.bmWidthBytes = byte;
	bmp.bmPlanes = 1;
	bmp.bmBits   = pixel_buffer;

	if(m_current_gradient)
	{
		DeleteObject( m_current_gradient );
		m_current_gradient = 0;
	}
	m_current_gradient = CreateBitmapIndirect(&bmp);
	DWORD error = GetLastError();

	delete []destWeight;
	delete []pixel_buffer;
}
Exemple #3
0
HBITMAP StretchBitmap (HBITMAP hBitmap1)
{
     BITMAP     bm1, bm2 ;
     HBITMAP    hBitmap2 ;
     HDC        hdc, hdcMem1, hdcMem2 ;
     int        cxChar, cyChar ;

          // Get the width and height of a system font character

     cxChar = LOWORD (GetDialogBaseUnits ()) ;
     cyChar = HIWORD (GetDialogBaseUnits ()) ;

          // Create 2 memory DCs compatible with the display
     
     hdc = CreateIC (TEXT ("DISPLAY"), NULL, NULL, NULL) ;
     hdcMem1 = CreateCompatibleDC (hdc) ;
     hdcMem2 = CreateCompatibleDC (hdc) ;
     DeleteDC (hdc) ;

          // Get the dimensions of the bitmap to be stretched
     
     GetObject (hBitmap1, sizeof (BITMAP), (PTSTR) &bm1) ;

          // Scale these dimensions based on the system font size
     
     bm2 = bm1 ;
     bm2.bmWidth      = (cxChar * bm2.bmWidth)  / 4 ;
     bm2.bmHeight     = (cyChar * bm2.bmHeight) / 8 ;
     bm2.bmWidthBytes = ((bm2.bmWidth + 15) / 16) * 2 ;

          // Create a new bitmap of larger size
     
     hBitmap2 = CreateBitmapIndirect (&bm2) ;

          // Select the bitmaps in the memory DCs and do a StretchBlt
     
     SelectObject (hdcMem1, hBitmap1) ;
     SelectObject (hdcMem2, hBitmap2) ;
     
     StretchBlt (hdcMem2, 0, 0, bm2.bmWidth, bm2.bmHeight,
                 hdcMem1, 0, 0, bm1.bmWidth, bm1.bmHeight, SRCCOPY) ;

          // Clean up
     
     DeleteDC (hdcMem1) ;
     DeleteDC (hdcMem2) ;
     DeleteObject (hBitmap1) ;
     
     return hBitmap2 ;
}
Exemple #4
0
/******************************************************************************
 * CreateBitmap [GDI32.@]
 *
 * Creates a bitmap with the specified info.
 *
 * PARAMS
 *    width  [I] bitmap width
 *    height [I] bitmap height
 *    planes [I] Number of color planes
 *    bpp    [I] Number of bits to identify a color
 *    bits   [I] Pointer to array containing color data
 *
 * RETURNS
 *    Success: Handle to bitmap
 *    Failure: 0
 */
HBITMAP WINAPI CreateBitmap( INT width, INT height, UINT planes,
                             UINT bpp, LPCVOID bits )
{
    BITMAP bm;

    bm.bmType = 0;
    bm.bmWidth = width;
    bm.bmHeight = height;
    bm.bmWidthBytes = get_bitmap_stride( width, bpp );
    bm.bmPlanes = planes;
    bm.bmBitsPixel = bpp;
    bm.bmBits = (LPVOID)bits;

    return CreateBitmapIndirect( &bm );
}
Exemple #5
0
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
     static BITMAP  bitmap = { 0, 8, 8, 2, 1, 1 } ;
     static BYTE    bits [8][2] = { 0xFF, 0, 0x0C, 0, 0x0C, 0, 0x0C, 0,
                                    0xFF, 0, 0xC0, 0, 0xC0, 0, 0xC0, 0 } ;
     static HBITMAP hBitmap ;
     static int     cxClient, cyClient, cxSource, cySource ;
     HDC            hdc, hdcMem ;
     int            x, y ;
     PAINTSTRUCT    ps ;
     
     switch (message)
     {
     case WM_CREATE:
          bitmap.bmBits = bits ;
          hBitmap = CreateBitmapIndirect (&bitmap) ;
          cxSource = bitmap.bmWidth ;
          cySource = bitmap.bmHeight ;
          return 0 ;

     case WM_SIZE:
          cxClient = LOWORD (lParam) ;
          cyClient = HIWORD (lParam) ;
          return 0 ;

     case WM_PAINT:
          hdc = BeginPaint (hwnd, &ps) ;

          hdcMem = CreateCompatibleDC (hdc) ;
          SelectObject (hdcMem, hBitmap) ;

          for (y = 0 ; y < cyClient ; y += cySource)
          for (x = 0 ; x < cxClient ; x += cxSource)
          {
               BitBlt (hdc, x, y, cxSource, cySource, hdcMem, 0, 0, SRCCOPY) ;
          }

          DeleteDC (hdcMem) ;
          EndPaint (hwnd, &ps) ;
          return 0 ;

     case WM_DESTROY:
          DeleteObject (hBitmap) ;
          PostQuitMessage (0) ;
          return 0 ;
     }
     return DefWindowProc (hwnd, message, wParam, lParam) ;
}
Exemple #6
0
// helper function - creates a bitmap of the specified width and height
HBITMAP CreateScreenCompatibleBitmap( INT32 width, INT32 height)
{
    HDC hProbeDC = ::CreateCompatibleDC(NULL); // device context used to probe the current screen mode
    ERROR3IF(hProbeDC == NULL, "Couldn't create probe DC");
    const BITMAP bitmapData =
    {
        0,
        width,
        height,
        width * GetDeviceCaps(hProbeDC, BITSPIXEL) * GetDeviceCaps(hProbeDC, PLANES) / 8 ,
        GetDeviceCaps(hProbeDC, PLANES),
        GetDeviceCaps(hProbeDC, BITSPIXEL),
        0L
    };
    DeleteDC(hProbeDC);
    return CreateBitmapIndirect(&bitmapData);
}
Exemple #7
0
/**********************************************************************
 *		BITMAP_CopyBitmap
 *
 */
HBITMAP BITMAP_CopyBitmap(HBITMAP hbitmap)
{
    HBITMAP res = 0;
    BITMAP bm;

    if (!GetObjectW( hbitmap, sizeof(bm), &bm )) return 0;
    res = CreateBitmapIndirect(&bm);

    if(res) {
        char *buf = HeapAlloc( GetProcessHeap(), 0, bm.bmWidthBytes *
			       bm.bmHeight );
        GetBitmapBits (hbitmap, bm.bmWidthBytes * bm.bmHeight, buf);
	SetBitmapBits (res, bm.bmWidthBytes * bm.bmHeight, buf);
	HeapFree( GetProcessHeap(), 0, buf );
    }
    return res;
}
Exemple #8
0
HBITMAP IconToAndBitmap( HDC hdc, an_icon *icon )
{
    BITMAP              bitmap;
    HBITMAP             bitmap_handle;
    BITMAPINFOHEADER    *h;

    hdc = hdc;
    h = &icon->bm->bmiHeader;
    bitmap.bmType = 0;
    bitmap.bmWidth = h->biWidth;
    bitmap.bmHeight = h->biWidth;
    bitmap.bmWidthBytes = ( bitmap.bmWidth + 7 ) / 8;
    bitmap.bmPlanes = 1;
    bitmap.bmBitsPixel = 1;
    bitmap.bmBits = icon->and_mask;
    bitmap_handle = CreateBitmapIndirect( &bitmap );
    return( bitmap_handle );
} /* IconToAndBitmap */
Exemple #9
0
/*
 * __CreateBitmapIndirect - cover for CreateBitmapIndirect
 */
HBITMAP FAR PASCAL __CreateBitmapIndirect( LPBITMAP bm )
{
    HBITMAP     rc;
    DWORD       alias,size;
    LPSTR       old;

    size = (DWORD) bm->bmHeight * (DWORD) bm->bmWidthBytes;
    size *= (DWORD) bm->bmPlanes;
    DPMIGetHugeAlias( (DWORD) bm->bmBits, &alias, size );
    old = bm->bmBits;
    bm->bmBits = (LPSTR) alias;

    rc = CreateBitmapIndirect( bm );

    DPMIFreeHugeAlias( alias, size );
    bm->bmBits = old;

    return( rc );

} /* __CreateBitmapIndirect */
Exemple #10
0
/**********************************************************************
 *		BITMAP_CopyBitmap
 *
 */
HBITMAP BITMAP_CopyBitmap(HBITMAP hbitmap)
{
    HBITMAP res;
    DIBSECTION dib;
    DWORD size;

    if (!(size = GetObjectW( hbitmap, sizeof(dib), &dib ))) return 0;

    if (size == sizeof(DIBSECTION))
    {
        void *bits;
        BITMAPINFO *bi;
        HDC dc = CreateCompatibleDC( NULL );

        if (!dc) return 0;
        if (!(bi = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] ))))
        {
            DeleteDC( dc );
            return 0;
        }
        bi->bmiHeader = dib.dsBmih;

        /* Get the color table or the color masks */
        GetDIBits( dc, hbitmap, 0, 0, NULL, bi, DIB_RGB_COLORS );

        res = CreateDIBSection( dc, bi, DIB_RGB_COLORS, &bits, NULL, 0 );
        if (res) SetDIBits( dc, res, 0, dib.dsBm.bmHeight, dib.dsBm.bmBits, bi, DIB_RGB_COLORS );
        HeapFree( GetProcessHeap(), 0, bi );
        DeleteDC( dc );
        return res;
    }

    res = CreateBitmapIndirect( &dib.dsBm );
    if(res) {
        char *buf = HeapAlloc( GetProcessHeap(), 0, dib.dsBm.bmWidthBytes * dib.dsBm.bmHeight );
        GetBitmapBits (hbitmap, dib.dsBm.bmWidthBytes * dib.dsBm.bmHeight, buf);
        SetBitmapBits (res, dib.dsBm.bmWidthBytes * dib.dsBm.bmHeight, buf);
	HeapFree( GetProcessHeap(), 0, buf );
    }
    return res;
}
Exemple #11
0
static BOOL ClipboardReadBitmap(HANDLE hFile, DWORD dwOffset, DWORD dwLength)
{
    HGLOBAL hData;
    HBITMAP hBitmap;
    LPBITMAP lpBitmap;

    hData = ClipboardReadMemoryBlock(hFile, dwOffset, dwLength);
    if (!hData)
    {
        return FALSE;
    }

    lpBitmap = GlobalLock(hData);
    if (!lpBitmap)
    {
        GlobalFree(hData);
        return FALSE;
    }

    lpBitmap->bmBits = lpBitmap + sizeof(BITMAP) + 1;

    hBitmap = CreateBitmapIndirect(lpBitmap);

    GlobalUnlock(hData);
    GlobalFree(hData);

    if (!hBitmap)
    {
        SetLastError(ERROR_OUTOFMEMORY);
        return FALSE;
    }

    if (!SetClipboardData(CF_BITMAP, hBitmap))
    {
        DeleteObject(hBitmap);
        return FALSE;
    }

    return TRUE;
}
Exemple #12
0
HBITMAP CopyBitmap(HBITMAP hBitmapSrc)
{
	BITMAP  bitmap;
	HBITMAP hBitmapDst;
	HDC     hdcSrc, hdcDst;

	GetObject(hBitmapSrc, sizeof (BITMAP), &bitmap);
	hBitmapDst = CreateBitmapIndirect(&bitmap);

	hdcSrc = CreateCompatibleDC(NULL);
	hdcDst = CreateCompatibleDC(NULL);

	SelectObject(hdcSrc, hBitmapSrc);
	SelectObject(hdcDst, hBitmapDst);

	BitBlt(hdcDst, 0, 0, bitmap.bmWidth, bitmap.bmHeight,
		hdcSrc, 0, 0, SRCCOPY);

	DeleteDC(hdcSrc);
	DeleteDC(hdcDst);

	return hBitmapDst;
}
Exemple #13
0
// -----------------------------------------------------------------------
// Stencil the "set" bits of the bitmap onto the canvas in the given color
// at the given origin.  The origin is in terms of the point system with
// the origin in the lower left, which must be converted to the device
// coordinate system which has an origin in the upper left.  Further, the
// blt coordinates must be adjusted this way as well.
//
// If the transformation matrix is identity, the bitmap is simply blt'd
// using the raster operation discussed in the Petzold V3.0 book on page
// 624 which transfers only the black bits.  It corresponds to the operation
// ((Destination ^ Pattern) & Source) ^ Pattern.  If the transformer is not
// identity, then the Windows function StretchBlt is used to scale the
// bitmap.  The default mode of STRETCH_ANDSCANS is appropriate since it
// favors the black bits.
//
// Currently, a rotated bitmap is NOT supported!
// -----------------------------------------------------------------------
void MWcanvas16::stencil(
    const Bitmap* mask, 			// bitmap to render
    const Color* c, 				// color to use
    Coord x, 						// x-coordinate
    Coord y) 						// y-coordinate
{
    BitmapRep& br = * mask->rep();
    HBITMAP hbm = CreateBitmapIndirect((BITMAP*) &(br.bm_));
    HDC mem_hdc = CreateCompatibleDC(drawable_);
    SelectObject(mem_hdc, hbm);

    //
    // convert the coordinates to TWIPS for the page space to device space
    // mapping.
    //
    int x0 = COORD2TWIPS( x + mask->left_bearing() );
    int y0 = COORD2TWIPS( y - mask->descent() );
    int width = COORD2TWIPS( mask->width() );
    int height = COORD2TWIPS( mask->height() );


    // ---- set the color to use ----
    flush();
    color(c);

    // For some reason, text color must be black when stenciling or the
    // colors of the stencil are affected.
    SetTextColor(drawable_, RGB(0, 0, 0));

    // ---- blt... the destination will be transformed ----
    MWassert( StretchBlt(drawable_, x0, y0 + height, width, - height,
                         mem_hdc, 0, 0, br.bm_.bmWidth, br.bm_.bmHeight, 0xB8074AL) );

    // ---- cleanup ----
    DeleteDC(mem_hdc);
    DeleteObject(hbm);
}
Exemple #14
0
void *wxGetClipboardData(wxDataFormat dataFormat, long *len)
{
    void *retval = NULL;

    switch ( dataFormat )
    {
#ifndef __WXWINCE__
        case wxDF_BITMAP:
            {
                BITMAP bm;
                HBITMAP hBitmap = (HBITMAP) GetClipboardData(CF_BITMAP);
                if (!hBitmap)
                    break;

                HDC hdcMem = CreateCompatibleDC((HDC) NULL);
                HDC hdcSrc = CreateCompatibleDC((HDC) NULL);

                HBITMAP old = (HBITMAP) ::SelectObject(hdcSrc, hBitmap);
                GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&bm);

                HBITMAP hNewBitmap = CreateBitmapIndirect(&bm);

                if (!hNewBitmap)
                {
                    SelectObject(hdcSrc, old);
                    DeleteDC(hdcMem);
                    DeleteDC(hdcSrc);
                    break;
                }

                HBITMAP old1 = (HBITMAP) SelectObject(hdcMem, hNewBitmap);
                BitBlt(hdcMem, 0, 0, bm.bmWidth, bm.bmHeight,
                       hdcSrc, 0, 0, SRCCOPY);

                // Select new bitmap out of memory DC
                SelectObject(hdcMem, old1);

                // Clean up
                SelectObject(hdcSrc, old);
                DeleteDC(hdcSrc);
                DeleteDC(hdcMem);

                // Create and return a new wxBitmap
                wxBitmap *wxBM = new wxBitmap;
                wxBM->SetHBITMAP((WXHBITMAP) hNewBitmap);
                wxBM->SetWidth(bm.bmWidth);
                wxBM->SetHeight(bm.bmHeight);
                wxBM->SetDepth(bm.bmPlanes);
                retval = wxBM;
                break;
            }
#endif
        case wxDF_METAFILE:
        case CF_SYLK:
        case CF_DIF:
        case CF_TIFF:
        case CF_PALETTE:
        case wxDF_DIB:
            wxLogError(_("Unsupported clipboard format."));
            return NULL;

        case wxDF_OEMTEXT:
            dataFormat = wxDF_TEXT;
            // fall through

        case wxDF_TEXT:
            {
                HANDLE hGlobalMemory = ::GetClipboardData(dataFormat);
                if (!hGlobalMemory)
                    break;

                DWORD hsize = ::GlobalSize(hGlobalMemory);
                if (len)
                    *len = hsize;

                char *s = new char[hsize];
                if (!s)
                    break;

                LPSTR lpGlobalMemory = (LPSTR) GlobalLock(hGlobalMemory);

                memcpy(s, lpGlobalMemory, hsize);

                GlobalUnlock(hGlobalMemory);

                retval = s;
                break;
            }

        default:
            {
                HANDLE hGlobalMemory = ::GetClipboardData(dataFormat);
                if ( !hGlobalMemory )
                    break;

                DWORD size = ::GlobalSize(hGlobalMemory);
                if ( len )
                    *len = size;

                void *buf = malloc(size);
                if ( !buf )
                    break;

                LPSTR lpGlobalMemory = (LPSTR) GlobalLock(hGlobalMemory);

                memcpy(buf, lpGlobalMemory, size);

                GlobalUnlock(hGlobalMemory);

                retval = buf;
                break;
            }
    }

    if ( !retval )
    {
        wxLogSysError(_("Failed to retrieve data from the clipboard."));
    }

    return retval;
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                                                                             %
%                                                                             %
%   C r o p I m a g e T o H B i t m a p                                       %
%                                                                             %
%                                                                             %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  CropImageToHBITMAP() extracts a specified region of the image and returns
%  it as a Windows HBITMAP. While the same functionality can be accomplished by
%  invoking CropImage() followed by ImageToHBITMAP(), this method is more
%  efficient since it copies pixels directly to the HBITMAP.
%
%  The format of the CropImageToHBITMAP method is:
%
%      HBITMAP CropImageToHBITMAP(Image* image,const RectangleInfo *geometry,
%        ExceptionInfo *exception)
%
%  A description of each parameter follows:
%
%    o image: the image.
%
%    o geometry: Define the region of the image to crop with members
%      x, y, width, and height.
%
%    o exception: return any errors or warnings in this structure.
%
*/
MagickExport void *CropImageToHBITMAP(Image *image,
  const RectangleInfo *geometry,ExceptionInfo *exception)
{
#define CropImageTag  "Crop/Image"

  BITMAP
    bitmap;

  HBITMAP
    bitmapH;

  HANDLE
    bitmap_bitsH;

  MagickBooleanType
    proceed;

  RectangleInfo
    page;

  register const PixelPacket
    *p;

  register RGBQUAD
    *q;

  RGBQUAD
    *bitmap_bits;

  ssize_t
    y;

  /*
    Check crop geometry.
  */
  assert(image != (const Image *) NULL);
  assert(image->signature == MagickSignature);
  if (image->debug != MagickFalse)
    (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
  assert(geometry != (const RectangleInfo *) NULL);
  assert(exception != (ExceptionInfo *) NULL);
  assert(exception->signature == MagickSignature);
  if (((geometry->x+(ssize_t) geometry->width) < 0) ||
      ((geometry->y+(ssize_t) geometry->height) < 0) ||
      (geometry->x >= (ssize_t) image->columns) ||
      (geometry->y >= (ssize_t) image->rows))
    ThrowImageException(OptionError,"GeometryDoesNotContainImage");
  page=(*geometry);
  if ((page.x+(ssize_t) page.width) > (ssize_t) image->columns)
    page.width=image->columns-page.x;
  if ((page.y+(ssize_t) page.height) > (ssize_t) image->rows)
    page.height=image->rows-page.y;
  if (page.x < 0)
    {
      page.width+=page.x;
      page.x=0;
    }
  if (page.y < 0)
    {
      page.height+=page.y;
      page.y=0;
    }

  if ((page.width == 0) || (page.height == 0))
    ThrowImageException(OptionError,"GeometryDimensionsAreZero");
  /*
    Initialize crop image attributes.
  */
  bitmap.bmType         = 0;
  bitmap.bmWidth        = (LONG) page.width;
  bitmap.bmHeight       = (LONG) page.height;
  bitmap.bmWidthBytes   = bitmap.bmWidth * 4;
  bitmap.bmPlanes       = 1;
  bitmap.bmBitsPixel    = 32;
  bitmap.bmBits         = NULL;

  bitmap_bitsH=(HANDLE) GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE,page.width*
    page.height*bitmap.bmBitsPixel);
  if (bitmap_bitsH == NULL)
    return(NULL);
  bitmap_bits=(RGBQUAD *) GlobalLock((HGLOBAL) bitmap_bitsH);
  if ( bitmap.bmBits == NULL )
    bitmap.bmBits = bitmap_bits;
  if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
    SetImageColorspace(image,sRGBColorspace);
  /*
    Extract crop image.
  */
  q=bitmap_bits;
  for (y=0; y < (ssize_t) page.height; y++)
  {
    register ssize_t
      x;

    p=GetVirtualPixels(image,page.x,page.y+y,page.width,1,exception);
    if (p == (const PixelPacket *) NULL)
      break;

    /* Transfer pixels, scaling to Quantum */
    for( x=(ssize_t) page.width ; x> 0 ; x-- )
    {
      q->rgbRed = ScaleQuantumToChar(GetPixelRed(p));
      q->rgbGreen = ScaleQuantumToChar(GetPixelGreen(p));
      q->rgbBlue = ScaleQuantumToChar(GetPixelBlue(p));
      q->rgbReserved = 0;
      p++;
      q++;
    }
    proceed=SetImageProgress(image,CropImageTag,y,page.height);
    if (proceed == MagickFalse)
      break;
  }
  if (y < (ssize_t) page.height)
    {
      GlobalUnlock((HGLOBAL) bitmap_bitsH);
      GlobalFree((HGLOBAL) bitmap_bitsH);
      return((void *) NULL);
    }
  bitmap.bmBits=bitmap_bits;
  bitmapH=CreateBitmapIndirect(&bitmap);
  GlobalUnlock((HGLOBAL) bitmap_bitsH);
  GlobalFree((HGLOBAL) bitmap_bitsH);
  return((void *) bitmapH);
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                                                                             %
%                                                                             %
%   I m a g e T o H B i t m a p                                               %
%                                                                             %
%                                                                             %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  ImageToHBITMAP() creates a Windows HBITMAP from an image.
%
%  The format of the ImageToHBITMAP method is:
%
%      HBITMAP ImageToHBITMAP(Image *image,Exceptioninfo *exception)
%
%  A description of each parameter follows:
%
%    o image: the image to convert.
%
*/
MagickExport void *ImageToHBITMAP(Image *image,ExceptionInfo *exception)
{
  BITMAP
    bitmap;

  HANDLE
    bitmap_bitsH;

  HBITMAP
    bitmapH;

  register ssize_t
    x;

  register const PixelPacket
    *p;

  register RGBQUAD
    *q;

  RGBQUAD
    *bitmap_bits;

  size_t
    length;

  ssize_t
    y;

  (void) ResetMagickMemory(&bitmap,0,sizeof(bitmap));
  bitmap.bmType=0;
  bitmap.bmWidth=(LONG) image->columns;
  bitmap.bmHeight=(LONG) image->rows;
  bitmap.bmWidthBytes=4*bitmap.bmWidth;
  bitmap.bmPlanes=1;
  bitmap.bmBitsPixel=32;
  bitmap.bmBits=NULL;
  length=bitmap.bmWidthBytes*bitmap.bmHeight;
  bitmap_bitsH=(HANDLE) GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE,length);
  if (bitmap_bitsH == NULL)
    {
      char
        *message;

      message=GetExceptionMessage(errno);
      (void) ThrowMagickException(exception,GetMagickModule(),
        ResourceLimitError,"MemoryAllocationFailed","`%s'",message);
      message=DestroyString(message);
      return(NULL);
    }
  bitmap_bits=(RGBQUAD *) GlobalLock((HGLOBAL) bitmap_bitsH);
  q=bitmap_bits;
  if (bitmap.bmBits == NULL)
    bitmap.bmBits=bitmap_bits;
  (void) SetImageColorspace(image,sRGBColorspace);
  for (y=0; y < (ssize_t) image->rows; y++)
  {
    p=GetVirtualPixels(image,0,y,image->columns,1,exception);
    if (p == (const PixelPacket *) NULL)
      break;
    for (x=0; x < (ssize_t) image->columns; x++)
    {
      q->rgbRed=ScaleQuantumToChar(GetPixelRed(p));
      q->rgbGreen=ScaleQuantumToChar(GetPixelGreen(p));
      q->rgbBlue=ScaleQuantumToChar(GetPixelBlue(p));
      q->rgbReserved=0;
      p++;
      q++;
    }
  }
  bitmap.bmBits=bitmap_bits;
  bitmapH=CreateBitmapIndirect(&bitmap);
  if (bitmapH == NULL)
    {
      char
        *message;

      message=GetExceptionMessage(errno);
      (void) ThrowMagickException(exception,GetMagickModule(),
        ResourceLimitError,"MemoryAllocationFailed","`%s'",message);
      message=DestroyString(message);
    }
  GlobalUnlock((HGLOBAL) bitmap_bitsH);
  GlobalFree((HGLOBAL) bitmap_bitsH);
  return((void *) bitmapH);
}
Exemple #17
0
long	UPSF::BITMAP_To_UBit(
	HGLOBAL&	hUniv,
	long&			univSize,
	BITMAP		bitmap,
	HBITMAP		hBmp)

	{
	WORD	nColorBits = bitmap.bmBitsPixel*bitmap.bmPlanes;
	if(nColorBits!=1 && nColorBits!=4 && nColorBits!=8 && nColorBits!=24)
		return AUPSF_CANTCONVERT;

	long	ColorTableSize;
	if(nColorBits==24)
		{
		// 24-bit color -- no color table
		ColorTableSize = 0;
		}
	else
		ColorTableSize = pow2(nColorBits);

	univSize = sizeof(BITMAPINFOHEADER) + ColorTableSize*sizeof(RGBQUAD);
	if(!(hUniv = GlobalReAlloc(hUniv, univSize, MEMFLAGS)))
		return AUPSF_MEMORYERROR;

	BITMAPINFOHEADER* bmpIH = (BITMAPINFOHEADER*)GlobalLock(hUniv);
	if(!bmpIH)
  	return AUPSF_MEMORYERROR;

	bmpIH->biWidth					= bitmap.bmWidth;
	bmpIH->biHeight					= bitmap.bmHeight;
	bmpIH->biPlanes					= 1;
	bmpIH->biSize						= sizeof(BITMAPINFOHEADER);
	bmpIH->biBitCount				= nColorBits;
	bmpIH->biCompression		= BI_RGB;

	bmpIH->biXPelsPerMeter	= 0;
	bmpIH->biYPelsPerMeter	= 0;

	bmpIH->biSizeImage			= 0;
	bmpIH->biClrUsed				= 0;
	bmpIH->biClrImportant		= 0;

  // get some handles for bitmap conversion
	HDC			hDC = CreateDC("DISPLAY", NULL, NULL, NULL);
	HBITMAP hBitmap;
  if(!hBmp)
		hBitmap = CreateBitmapIndirect(&bitmap);
	else
  	hBitmap = hBmp;

	// fill in the BITMAPINFO structure -- fills in the color table
	GetDIBits(hDC, hBitmap, 0, bitmap.bmHeight, NULL, (LPBITMAPINFO)bmpIH, DIB_RGB_COLORS);

  // check to see if the video driver messed up!
	if(bmpIH->biSizeImage==0)
		bmpIH->biSizeImage = (((DWORD)bitmap.bmWidth * nColorBits + 31)/8) * bitmap.bmHeight;

	long	BitmapSize = bmpIH->biSizeImage;
  GlobalUnlock(hUniv);

	// resize the universal memory without losing information
  univSize += BitmapSize;
	if(!(hUniv = GlobalReAlloc(hUniv, univSize, MEMFLAGS)))
		return AUPSF_MEMORYERROR;
	bmpIH = (BITMAPINFOHEADER*)GlobalLock(hUniv);
	if(!bmpIH)
  	return AUPSF_MEMORYERROR;

	void*	bits = (void*)((BYTE*)bmpIH + (size_t)(ColorTableSize*sizeof(RGBQUAD) + sizeof(BITMAPINFOHEADER)));

	 // fill in the bitmap data -- copies the bits automatically
	GetDIBits(hDC, hBitmap, 0, bitmap.bmHeight, (char*)bits, (LPBITMAPINFO)bmpIH, DIB_RGB_COLORS);

  // free the resources
  if(!hBmp)
	  DeleteObject(hBitmap);
	DeleteDC(hDC);

	GlobalUnlock(hUniv);
  return AUPSF_NOERROR;
	}
Exemple #18
0
/*****************************************************************
 *		CreateCaret (USER32.@)
 */
BOOL WINAPI CreateCaret( HWND hwnd, HBITMAP bitmap, INT width, INT height )
{
    BOOL ret;
    RECT r;
    int old_state = 0;
    int hidden = 0;
    HBITMAP hBmp = 0;
    HWND prev = 0;

    TRACE("hwnd=%p\n", hwnd);

    if (!hwnd) return FALSE;

    if (bitmap && (bitmap != (HBITMAP)1))
    {
        BITMAP bmp;
        if (!GetObjectA( bitmap, sizeof(bmp), &bmp )) return FALSE;
        width = bmp.bmWidth;
        height = bmp.bmHeight;
	bmp.bmBits = NULL;
	hBmp = CreateBitmapIndirect(&bmp);
	if (hBmp)
	{
	    /* copy the bitmap */
	    LPBYTE buf = HeapAlloc(GetProcessHeap(), 0, bmp.bmWidthBytes * bmp.bmHeight);
	    GetBitmapBits(bitmap, bmp.bmWidthBytes * bmp.bmHeight, buf);
	    SetBitmapBits(hBmp, bmp.bmWidthBytes * bmp.bmHeight, buf);
	    HeapFree(GetProcessHeap(), 0, buf);
	}
    }
    else
    {
	HDC hdc;

        if (!width) width = GetSystemMetrics(SM_CXBORDER);
        if (!height) height = GetSystemMetrics(SM_CYBORDER);

	/* create the uniform bitmap on the fly */
	hdc = GetDC(hwnd);
	if (hdc)
	{
	    HDC hMemDC = CreateCompatibleDC(hdc);
	    if (hMemDC)
	    {
		if ((hBmp = CreateCompatibleBitmap(hMemDC, width, height )))
		{
		    HBITMAP hPrevBmp = SelectObject(hMemDC, hBmp);
                    SetRect( &r, 0, 0, width, height );
		    FillRect(hMemDC, &r, ULongToHandle((bitmap ? COLOR_GRAYTEXT : COLOR_WINDOW) + 1));
		    SelectObject(hMemDC, hPrevBmp);
		}
		DeleteDC(hMemDC);
	    }
	    ReleaseDC(hwnd, hdc);
	}
    }
    if (!hBmp) return FALSE;

    SERVER_START_REQ( set_caret_window )
    {
        req->handle = wine_server_user_handle( hwnd );
        req->width  = width;
        req->height = height;
        if ((ret = !wine_server_call_err( req )))
        {
            prev      = wine_server_ptr_handle( reply->previous );
            r.left    = reply->old_rect.left;
            r.top     = reply->old_rect.top;
            r.right   = reply->old_rect.right;
            r.bottom  = reply->old_rect.bottom;
            old_state = reply->old_state;
            hidden    = reply->old_hide;
        }
    }
    SERVER_END_REQ;
    if (!ret) return FALSE;

    if (prev && !hidden)  /* hide the previous one */
    {
        /* FIXME: won't work if prev belongs to a different process */
        KillSystemTimer( prev, TIMERID );
        if (old_state) CARET_DisplayCaret( prev, &r );
    }

    if (Caret.hBmp) DeleteObject( Caret.hBmp );
    Caret.hBmp = hBmp;
    Caret.timeout = GetProfileIntA( "windows", "CursorBlinkRate", 500 );
    return TRUE;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    
    PAINTSTRUCT ps;
    HDC hdc;
    
    switch (message) 
    {
    case WM_CREATE:
        {
            if (!hmWnd)
                hmWnd = hWnd;

            hdc = GetDC(hWnd);
            CBIT = GetDeviceCaps(hdc, BITSPIXEL);    
            CBYTE = CBIT/8;
            ReleaseDC(hWnd, hdc);
            
            buffer = malloc(width*height*CBYTE);
            bmp.bmType = 0;
            bmp.bmWidth = width;
            bmp.bmHeight = height;
            bmp.bmWidthBytes = width*CBYTE;
            bmp.bmPlanes = 1;
            bmp.bmBitsPixel = CBIT;
            bmp.bmBits = buffer;
            
            if (CBYTE == 4)
                fmt = COLOR_FORMAT_BGRA;
            else if (CBYTE == 3)
                fmt = COLOR_FORMAT_BGR;
            else if (CBYTE == 2)
                fmt = COLOR_FORMAT_RGB565;

            ps_initialize();

            canvas = ps_canvas_create_with_data(buffer, fmt, width, height, width*CBYTE);
            context = ps_context_create(canvas, 0);
            on_init(context, width, height);    
        }
        break;
    case WM_PAINT:
        {
            HDC mdc;
            HGDIOBJ h;
            hdc = BeginPaint(hWnd, &ps);
            
            on_draw(context);
            
            mdc = CreateCompatibleDC(hdc);
            hbmp = CreateBitmapIndirect(&bmp);
            h = SelectObject(mdc, hbmp);
            BitBlt(hdc, 0, 0, width, height, mdc, 0, 0, SRCCOPY);
            SelectObject(mdc, h);
            DeleteObject(hbmp);
            DeleteDC(mdc);
            EndPaint(hWnd, &ps);
        }
        break;
    case WM_TIMER:
        on_timer();
        break;
    case WM_SIZE:
        {
            ps_canvas* old_canvas = 0;
            width = LOWORD(lParam);
            height = HIWORD(lParam);
            free(buffer);

            if (width < 1)
                width = 1;
            if (height < 1)
                height = 1;

            buffer = malloc(width*height*CBYTE);
            bmp.bmType = 0;
            bmp.bmWidth = width;
            bmp.bmHeight = height;
            bmp.bmWidthBytes = width*CBYTE;
            bmp.bmPlanes = 1;
            bmp.bmBitsPixel = CBIT;
            bmp.bmBits = buffer;

            canvas = ps_canvas_create_with_data(buffer, fmt, width, height, width*CBYTE);
            old_canvas = ps_context_set_canvas(context, canvas);
            ps_canvas_unref(old_canvas);
            on_size(width, height);
        }
        break;
    case WM_ERASEBKGND:
        break;
    case WM_LBUTTONDOWN:
            on_mouse_event(LEFT_BUTTON_DOWN, wParam, LOWORD(lParam), HIWORD(lParam));
        break;
    case WM_MOUSEMOVE:
            on_mouse_event(MOUSE_MOVE, wParam, LOWORD(lParam), HIWORD(lParam));
        break;
    case WM_KEYDOWN:
            on_key_event(KEY_EVENT_DOWN, wParam);
        break;
    case WM_KEYUP:
            on_key_event(KEY_EVENT_UP, wParam);
        break;
    case WM_DESTROY:
        {
            on_term(context);
            ps_context_unref(context);
            ps_canvas_unref(canvas);
            ps_shutdown();
            free(buffer);
            PostQuitMessage(0);
        }
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}
Exemple #20
0
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                                                                             %
%                                                                             %
%   C r o p I m a g e T o H B i t m a p                                       %
%                                                                             %
%                                                                             %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  CropImageToHBITMAP() extracts a specified region of the image and returns
%  it as a Windows HBITMAP. While the same functionality can be accomplished by
%  invoking CropImage() followed by ImageToHBITMAP(), this method is more
%  efficient since it copies pixels directly to the HBITMAP.
%
%  The format of the CropImageToHBITMAP method is:
%
%      HBITMAP CropImageToHBITMAP(Image* image,const RectangleInfo *geometry,
%        ExceptionInfo *exception)
%
%  A description of each parameter follows:
%
%    o image: The image.
%
%    o geometry: Define the region of the image to crop with members
%      x, y, width, and height.
%
%    o exception: Return any errors or warnings in this structure.
%
*/
MagickExport void *CropImageToHBITMAP(Image *image,
  const RectangleInfo *geometry,ExceptionInfo *exception)
{
#define CropImageTag  "Crop/Image"

  long
    y;

  MagickBooleanType
    status;

  RectangleInfo
    page;

  register const PixelPacket
    *p;

  BITMAP
    bitmap;

  HBITMAP
    bitmapH;

  HANDLE
    bitmap_bitsH;

  register RGBQUAD
    *q;

  RGBQUAD
    *bitmap_bits;

  /*
    Check crop geometry.
  */
  assert(image != (const Image *) NULL);
  assert(image->signature == MagickSignature);
  if (image->debug != MagickFalse)
    (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
  assert(geometry != (const RectangleInfo *) NULL);
  assert(exception != (ExceptionInfo *) NULL);
  assert(exception->signature == MagickSignature);
  if (((geometry->x+(long) geometry->width) < 0) ||
      ((geometry->y+(long) geometry->height) < 0) ||
      (geometry->x >= (long) image->columns) ||
      (geometry->y >= (long) image->rows))
    ThrowImageException(OptionError,"GeometryDoesNotContainImage");
  page=(*geometry);
  if ((page.x+(long) page.width) > (long) image->columns)
    page.width=image->columns-page.x;
  if ((page.y+(long) page.height) > (long) image->rows)
    page.height=image->rows-page.y;
  if (page.x < 0)
    {
      page.width+=page.x;
      page.x=0;
    }
  if (page.y < 0)
    {
      page.height+=page.y;
      page.y=0;
    }

  if ((page.width == 0) || (page.height == 0))
    ThrowImageException(OptionError,"GeometryDimensionsAreZero");
  /*
    Initialize crop image attributes.
  */
  bitmap.bmType         = 0;
  bitmap.bmWidth        = page.width;
  bitmap.bmHeight       = page.height;
  bitmap.bmWidthBytes   = bitmap.bmWidth * 4;
  bitmap.bmPlanes       = 1;
  bitmap.bmBitsPixel    = 32;
  bitmap.bmBits         = NULL;

  bitmap_bitsH = (HANDLE) GlobalAlloc (GMEM_MOVEABLE | GMEM_DDESHARE,
               page.width*page.height*bitmap.bmBitsPixel);
  if (bitmap_bitsH == NULL)
    return(NULL);
  bitmap_bits=(RGBQUAD *) GlobalLock((HGLOBAL) bitmap_bitsH);
  if ( bitmap.bmBits == NULL )
    bitmap.bmBits = bitmap_bits;
  if (image->colorspace != RGBColorspace)
    SetImageColorspace(image,RGBColorspace);
  /*
    Extract crop image.
  */
  q=bitmap_bits;
  for (y=0; y < (long) page.height; y++)
  {
    p=AcquireImagePixels(image,page.x,page.y+y,page.width,1,exception);
    if (p == (const PixelPacket *) NULL)
      break;

#if MAGICKCORE_QUANTUM_DEPTH == 8
      /* Form of PixelPacket is identical to RGBQUAD when MAGICKCORE_QUANTUM_DEPTH==8 */
      CopyMagickMemory((void*)q,(const void*)p,page.width*sizeof(PixelPacket));
      q += page.width;

#else  /* 16 or 32 bit Quantum */
      {
        long
          x;

        /* Transfer pixels, scaling to Quantum */
        for( x=page.width ; x> 0 ; x-- )
          {
            q->rgbRed = ScaleQuantumToChar(p->red);
            q->rgbGreen = ScaleQuantumToChar(p->green);
            q->rgbBlue = ScaleQuantumToChar(p->blue);
            q->rgbReserved = 0;
            ++q;
            ++p;
          }
      }
#endif
    if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
        (QuantumTick(y,page.height) != MagickFalse))
      {
        status=image->progress_monitor(CropImageTag,y,page.height,
          image->client_data);
        if (status == MagickFalse)
          break;
      }
  }
  if (y < (long) page.height)
    {
      GlobalUnlock((HGLOBAL) bitmap_bitsH);
      GlobalFree((HGLOBAL) bitmap_bitsH);
      return((void *) NULL);
    }
  bitmap.bmBits = bitmap_bits;
  bitmapH = CreateBitmapIndirect( &bitmap );
  GlobalUnlock((HGLOBAL) bitmap_bitsH);
  return((void *) bitmapH);
}
Exemple #21
0
/******************************************************************************
 *              OleDuplicateData        [OLE32.@]
 *
 * Duplicates clipboard data.
 *
 * PARAMS
 *  hSrc     [I] Handle of the source clipboard data.
 *  cfFormat [I] The clipboard format of hSrc.
 *  uiFlags  [I] Flags to pass to GlobalAlloc.
 *
 * RETURNS
 *  Success: handle to the duplicated data.
 *  Failure: NULL.
 */
HANDLE WINAPI OleDuplicateData(HANDLE hSrc, CLIPFORMAT cfFormat,
	                          UINT uiFlags)
{
    HANDLE hDst = NULL;

    TRACE("(%p,%x,%x)\n", hSrc, cfFormat, uiFlags);

    if (!uiFlags) uiFlags = GMEM_MOVEABLE;

    switch (cfFormat)
    {
    case CF_ENHMETAFILE:
        hDst = CopyEnhMetaFileW(hSrc, NULL);
        break;
    case CF_METAFILEPICT:
        hDst = CopyMetaFileW(hSrc, NULL);
        break;
    case CF_PALETTE:
        {
            LOGPALETTE * logpalette;
            UINT nEntries = GetPaletteEntries(hSrc, 0, 0, NULL);
            if (!nEntries) return NULL;
            logpalette = HeapAlloc(GetProcessHeap(), 0,
                FIELD_OFFSET(LOGPALETTE, palPalEntry[nEntries]));
            if (!logpalette) return NULL;
            if (!GetPaletteEntries(hSrc, 0, nEntries, logpalette->palPalEntry))
            {
                HeapFree(GetProcessHeap(), 0, logpalette);
                return NULL;
            }
            logpalette->palVersion = 0x300;
            logpalette->palNumEntries = (WORD)nEntries;

            hDst = CreatePalette(logpalette);

            HeapFree(GetProcessHeap(), 0, logpalette);
            break;
        }
    case CF_BITMAP:
        {
            LONG size;
            BITMAP bm;
            if (!GetObjectW(hSrc, sizeof(bm), &bm))
                return NULL;
            size = GetBitmapBits(hSrc, 0, NULL);
            if (!size) return NULL;
            bm.bmBits = HeapAlloc(GetProcessHeap(), 0, size);
            if (!bm.bmBits) return NULL;
            if (GetBitmapBits(hSrc, size, bm.bmBits))
                hDst = CreateBitmapIndirect(&bm);
            HeapFree(GetProcessHeap(), 0, bm.bmBits);
            break;
        }
    default:
        {
            SIZE_T size = GlobalSize(hSrc);
            LPVOID pvSrc = NULL;
            LPVOID pvDst = NULL;

            /* allocate space for object */
            if (!size) return NULL;
            hDst = GlobalAlloc(uiFlags, size);
            if (!hDst) return NULL;

            /* lock pointers */
            pvSrc = GlobalLock(hSrc);
            if (!pvSrc)
            {
                GlobalFree(hDst);
                return NULL;
            }
            pvDst = GlobalLock(hDst);
            if (!pvDst)
            {
                GlobalUnlock(hSrc);
                GlobalFree(hDst);
                return NULL;
            }
            /* copy data */
            memcpy(pvDst, pvSrc, size);

            /* cleanup */
            GlobalUnlock(hDst);
            GlobalUnlock(hSrc);
        }
    }

    TRACE("returning %p\n", hDst);
    return hDst;
}