1 // xImaSel.cpp : Selection functions
2 /* 07/08/2001 v1.00 - Davide Pizzolato - www.xdp.it
3 * CxImage version 6.0.0 02/Feb/2008
8 #if CXIMAGE_SUPPORT_SELECTION
10 ////////////////////////////////////////////////////////////////////////////////
12 * Checks if the image has a valid selection.
14 bool CxImage::SelectionIsValid()
18 ////////////////////////////////////////////////////////////////////////////////
20 * Gets the smallest rectangle that contains the selection
22 void CxImage::SelectionGetBox(RECT& r)
24 memcpy(&r,&info.rSelectionBox,sizeof(RECT));
26 ////////////////////////////////////////////////////////////////////////////////
28 * Empties the selection.
30 bool CxImage::SelectionClear(BYTE level)
34 memset(pSelection,0,head.biWidth * head.biHeight);
35 info.rSelectionBox.left = head.biWidth;
36 info.rSelectionBox.bottom = head.biHeight;
37 info.rSelectionBox.right = info.rSelectionBox.top = 0;
39 memset(pSelection,level,head.biWidth * head.biHeight);
40 info.rSelectionBox.right = head.biWidth;
41 info.rSelectionBox.top = head.biHeight;
42 info.rSelectionBox.left = info.rSelectionBox.bottom = 0;
48 ////////////////////////////////////////////////////////////////////////////////
50 * Allocates an empty selection.
52 bool CxImage::SelectionCreate()
55 pSelection = (BYTE*)calloc(head.biWidth * head.biHeight, 1);
56 return (pSelection!=0);
58 ////////////////////////////////////////////////////////////////////////////////
60 * Deallocates the selction.
62 bool CxImage::SelectionDelete()
68 info.rSelectionBox.left = head.biWidth;
69 info.rSelectionBox.bottom = head.biHeight;
70 info.rSelectionBox.right = info.rSelectionBox.top = 0;
73 ////////////////////////////////////////////////////////////////////////////////
75 * Checks if the coordinates are inside the selection.
77 bool CxImage::SelectionIsInside(long x, long y)
80 if (pSelection==NULL) return true;
81 return pSelection[x+y*head.biWidth]!=0;
85 ////////////////////////////////////////////////////////////////////////////////
87 * Checks if the coordinates are inside the selection.
88 * "blind" version assumes that (x,y) is inside to the image.
90 bool CxImage::BlindSelectionIsInside(long x, long y)
94 #if CXIMAGE_SUPPORT_EXCEPTION_HANDLING
100 if (pSelection==NULL) return true;
101 return pSelection[x+y*head.biWidth]!=0;
103 ////////////////////////////////////////////////////////////////////////////////
105 * Adds a rectangle to the existing selection.
107 bool CxImage::SelectionAddRect(RECT r, BYTE level)
109 if (pSelection==NULL) SelectionCreate();
110 if (pSelection==NULL) return false;
113 if (r.left<r.right) {r2.left=r.left; r2.right=r.right; } else {r2.left=r.right ; r2.right=r.left; }
114 if (r.bottom<r.top) {r2.bottom=r.bottom; r2.top=r.top; } else {r2.bottom=r.top ; r2.top=r.bottom; }
116 if (info.rSelectionBox.top <= r2.top) info.rSelectionBox.top = __max(0L,__min(head.biHeight,r2.top+1));
117 if (info.rSelectionBox.left > r2.left) info.rSelectionBox.left = __max(0L,__min(head.biWidth,r2.left));
118 if (info.rSelectionBox.right <= r2.right) info.rSelectionBox.right = __max(0L,__min(head.biWidth,r2.right+1));
119 if (info.rSelectionBox.bottom > r2.bottom) info.rSelectionBox.bottom = __max(0L,__min(head.biHeight,r2.bottom));
121 long ymin = __max(0L,__min(head.biHeight,r2.bottom));
122 long ymax = __max(0L,__min(head.biHeight,r2.top+1));
123 long xmin = __max(0L,__min(head.biWidth,r2.left));
124 long xmax = __max(0L,__min(head.biWidth,r2.right+1));
126 for (long y=ymin; y<ymax; y++)
127 memset(pSelection + xmin + y * head.biWidth, level, xmax-xmin);
131 ////////////////////////////////////////////////////////////////////////////////
133 * Adds an ellipse to the existing selection.
135 bool CxImage::SelectionAddEllipse(RECT r, BYTE level)
137 if (pSelection==NULL) SelectionCreate();
138 if (pSelection==NULL) return false;
140 long xradius = labs(r.right - r.left)/2;
141 long yradius = labs(r.top - r.bottom)/2;
142 if (xradius==0 || yradius==0) return false;
144 long xcenter = (r.right + r.left)/2;
145 long ycenter = (r.top + r.bottom)/2;
147 if (info.rSelectionBox.left > (xcenter - xradius)) info.rSelectionBox.left = __max(0L,__min(head.biWidth,(xcenter - xradius)));
148 if (info.rSelectionBox.right <= (xcenter + xradius)) info.rSelectionBox.right = __max(0L,__min(head.biWidth,(xcenter + xradius + 1)));
149 if (info.rSelectionBox.bottom > (ycenter - yradius)) info.rSelectionBox.bottom = __max(0L,__min(head.biHeight,(ycenter - yradius)));
150 if (info.rSelectionBox.top <= (ycenter + yradius)) info.rSelectionBox.top = __max(0L,__min(head.biHeight,(ycenter + yradius + 1)));
152 long xmin = __max(0L,__min(head.biWidth,xcenter - xradius));
153 long xmax = __max(0L,__min(head.biWidth,xcenter + xradius + 1));
154 long ymin = __max(0L,__min(head.biHeight,ycenter - yradius));
155 long ymax = __max(0L,__min(head.biHeight,ycenter + yradius + 1));
158 for (y=ymin; y<__min(ycenter,ymax); y++){
159 for (long x=xmin; x<xmax; x++){
160 yo = (long)(ycenter - yradius * sqrt(1-pow((float)(x - xcenter)/(float)xradius,2)));
161 if (yo<y) pSelection[x + y * head.biWidth] = level;
164 for (y=ycenter; y<ymax; y++){
165 for (long x=xmin; x<xmax; x++){
166 yo = (long)(ycenter + yradius * sqrt(1-pow((float)(x - xcenter)/(float)xradius,2)));
167 if (yo>y) pSelection[x + y * head.biWidth] = level;
172 ////////////////////////////////////////////////////////////////////////////////
174 * Inverts the selection.
175 * Note: the SelectionBox is set to "full image", call SelectionGetBox before (if necessary)
177 bool CxImage::SelectionInvert()
180 BYTE *iSrc=pSelection;
181 long n=head.biHeight*head.biWidth;
182 for(long i=0; i < n; i++){
183 *iSrc=(BYTE)~(*(iSrc));
187 SelectionRebuildBox();
193 ////////////////////////////////////////////////////////////////////////////////
195 * Imports an existing region from another image with the same width and height.
197 bool CxImage::SelectionCopy(CxImage &from)
199 if (from.pSelection == NULL || head.biWidth != from.head.biWidth || head.biHeight != from.head.biHeight) return false;
200 if (pSelection==NULL) pSelection = (BYTE*)malloc(head.biWidth * head.biHeight);
201 if (pSelection==NULL) return false;
202 memcpy(pSelection,from.pSelection,head.biWidth * head.biHeight);
203 memcpy(&info.rSelectionBox,&from.info.rSelectionBox,sizeof(RECT));
206 ////////////////////////////////////////////////////////////////////////////////
208 * Adds a polygonal region to the existing selection. points points to an array of POINT structures.
209 * Each structure specifies the x-coordinate and y-coordinate of one vertex of the polygon.
210 * npoints specifies the number of POINT structures in the array pointed to by points.
212 bool CxImage::SelectionAddPolygon(POINT *points, long npoints, BYTE level)
214 if (points==NULL || npoints<3) return false;
216 if (pSelection==NULL) SelectionCreate();
217 if (pSelection==NULL) return false;
219 BYTE* plocal = (BYTE*)calloc(head.biWidth*head.biHeight, 1);
220 RECT localbox = {head.biWidth,0,0,head.biHeight};
228 current = &points[i];
230 if (i==0 || (i>0 && points[i-1].x==-1)) start = &points[i];
232 if ((i+1)==npoints || points[i+1].x==-1)
238 if (current->x != next->x){
239 beta = (float)(next->y - current->y)/(float)(next->x - current->x);
240 if (current->x < next->x){
241 for (x=current->x; x<=next->x; x++){
242 y = (long)(current->y + (x - current->x) * beta);
243 if (IsInside(x,y)) plocal[x + y * head.biWidth] = 255;
246 for (x=current->x; x>=next->x; x--){
247 y = (long)(current->y + (x - current->x) * beta);
248 if (IsInside(x,y)) plocal[x + y * head.biWidth] = 255;
252 if (current->y != next->y){
253 beta = (float)(next->x - current->x)/(float)(next->y - current->y);
254 if (current->y < next->y){
255 for (y=current->y; y<=next->y; y++){
256 x = (long)(current->x + (y - current->y) * beta);
257 if (IsInside(x,y)) plocal[x + y * head.biWidth] = 255;
260 for (y=current->y; y>=next->y; y--){
261 x = (long)(current->x + (y - current->y) * beta);
262 if (IsInside(x,y)) plocal[x + y * head.biWidth] = 255;
269 if (current->x < next->x) {r2.left=current->x; r2.right=next->x; } else {r2.left=next->x ; r2.right=current->x; }
270 if (current->y < next->y) {r2.bottom=current->y; r2.top=next->y; } else {r2.bottom=next->y ; r2.top=current->y; }
271 if (localbox.top < r2.top) localbox.top = __max(0L,__min(head.biHeight-1,r2.top+1));
272 if (localbox.left > r2.left) localbox.left = __max(0L,__min(head.biWidth-1,r2.left-1));
273 if (localbox.right < r2.right) localbox.right = __max(0L,__min(head.biWidth-1,r2.right+1));
274 if (localbox.bottom > r2.bottom) localbox.bottom = __max(0L,__min(head.biHeight-1,r2.bottom-1));
279 //fill the outer region
280 long npix=(localbox.right - localbox.left)*(localbox.top - localbox.bottom);
281 POINT* pix = (POINT*)calloc(npix,sizeof(POINT));
283 long fx, fy, fxx, fyy, first, last;
289 for (int side=0; side<4; side++){
292 xmin=localbox.left; xmax=localbox.right+1; ymin=localbox.bottom; ymax=localbox.bottom+1;
295 xmin=localbox.right; xmax=localbox.right+1; ymin=localbox.bottom; ymax=localbox.top+1;
298 xmin=localbox.left; xmax=localbox.right+1; ymin=localbox.top; ymax=localbox.top+1;
301 xmin=localbox.left; xmax=localbox.left+1; ymin=localbox.bottom; ymax=localbox.top+1;
304 //fill from the border points
305 for(y=ymin;y<ymax;y++){
306 for(x=xmin;x<xmax;x++){
307 if (plocal[x+y*head.biWidth]==0){
308 // Subject: FLOOD FILL ROUTINE Date: 12-23-97 (00:57)
309 // Author: Petter Holmberg Code: QB, QBasic, PDS
310 // Origin: petter.holmberg@usa.net Packet: GRAPHICS.ABC
320 if ((plocal[fxx + fyy*head.biWidth] == back) &&
321 fxx>=localbox.left && fxx<=localbox.right && fyy>=localbox.bottom && fyy<=localbox.top )
323 plocal[fxx + fyy*head.biWidth] = mark;
324 if (fyy > 0 && plocal[fxx + (fyy - 1)*head.biWidth] == back){
326 pix[last].y = fy - 1;
328 if (last == npix) last = 0;
330 if ((fyy + 1)<head.biHeight && plocal[fxx + (fyy + 1)*head.biWidth] == back){
332 pix[last].y = fy + 1;
334 if (last == npix) last = 0;
343 fx = pix[first].x - 1;
350 if ((plocal[fxx + fyy*head.biWidth] == back) &&
351 fxx>=localbox.left && fxx<=localbox.right && fyy>=localbox.bottom && fyy<=localbox.top )
353 plocal[fxx + (y + fy)*head.biWidth] = mark;
354 if (fyy > 0 && plocal[fxx + (fyy - 1)*head.biWidth] == back){
356 pix[last].y = fy - 1;
358 if (last == npix) last = 0;
360 if ((fyy + 1)<head.biHeight && plocal[fxx + (fyy + 1)*head.biWidth] == back){
362 pix[last].y = fy + 1;
364 if (last == npix) last = 0;
374 if (first == npix) first = 0;
381 //transfer the region
383 for (y=localbox.bottom; y<=localbox.top; y++){
384 yoffset = y * head.biWidth;
385 for (x=localbox.left; x<=localbox.right; x++)
386 if (plocal[x + yoffset]!=1) pSelection[x + yoffset]=level;
388 if (info.rSelectionBox.top <= localbox.top) info.rSelectionBox.top = __min(head.biHeight,localbox.top + 1);
389 if (info.rSelectionBox.left > localbox.left) info.rSelectionBox.left = __min(head.biWidth,localbox.left);
390 if (info.rSelectionBox.right <= localbox.right) info.rSelectionBox.right = __min(head.biWidth,localbox.right + 1);
391 if (info.rSelectionBox.bottom > localbox.bottom) info.rSelectionBox.bottom = __min(head.biHeight,localbox.bottom);
398 ////////////////////////////////////////////////////////////////////////////////
400 * Adds to the selection all the pixels matching the specified color.
402 bool CxImage::SelectionAddColor(RGBQUAD c, BYTE level)
404 if (pSelection==NULL) SelectionCreate();
405 if (pSelection==NULL) return false;
407 RECT localbox = {head.biWidth,0,0,head.biHeight};
409 for (long y = 0; y < head.biHeight; y++){
410 for (long x = 0; x < head.biWidth; x++){
411 RGBQUAD color = BlindGetPixelColor(x, y);
412 if (color.rgbRed == c.rgbRed &&
413 color.rgbGreen == c.rgbGreen &&
414 color.rgbBlue == c.rgbBlue)
416 pSelection[x + y * head.biWidth] = level;
418 if (localbox.top < y) localbox.top = y;
419 if (localbox.left > x) localbox.left = x;
420 if (localbox.right < x) localbox.right = x;
421 if (localbox.bottom > y) localbox.bottom = y;
426 if (info.rSelectionBox.top <= localbox.top) info.rSelectionBox.top = localbox.top + 1;
427 if (info.rSelectionBox.left > localbox.left) info.rSelectionBox.left = localbox.left;
428 if (info.rSelectionBox.right <= localbox.right) info.rSelectionBox.right = localbox.right + 1;
429 if (info.rSelectionBox.bottom > localbox.bottom) info.rSelectionBox.bottom = localbox.bottom;
433 ////////////////////////////////////////////////////////////////////////////////
435 * Adds a single pixel to the existing selection.
437 bool CxImage::SelectionAddPixel(long x, long y, BYTE level)
439 if (pSelection==NULL) SelectionCreate();
440 if (pSelection==NULL) return false;
443 pSelection[x + y * head.biWidth] = level; // set the correct mask bit
445 if (info.rSelectionBox.top <= y) info.rSelectionBox.top = y+1;
446 if (info.rSelectionBox.left > x) info.rSelectionBox.left = x;
447 if (info.rSelectionBox.right <= x) info.rSelectionBox.right = x+1;
448 if (info.rSelectionBox.bottom > y) info.rSelectionBox.bottom = y;
455 ////////////////////////////////////////////////////////////////////////////////
457 * Exports the selection channel in a 8bpp grayscale image.
459 bool CxImage::SelectionSplit(CxImage *dest)
461 if (!pSelection || !dest) return false;
463 CxImage tmp(head.biWidth,head.biHeight,8);
465 strcpy(info.szLastError,tmp.GetLastError());
469 for(long y=0; y<head.biHeight; y++){
470 for(long x=0; x<head.biWidth; x++){
471 tmp.BlindSetPixelIndex(x,y,pSelection[x+y*head.biWidth]);
475 tmp.SetGrayPalette();
480 ////////////////////////////////////////////////////////////////////////////////
482 * Creates the selection channel from a gray scale image.
485 bool CxImage::SelectionSet(CxImage &from)
487 if (!from.IsGrayScale() || head.biWidth != from.head.biWidth || head.biHeight != from.head.biHeight){
488 strcpy(info.szLastError,"CxImage::SelectionSet: wrong width or height, or image is not gray scale");
492 if (pSelection==NULL) pSelection = (BYTE*)malloc(head.biWidth * head.biHeight);
494 BYTE* src = from.info.pImage;
495 BYTE* dst = pSelection;
496 if (src==NULL || dst==NULL){
497 strcpy(info.szLastError,"CxImage::SelectionSet: null pointer");
501 for (long y=0; y<head.biHeight; y++){
502 memcpy(dst,src,head.biWidth);
504 src += from.info.dwEffWidth;
507 SelectionRebuildBox();
511 ////////////////////////////////////////////////////////////////////////////////
513 * Sets the Selection level for a single pixel
514 * internal use only: doesn't set SelectionBox. Use SelectionAddPixel
516 void CxImage::SelectionSet(const long x,const long y,const BYTE level)
518 if (pSelection && IsInside(x,y)) pSelection[x+y*head.biWidth]=level;
520 ////////////////////////////////////////////////////////////////////////////////
522 * Gets the Selection level for a single pixel
524 BYTE CxImage::SelectionGet(const long x,const long y)
526 if (pSelection && IsInside(x,y)) return pSelection[x+y*head.biWidth];
529 ////////////////////////////////////////////////////////////////////////////////
531 * Rebuilds the SelectionBox
533 void CxImage::SelectionRebuildBox()
535 info.rSelectionBox.left = head.biWidth;
536 info.rSelectionBox.bottom = head.biHeight;
537 info.rSelectionBox.right = info.rSelectionBox.top = 0;
544 for (y=0; y<head.biHeight; y++){
545 for (x=0; x<info.rSelectionBox.left; x++){
546 if (pSelection[x+y*head.biWidth]){
547 info.rSelectionBox.left = x;
553 for (y=0; y<head.biHeight; y++){
554 for (x=head.biWidth-1; x>=info.rSelectionBox.right; x--){
555 if (pSelection[x+y*head.biWidth]){
556 info.rSelectionBox.right = x+1;
562 for (x=0; x<head.biWidth; x++){
563 for (y=0; y<info.rSelectionBox.bottom; y++){
564 if (pSelection[x+y*head.biWidth]){
565 info.rSelectionBox.bottom = y;
571 for (x=0; x<head.biWidth; x++){
572 for (y=head.biHeight-1; y>=info.rSelectionBox.top; y--){
573 if (pSelection[x+y*head.biWidth]){
574 info.rSelectionBox.top = y+1;
581 ////////////////////////////////////////////////////////////////////////////////
583 * Gets the Selection level for a single pixel
584 * "blind" version assumes that (x,y) is inside to the image.
586 BYTE CxImage::BlindSelectionGet(const long x,const long y)
589 if (!IsInside(x,y) || (pSelection==0))
590 #if CXIMAGE_SUPPORT_EXCEPTION_HANDLING
596 return pSelection[x+y*head.biWidth];
598 ////////////////////////////////////////////////////////////////////////////////
600 * Returns pointer to selection data for pixel (x,y).
602 BYTE* CxImage::SelectionGetPointer(const long x,const long y)
604 if (pSelection && IsInside(x,y)) return pSelection+x+y*head.biWidth;
607 ////////////////////////////////////////////////////////////////////////////////
608 bool CxImage::SelectionFlip()
610 if (!pSelection) return false;
612 BYTE *buff = (BYTE*)malloc(head.biWidth);
613 if (!buff) return false;
616 iSrc = pSelection + (head.biHeight-1)*head.biWidth;
618 for (long i=0; i<(head.biHeight/2); ++i)
620 memcpy(buff, iSrc, head.biWidth);
621 memcpy(iSrc, iDst, head.biWidth);
622 memcpy(iDst, buff, head.biWidth);
629 long top = info.rSelectionBox.top;
630 info.rSelectionBox.top = head.biHeight - info.rSelectionBox.bottom;
631 info.rSelectionBox.bottom = head.biHeight - top;
634 ////////////////////////////////////////////////////////////////////////////////
635 bool CxImage::SelectionMirror()
637 if (!pSelection) return false;
638 BYTE* pSelection2 = (BYTE*)malloc(head.biWidth * head.biHeight);
639 if (!pSelection2) return false;
642 long wdt=head.biWidth-1;
643 iSrc=pSelection + wdt;
645 for(long y=0; y < head.biHeight; y++){
646 for(long x=0; x <= wdt; x++)
652 pSelection=pSelection2;
654 long left = info.rSelectionBox.left;
655 info.rSelectionBox.left = head.biWidth - info.rSelectionBox.right;
656 info.rSelectionBox.right = head.biWidth - left;
659 ////////////////////////////////////////////////////////////////////////////////
660 #if CXIMAGE_SUPPORT_WINDOWS
662 * Converts the selection in a HRGN object.
664 bool CxImage::SelectionToHRGN(HRGN& region)
666 if (pSelection && region){
667 for(int y = 0; y < head.biHeight; y++){
671 for(; x < head.biWidth; x++){
672 if (pSelection[x + y * head.biWidth] != 0){
673 if (iStart == -1) iStart = x;
677 hTemp = CreateRectRgn(iStart, y, x, y + 1);
678 CombineRgn(region, hTemp, region, RGN_OR);
685 hTemp = CreateRectRgn(iStart, y, x, y + 1);
686 CombineRgn(region, hTemp, region, RGN_OR);
695 #endif //CXIMAGE_SUPPORT_WINDOWS
696 ////////////////////////////////////////////////////////////////////////////////
697 #endif //CXIMAGE_SUPPORT_SELECTION