1 // ximage.cpp : main implementation file
\r
2 /* 07/08/2001 v1.00 - Davide Pizzolato - www.xdp.it
\r
3 * CxImage version 6.0.0 02/Feb/2008
\r
8 ////////////////////////////////////////////////////////////////////////////////
\r
10 ////////////////////////////////////////////////////////////////////////////////
\r
12 * Initialize the internal structures
\r
14 void CxImage::Startup(DWORD imagetype)
\r
17 pDib = pSelection = pAlpha = NULL;
\r
18 ppLayers = ppFrames = NULL;
\r
20 memset(&head,0,sizeof(BITMAPINFOHEADER));
\r
21 memset(&info,0,sizeof(CXIMAGEINFO));
\r
22 //init default attributes
\r
23 info.dwType = imagetype;
\r
24 info.fQuality = 90.0f;
\r
25 info.nAlphaMax = 255;
\r
26 info.nBkgndIndex = -1;
\r
27 info.bEnabled = true;
\r
28 SetXDPI(CXIMAGE_DEFAULT_DPI);
\r
29 SetYDPI(CXIMAGE_DEFAULT_DPI);
\r
32 info.bLittleEndianHost = (*((char *) &test) == 1);
\r
34 ////////////////////////////////////////////////////////////////////////////////
\r
36 * Empty image constructor
\r
37 * \param imagetype: (optional) set the image format, see ENUM_CXIMAGE_FORMATS
\r
39 CxImage::CxImage(DWORD imagetype)
\r
43 ////////////////////////////////////////////////////////////////////////////////
\r
45 * Call this function to destroy image pixels, alpha channel, selection and sub layers.
\r
46 * - Attributes are not erased, but IsValid returns false.
\r
48 * \return true if everything is freed, false if the image is a Ghost
\r
50 bool CxImage::Destroy()
\r
52 //free this only if it's valid and it's not a ghost
\r
53 if (info.pGhost==NULL){
\r
55 for(long n=0; n<info.nNumLayers;n++){ delete ppLayers[n]; }
\r
56 delete [] ppLayers; ppLayers=0; info.nNumLayers = 0;
\r
58 if (pSelection) {free(pSelection); pSelection=0;}
\r
59 if (pAlpha) {free(pAlpha); pAlpha=0;}
\r
60 if (pDib) {free(pDib); pDib=0;}
\r
65 ////////////////////////////////////////////////////////////////////////////////
\r
66 bool CxImage::DestroyFrames()
\r
68 if (info.pGhost==NULL) {
\r
70 for (long n=0; n<info.nNumFrames; n++) { delete ppFrames[n]; }
\r
71 delete [] ppFrames; ppFrames = NULL; info.nNumFrames = 0;
\r
77 ////////////////////////////////////////////////////////////////////////////////
\r
79 * Sized image constructor
\r
80 * \param dwWidth: width
\r
81 * \param dwHeight: height
\r
82 * \param wBpp: bit per pixel, can be 1, 4, 8, 24
\r
83 * \param imagetype: (optional) set the image format, see ENUM_CXIMAGE_FORMATS
\r
85 CxImage::CxImage(DWORD dwWidth, DWORD dwHeight, DWORD wBpp, DWORD imagetype)
\r
88 Create(dwWidth,dwHeight,wBpp,imagetype);
\r
90 ////////////////////////////////////////////////////////////////////////////////
\r
92 * image constructor from existing source
\r
93 * \param src: source image.
\r
94 * \param copypixels: copy the pixels from the source image into the new image.
\r
95 * \param copyselection: copy the selection from source
\r
96 * \param copyalpha: copy the alpha channel from source
\r
99 CxImage::CxImage(const CxImage &src, bool copypixels, bool copyselection, bool copyalpha)
\r
101 Startup(src.GetType());
\r
102 Copy(src,copypixels,copyselection,copyalpha);
\r
104 ////////////////////////////////////////////////////////////////////////////////
\r
106 * Copies the image from an exsisting source
\r
107 * \param src: source image.
\r
108 * \param copypixels: copy the pixels from the source image into the new image.
\r
109 * \param copyselection: copy the selection from source
\r
110 * \param copyalpha: copy the alpha channel from source
\r
112 void CxImage::Copy(const CxImage &src, bool copypixels, bool copyselection, bool copyalpha)
\r
114 // if the source is a ghost, the copy is still a ghost
\r
115 if (src.info.pGhost){
\r
119 //copy the attributes
\r
120 memcpy(&info,&src.info,sizeof(CXIMAGEINFO));
\r
121 memcpy(&head,&src.head,sizeof(BITMAPINFOHEADER)); // [andy] - fix for bitmap header DPI
\r
122 //rebuild the image
\r
123 Create(src.GetWidth(),src.GetHeight(),src.GetBpp(),src.GetType());
\r
124 //copy the pixels and the palette, or at least copy the palette only.
\r
125 if (copypixels && pDib && src.pDib) memcpy(pDib,src.pDib,GetSize());
\r
126 else SetPalette(src.GetPalette());
\r
127 long nSize = head.biWidth * head.biHeight;
\r
128 //copy the selection
\r
129 if (copyselection && src.pSelection){
\r
130 if (pSelection) free(pSelection);
\r
131 pSelection = (BYTE*)malloc(nSize);
\r
132 memcpy(pSelection,src.pSelection,nSize);
\r
134 //copy the alpha channel
\r
135 if (copyalpha && src.pAlpha){
\r
136 if (pAlpha) free(pAlpha);
\r
137 pAlpha = (BYTE*)malloc(nSize);
\r
138 memcpy(pAlpha,src.pAlpha,nSize);
\r
141 ////////////////////////////////////////////////////////////////////////////////
\r
143 * Copies the image attributes from an existing image.
\r
144 * - Works only on an empty image, and the image will be still empty.
\r
145 * - <b> Use it before Create() </b>
\r
147 void CxImage::CopyInfo(const CxImage &src)
\r
149 if (pDib==NULL) memcpy(&info,&src.info,sizeof(CXIMAGEINFO));
\r
151 ////////////////////////////////////////////////////////////////////////////////
\r
155 CxImage& CxImage::operator = (const CxImage& isrc)
\r
157 if (this != &isrc) Copy(isrc);
\r
160 ////////////////////////////////////////////////////////////////////////////////
\r
162 * Initializes or rebuilds the image.
\r
163 * \param dwWidth: width
\r
164 * \param dwHeight: height
\r
165 * \param wBpp: bit per pixel, can be 1, 4, 8, 24
\r
166 * \param imagetype: (optional) set the image format, see ENUM_CXIMAGE_FORMATS
\r
167 * \return pointer to the internal pDib object; NULL if an error occurs.
\r
169 void* CxImage::Create(DWORD dwWidth, DWORD dwHeight, DWORD wBpp, DWORD imagetype)
\r
171 // destroy the existing image (if any)
\r
175 // prevent further actions if width or height are not vaild <Balabasnia>
\r
176 if ((dwWidth == 0) || (dwHeight == 0)){
\r
177 strcpy(info.szLastError,"CxImage::Create : width and height must be greater than zero");
\r
181 // Make sure bits per pixel is valid
\r
182 if (wBpp <= 1) wBpp = 1;
\r
183 else if (wBpp <= 4) wBpp = 4;
\r
184 else if (wBpp <= 8) wBpp = 8;
\r
187 // limit memory requirements (and also a check for bad parameters)
\r
188 if (((dwWidth*dwHeight*wBpp)>>3) > CXIMAGE_MAX_MEMORY ||
\r
189 ((dwWidth*dwHeight*wBpp)/wBpp) != (dwWidth*dwHeight))
\r
191 strcpy(info.szLastError,"CXIMAGE_MAX_MEMORY exceeded");
\r
195 // set the correct bpp value
\r
198 head.biClrUsed = 2; break;
\r
200 head.biClrUsed = 16; break;
\r
202 head.biClrUsed = 256; break;
\r
204 head.biClrUsed = 0;
\r
207 //set the common image informations
\r
208 info.dwEffWidth = ((((wBpp * dwWidth) + 31) / 32) * 4);
\r
209 info.dwType = imagetype;
\r
211 // initialize BITMAPINFOHEADER
\r
212 head.biSize = sizeof(BITMAPINFOHEADER); //<ralphw>
\r
213 head.biWidth = dwWidth; // fill in width from parameter
\r
214 head.biHeight = dwHeight; // fill in height from parameter
\r
215 head.biPlanes = 1; // must be 1
\r
216 head.biBitCount = (WORD)wBpp; // from parameter
\r
217 head.biCompression = BI_RGB;
\r
218 head.biSizeImage = info.dwEffWidth * dwHeight;
\r
219 // head.biXPelsPerMeter = 0; See SetXDPI
\r
220 // head.biYPelsPerMeter = 0; See SetYDPI
\r
221 // head.biClrImportant = 0; See SetClrImportant
\r
223 pDib = malloc(GetSize()); // alloc memory block to store our bitmap
\r
225 strcpy(info.szLastError,"CxImage::Create can't allocate memory");
\r
229 //clear the palette
\r
230 RGBQUAD* pal=GetPalette();
\r
231 if (pal) memset(pal,0,GetPaletteSize());
\r
232 //Destroy the existing selection
\r
233 #if CXIMAGE_SUPPORT_SELECTION
\r
234 if (pSelection) SelectionDelete();
\r
235 #endif //CXIMAGE_SUPPORT_SELECTION
\r
236 //Destroy the existing alpha channel
\r
237 #if CXIMAGE_SUPPORT_ALPHA
\r
238 if (pAlpha) AlphaDelete();
\r
239 #endif //CXIMAGE_SUPPORT_ALPHA
\r
241 // use our bitmap info structure to fill in first part of
\r
242 // our DIB with the BITMAPINFOHEADER
\r
243 BITMAPINFOHEADER* lpbi;
\r
244 lpbi = (BITMAPINFOHEADER*)(pDib);
\r
247 info.pImage=GetBits();
\r
249 return pDib; //return handle to the DIB
\r
251 ////////////////////////////////////////////////////////////////////////////////
\r
253 * \return pointer to the image pixels. <b> USE CAREFULLY </b>
\r
255 BYTE* CxImage::GetBits(DWORD row)
\r
259 if (row<(DWORD)head.biHeight){
\r
260 return ((BYTE*)pDib + *(DWORD*)pDib + GetPaletteSize() + (info.dwEffWidth * row));
\r
265 return ((BYTE*)pDib + *(DWORD*)pDib + GetPaletteSize());
\r
270 ////////////////////////////////////////////////////////////////////////////////
\r
272 * \return the size in bytes of the internal pDib object
\r
274 long CxImage::GetSize()
\r
276 return head.biSize + head.biSizeImage + GetPaletteSize();
\r
278 ////////////////////////////////////////////////////////////////////////////////
\r
280 * Checks if the coordinates are inside the image
\r
281 * \return true if x and y are both inside the image
\r
283 bool CxImage::IsInside(long x, long y)
\r
285 return (0<=y && y<head.biHeight && 0<=x && x<head.biWidth);
\r
287 ////////////////////////////////////////////////////////////////////////////////
\r
289 * Sets the image bits to the specified value
\r
290 * - for indexed images, the output color is set by the palette entries.
\r
291 * - for RGB images, the output color is a shade of gray.
\r
293 void CxImage::Clear(BYTE bval)
\r
295 if (pDib == 0) return;
\r
297 if (GetBpp() == 1){
\r
298 if (bval > 0) bval = 255;
\r
300 if (GetBpp() == 4){
\r
301 bval = (BYTE)(17*(0x0F & bval));
\r
304 memset(info.pImage,bval,head.biSizeImage);
\r
306 ////////////////////////////////////////////////////////////////////////////////
\r
308 * Transfers the image from an existing source image. The source becomes empty.
\r
309 * \return true if everything is ok
\r
311 bool CxImage::Transfer(CxImage &from, bool bTransferFrames /*=true*/)
\r
316 memcpy(&head,&from.head,sizeof(BITMAPINFOHEADER));
\r
317 memcpy(&info,&from.info,sizeof(CXIMAGEINFO));
\r
320 pSelection = from.pSelection;
\r
321 pAlpha = from.pAlpha;
\r
322 ppLayers = from.ppLayers;
\r
324 memset(&from.head,0,sizeof(BITMAPINFOHEADER));
\r
325 memset(&from.info,0,sizeof(CXIMAGEINFO));
\r
326 from.pDib = from.pSelection = from.pAlpha = NULL;
\r
327 from.ppLayers = NULL;
\r
329 if (bTransferFrames){
\r
331 ppFrames = from.ppFrames;
\r
332 from.ppFrames = NULL;
\r
337 ////////////////////////////////////////////////////////////////////////////////
\r
339 * (this) points to the same pDib owned by (*from), the image remains in (*from)
\r
340 * but (this) has the access to the pixels. <b>Use carefully !!!</b>
\r
342 void CxImage::Ghost(const CxImage *from)
\r
345 memcpy(&head,&from->head,sizeof(BITMAPINFOHEADER));
\r
346 memcpy(&info,&from->info,sizeof(CXIMAGEINFO));
\r
348 pSelection = from->pSelection;
\r
349 pAlpha = from->pAlpha;
\r
350 ppLayers = from->ppLayers;
\r
351 ppFrames = from->ppFrames;
\r
352 info.pGhost=(CxImage *)from;
\r
355 ////////////////////////////////////////////////////////////////////////////////
\r
357 * turns a 16 or 32 bit bitfield image into a RGB image
\r
359 void CxImage::Bitfield2RGB(BYTE *src, DWORD redmask, DWORD greenmask, DWORD bluemask, BYTE bpp)
\r
364 DWORD ns[3]={0,0,0};
\r
365 // compute the number of shift for each mask
\r
366 for (int i=0;i<16;i++){
\r
367 if ((redmask>>i)&0x01) ns[0]++;
\r
368 if ((greenmask>>i)&0x01) ns[1]++;
\r
369 if ((bluemask>>i)&0x01) ns[2]++;
\r
371 ns[1]+=ns[0]; ns[2]+=ns[1]; ns[0]=8-ns[0]; ns[1]-=8; ns[2]-=8;
\r
372 // dword aligned width for 16 bit image
\r
373 long effwidth2=(((head.biWidth + 1) / 2) * 4);
\r
376 BYTE *p=info.pImage;
\r
377 // scan the buffer in reverse direction to avoid reallocations
\r
378 for (long y=head.biHeight-1; y>=0; y--){
\r
380 y3=info.dwEffWidth*y;
\r
381 for (long x=head.biWidth-1; x>=0; x--){
\r
384 w = (WORD)(src[x2]+256*src[1+x2]);
\r
385 p[ x3]=(BYTE)((w & bluemask)<<ns[0]);
\r
386 p[1+x3]=(BYTE)((w & greenmask)>>ns[1]);
\r
387 p[2+x3]=(BYTE)((w & redmask)>>ns[2]);
\r
394 DWORD ns[3]={0,0,0};
\r
395 // compute the number of shift for each mask
\r
396 for (int i=8;i<32;i+=8){
\r
397 if (redmask>>i) ns[0]++;
\r
398 if (greenmask>>i) ns[1]++;
\r
399 if (bluemask>>i) ns[2]++;
\r
401 // dword aligned width for 32 bit image
\r
402 long effwidth4 = head.biWidth * 4;
\r
404 BYTE *p=info.pImage;
\r
405 // scan the buffer in reverse direction to avoid reallocations
\r
406 for (long y=head.biHeight-1; y>=0; y--){
\r
408 y3=info.dwEffWidth*y;
\r
409 for (long x=head.biWidth-1; x>=0; x--){
\r
412 p[ x3]=src[ns[2]+x4];
\r
413 p[1+x3]=src[ns[1]+x4];
\r
414 p[2+x3]=src[ns[0]+x4];
\r
422 ////////////////////////////////////////////////////////////////////////////////
\r
424 * Creates an image from a generic buffer
\r
425 * \param pArray: source memory buffer
\r
426 * \param dwWidth: image width
\r
427 * \param dwHeight: image height
\r
428 * \param dwBitsperpixel: can be 1,4,8,24,32
\r
429 * \param dwBytesperline: line alignment, in bytes, for a single row stored in pArray
\r
430 * \param bFlipImage: tune this parameter if the image is upsidedown
\r
431 * \return true if everything is ok
\r
433 bool CxImage::CreateFromArray(BYTE* pArray,DWORD dwWidth,DWORD dwHeight,DWORD dwBitsperpixel, DWORD dwBytesperline, bool bFlipImage)
\r
435 if (pArray==NULL) return false;
\r
436 if (!((dwBitsperpixel==1)||(dwBitsperpixel==4)||(dwBitsperpixel==8)||
\r
437 (dwBitsperpixel==24)||(dwBitsperpixel==32))) return false;
\r
439 if (!Create(dwWidth,dwHeight,dwBitsperpixel)) return false;
\r
441 if (dwBitsperpixel<24) SetGrayPalette();
\r
443 #if CXIMAGE_SUPPORT_ALPHA
\r
444 if (dwBitsperpixel==32) AlphaCreate();
\r
445 #endif //CXIMAGE_SUPPORT_ALPHA
\r
449 for (DWORD y = 0; y<dwHeight; y++) {
\r
450 dst = info.pImage + (bFlipImage?(dwHeight-1-y):y) * info.dwEffWidth;
\r
451 src = pArray + y * dwBytesperline;
\r
452 if (dwBitsperpixel==32){
\r
453 for(DWORD x=0;x<dwWidth;x++){
\r
457 #if CXIMAGE_SUPPORT_ALPHA
\r
458 AlphaSet(x,(bFlipImage?(dwHeight-1-y):y),src[3]);
\r
459 #endif //CXIMAGE_SUPPORT_ALPHA
\r
463 memcpy(dst,src,min(info.dwEffWidth,dwBytesperline));
\r
468 ////////////////////////////////////////////////////////////////////////////////
\r
470 * \sa CreateFromArray
\r
472 bool CxImage::CreateFromMatrix(BYTE** ppMatrix,DWORD dwWidth,DWORD dwHeight,DWORD dwBitsperpixel, DWORD dwBytesperline, bool bFlipImage)
\r
474 if (ppMatrix==NULL) return false;
\r
475 if (!((dwBitsperpixel==1)||(dwBitsperpixel==4)||(dwBitsperpixel==8)||
\r
476 (dwBitsperpixel==24)||(dwBitsperpixel==32))) return false;
\r
478 if (!Create(dwWidth,dwHeight,dwBitsperpixel)) return false;
\r
480 if (dwBitsperpixel<24) SetGrayPalette();
\r
482 #if CXIMAGE_SUPPORT_ALPHA
\r
483 if (dwBitsperpixel==32) AlphaCreate();
\r
484 #endif //CXIMAGE_SUPPORT_ALPHA
\r
488 for (DWORD y = 0; y<dwHeight; y++) {
\r
489 dst = info.pImage + (bFlipImage?(dwHeight-1-y):y) * info.dwEffWidth;
\r
492 if (dwBitsperpixel==32){
\r
493 for(DWORD x=0;x<dwWidth;x++){
\r
497 #if CXIMAGE_SUPPORT_ALPHA
\r
498 AlphaSet(x,(bFlipImage?(dwHeight-1-y):y),src[3]);
\r
499 #endif //CXIMAGE_SUPPORT_ALPHA
\r
503 memcpy(dst,src,min(info.dwEffWidth,dwBytesperline));
\r
509 ////////////////////////////////////////////////////////////////////////////////
\r
511 * \return lightness difference between elem1 and elem2
\r
513 int CxImage::CompareColors(const void *elem1, const void *elem2)
\r
515 RGBQUAD* c1 = (RGBQUAD*)elem1;
\r
516 RGBQUAD* c2 = (RGBQUAD*)elem2;
\r
518 int g1 = (int)RGB2GRAY(c1->rgbRed,c1->rgbGreen,c1->rgbBlue);
\r
519 int g2 = (int)RGB2GRAY(c2->rgbRed,c2->rgbGreen,c2->rgbBlue);
\r
523 ////////////////////////////////////////////////////////////////////////////////
\r
525 * simply calls "if (memblock) free(memblock);".
\r
526 * Useful when calling Encode for a memory buffer,
\r
527 * from a DLL compiled with different memory management options.
\r
528 * CxImage::FreeMemory will use the same memory environment used by Encode.
\r
531 void CxImage::FreeMemory(void* memblock)
\r
536 ////////////////////////////////////////////////////////////////////////////////
\r