Exemplo n.º 1
0
HBITMAP     CreateThumbnailBmp( CAMRecPtr pcr, char * pBuf)

{
    HBITMAP             hBmp = 0;
    uint32_t            ctr;
    HDC                 hdc = 0;
    HPS                 hps = 0;
    char *              pErr = 0;
    BITMAPINFOHEADER2 * pbmih = 0;
    RGB2 *              pRGB;
    SIZEL               sizl;
    CAMJThumb           cjt;

do {
    // decode the JPEG data from the camera
    memset( &cjt, 0, sizeof(cjt));
    cjt.pSrc = pBuf;
    cjt.cbSrc = pcr->tnsize;
    ctr = JpgDecodeThumbnail( &cjt);
    if (ctr) {
        printf( "JpgDecodeThumbnail - rc= 0x%x\n", (int)ctr);
        break;
    }

    // allocate a BITMAPINFOHEADER2 / BITMAPINFO2
    // suitable for 24-bit color or 8-bit grayscale
    ctr = sizeof(BITMAPINFOHEADER2);
    if (cjt.cbPix == 1)
        ctr += 256 * sizeof(RGB2);
        
    pbmih = (BITMAPINFOHEADER2*)malloc( ctr);
    memset( pbmih, 0, ctr);

    // if grayscale, create a default colormap
    if (cjt.cbPix == 1)
        for (ctr = 0, pRGB = (RGB2*)&pbmih[1]; ctr < 256; ctr++, pRGB++) {
            pRGB->bBlue  = ctr;
            pRGB->bGreen = ctr;
            pRGB->bRed   = ctr;
        }

    // init the BMIH
    pbmih->cbFix         = sizeof(BITMAPINFOHEADER2);
    pbmih->cx            = cjt.cxDst;
    pbmih->cy            = cjt.cyDst;
    pbmih->cPlanes       = 1;
    pbmih->cBitCount     = cjt.cbPix * 8;
    pbmih->ulCompression = BCA_UNCOMP;
    pbmih->cbImage       = cjt.cbDst;

    // create a memory DC
    hdc = DevOpenDC( 0, OD_MEMORY, "*", 0L, 0, 0);
    if (!hdc) {
        pErr = "DevOpenDC";
        break;
    }

    // create a square PS using the longer side so we can
    // reuse the PS if we have to rotate the bitmap
    sizl.cx = ((pbmih->cx >= pbmih->cy) ? pbmih->cx : pbmih->cy);
    sizl.cy = sizl.cx;

    hps = GpiCreatePS( 0, hdc, &sizl, PU_PELS | GPIF_DEFAULT | GPIT_MICRO | GPIA_ASSOC);
    if (!hps) {
        pErr = "GpiCreatePS";
        break;
    }

    // create a bitmap from the data
    hBmp = GpiCreateBitmap( hps, pbmih, CBM_INIT, cjt.pDst, (BITMAPINFO2*)pbmih);
    if (!hBmp) {
        pErr = "GpiCreateBitmap";
        break;
    }

    // if the bitmap should be rotated, replace the original one
    if (STAT_GETROT(pcr->status)) {
        HBITMAP hBmpRot = RotateBitmap( hps, hBmp, pbmih,
                                        STAT_GETROT(pcr->status));
        if (hBmpRot) {
            GpiDeleteBitmap( hBmp);
            hBmp = hBmpRot;
        }
    }

} while (0);

    if (pErr)
        printf( "CreateThumbnailBmp - %s failed\n", pErr);

    if (hps) {
        GpiSetBitmap( hps, 0);
        GpiDestroyPS( hps);
    }
    if (hdc)
        DevCloseDC( hdc);
    if (pbmih)
        free( pbmih);
    if (cjt.pDst)
        free( cjt.pDst);

    return (hBmp);
}
Exemplo n.º 2
0
// ---
BOOL CTextRotator::RotateText( UINT uiAngle ) {
  Clear(); // Reset all preview memory DC

  m_uiAngle = ::NormalizeAngle( uiAngle );

  // Split text into a list of text lines (for multiline support)
  int             iLineCounter = 0;
  CPArray<TCHAR>  arrayText( 0, 1, false );

  TCHAR *psz = _tcstok(m_strText, _T("\n"));

  while (psz) {
    arrayText.Add( psz );
    iLineCounter++;
    psz = _tcstok(NULL, _T("\n"));
  }

  HDC hMemDC = ::CreateCompatibleDC( m_hSourceDC );

  if ( !hMemDC ) {
    return FALSE;
  }

  HGDIOBJ hOldFont		= ::SelectObject( hMemDC, m_hFont ); 

  // Calculate the size of the lenghtly text of the text list
  SIZE sizeText = GetLengthlyTextSize( hMemDC, &arrayText );

  // Set Full rectangle size
  CTRect rectFull;

  rectFull.left   = 0;
  rectFull.top    = 0;
  rectFull.right  = sizeText.cx;
  rectFull.bottom = sizeText.cy * iLineCounter;

  // Create an empty memory DC for rotation operation
  int iMax = rectFull.Width();

  if (iMax < rectFull.Height() ) 
		iMax = rectFull.Height();

  HBITMAP hEmptyBitmap1 = ::CreateCompatibleBitmap( m_hSourceDC, iMax, iMax );

  if ( !hEmptyBitmap1 ) 
		return FALSE;

  HBITMAP hEmptyBitmap2 = ::CreateCompatibleBitmap( m_hSourceDC, iMax, iMax );

  if (!hEmptyBitmap2) {
    ::DeleteObject( hEmptyBitmap1 );
		::SelectObject( hMemDC, hOldFont );
    return FALSE;
  }

  m_hRotatedMemDC = ::CreateCompatibleDC( m_hSourceDC );

  if ( !m_hRotatedMemDC ) {
		::SelectObject( hMemDC, hOldFont );
    ::DeleteObject( m_hRotatedMemDC );
    ::DeleteDC( hMemDC );
    ::DeleteObject( hEmptyBitmap2 );
    ::DeleteObject( hEmptyBitmap1 );
    return FALSE;
  }

  HGDIOBJ hOldBitmap1 = ::SelectObject( hMemDC, hEmptyBitmap1 ); // Put the empty bitmap in memory DC

	if ( m_hSourceBmp )
		::DeleteObject( ::SelectObject(m_hRotatedMemDC, m_hSourceBmp) ); // Delete temporary empty bitmap 2
  m_hSourceBmp = ::SelectObject( m_hRotatedMemDC, hEmptyBitmap2 ); // Put the empty bitmap in rotated memory DC

  ::SetBkMode( hMemDC, TRANSPARENT );

  HBRUSH hBrush = ::CreateSolidBrush( m_clrBackground );

  HGDIOBJ hOldBrush = ::SelectObject( hMemDC, hBrush );
  
  ::FillRect( hMemDC, &rectFull, hBrush );

  ::FillRect( m_hRotatedMemDC, &rectFull, hBrush );

  ::SetTextColor(hMemDC, m_clrText);

  // Draw the list of text into memory DC
  for (int i=0,c=arrayText.Count(); i<c; i++) {
    RECT stRect;

    stRect.left   = 0;
    stRect.top    = i * sizeText.cy;
    stRect.right  = stRect.left + sizeText.cx;
    stRect.bottom = stRect.top  + sizeText.cy;

    if ( m_bDisabled ) {
      ::SetTextColor(hMemDC, GetSysColor(COLOR_3DHIGHLIGHT));

      RECT rect = stRect;

      rect.left   += 1;
      rect.top    += 1;
      rect.right  += 1;
      rect.bottom += 1;

      ::DrawText(hMemDC, arrayText[i], -1, &rect, DT_SINGLELINE | DT_VCENTER | m_uiHorzAlignment);

      ::SetTextColor(hMemDC, GetSysColor(COLOR_3DSHADOW));
     }

    ::DrawText(hMemDC, arrayText[i], -1, &stRect, DT_SINGLELINE | DT_VCENTER | m_uiHorzAlignment);
  }

	RotateBitmap( uiAngle, m_clrBackground, rectFull.Height(), rectFull.Width(), hMemDC, m_hRotatedMemDC );

  ::DeleteObject( ::SelectObject(hMemDC, hOldBitmap1) ); // Delete temporary empty bitmap 1
  ::DeleteObject( ::SelectObject(hMemDC, hOldBrush) );
  ::SelectObject( hMemDC, hOldFont );
  ::DeleteDC( hMemDC );

  if (uiAngle == 90 || uiAngle == 270) {
    m_rectRotated.left   = 0;
    m_rectRotated.top    = 0;
    m_rectRotated.right  = rectFull.Height();
    m_rectRotated.bottom = rectFull.Width();
  }
  else {
    m_rectRotated.left   = 0;
    m_rectRotated.top    = 0;
    m_rectRotated.right  = rectFull.Width();
    m_rectRotated.bottom = rectFull.Height();
  }

  return TRUE;
}