//--------------------------------------------------------------------------- #include #pragma hdrstop #include "TPKnitImage.h" #pragma link "TPImage" #pragma package(smart_init) //--------------------------------------------------------------------------- // hotfix of undefined of min/max #define min(X,Y) (XY?X:Y) //--------------------------------------------------------------------------- // TPStitchdata //--------------------------------------------------------------------------- __fastcall TPStitchData::TPStitchData() { FBitmap = NULL; FDC = NULL; } //--------------------------------------------------------------------------- bool __fastcall TPStitchData::Create(int width, int height, int bpp, RGBQUAD *rgb) { if ((FBitmap = new TTexpiaBitmap) == NULL) goto fail; if (!FBitmap->Create(width, height, bpp, rgb)) goto fail; if ((FDC = FBitmap->CreateDC()) == NULL) goto fail; return true; fail: ::doDestroy(FBitmap); return false; } //--------------------------------------------------------------------------- __fastcall TPStitchData::~TPStitchData() { if (FDC) FBitmap->DeleteDC(FDC); if (FBitmap) delete FBitmap; } //--------------------------------------------------------------------------- bool __fastcall TPStitchData::DrawMark(char *m) { int x, y; Byte *IP, *DP, mm; if (FBitmap == NULL) goto fail; if (FDC) { FBitmap->DeleteDC(FDC); FDC = NULL; } if (!FBitmap->StartScanLine()) goto fail; for (y = 0; y < FBitmap->Height; y++) { IP = FBitmap->GetScanLine(y); DP = m + ((FBitmap->Width >> 3) + 1) * y; mm = 0x80; for (x = 0; x < FBitmap->Width; x++, IP++) { if (DP[x >> 3] & mm) *IP = 255; else *IP = 0; if (mm == 1) mm = 0x80; else mm >>= 1; } FBitmap->PutScanLine(y); } FBitmap->StopScanLine(); if ((FDC = FBitmap->CreateDC()) == NULL) goto fail; return true; fail: return false; } //--------------------------------------------------------------------------- void __fastcall TPStitchData::DrawMark(HDC gridhDC, RECT r) { int i, w, h, sx, sy; w = r.right - r.left; h = r.bottom - r.top; sx = ((w - FBitmap->Width) / 2) + r.left; sy = ((h - FBitmap->Height) / 2) + r.top; Rectangle(gridhDC, sx - 2, sy - 2, sx + FBitmap->Width + 2, sy + FBitmap->Height + 2); BitBlt(gridhDC, sx, sy, FBitmap->Width, FBitmap->Height, FDC, 0, 0, SRCCOPY); } //--------------------------------------------------------------------------- // ValidCtrCheck is used to assure that the components created do not have // any pure virtual functions. // static inline void ValidCtrCheck(TPKnitImage *) { new TPKnitImage(NULL); } //--------------------------------------------------------------------------- __fastcall TPKnitImage::TPKnitImage(TComponent* Owner) : TPImage(Owner) { FArrayCount = 1; FArrayBitmap = new TTexpiaBitmap[1]; FArrayIndex = 0; FBitmap = &FArrayBitmap[0]; FBitmap->OnPropertyChange = PropertyChange; FInsideBitmap = true; SetArrayCount(3); ArrayBitmap[0].CoordinateSystem = csBottomLeft; ArrayBitmap[1].CoordinateSystem = csBottomLeft; ArrayBitmap[2].CoordinateSystem = csBottomLeft; FMarkBitmap = new TTexpiaBitmap; FMarkBitmap->CoordinateSystem = csBottomLeft; } //--------------------------------------------------------------------------- __fastcall TPKnitImage::~TPKnitImage() { if (FMarkBitmap) delete FMarkBitmap; if (FInsideBitmap) delete[] FArrayBitmap; } //--------------------------------------------------------------------------- // Private Function //--------------------------------------------------------------------------- void __fastcall TPKnitImage::SetArrayIndex(int Value) { if (FArrayIndex!=Value && Value>=0 && ValueOnPropertyChange = PropertyChange; FArrayIndex = Value; Paint(); Invalidate(); } } //--------------------------------------------------------------------------- void __fastcall TPKnitImage::SetBitmap(TTexpiaBitmap *Value) { if (FBitmap!=Value) { if (Value) { if (FInsideBitmap) { delete[] FArrayBitmap; FArrayBitmap = NULL; } FBitmap = Value; FBitmap->OnPropertyChange = PropertyChange; FInsideBitmap = false; } else if (!FInsideBitmap) { FArrayCount = 1; FArrayBitmap = new TTexpiaBitmap[1]; FArrayIndex = 0; FBitmap = &FArrayBitmap[0]; FBitmap->OnPropertyChange = PropertyChange; FInsideBitmap = true; } Paint(); Invalidate(); } } //--------------------------------------------------------------------------- bool __fastcall TPKnitImage::SetArrayCount(int Value) { TTexpiaBitmap *tb; if (!FInsideBitmap) return false; if (FArrayCount!=Value) { if ((tb = new TTexpiaBitmap[Value])==NULL) return false; delete[] FArrayBitmap; FArrayBitmap = tb; FArrayCount = Value; FArrayIndex = 0; FBitmap = &FArrayBitmap[0]; FBitmap->OnPropertyChange = PropertyChange; } return true; } //--------------------------------------------------------------------------- void __fastcall TPKnitImage::SetStitch(TPStitchData **Value) { if (FStitch!=Value) { FStitch = Value; Paint(); Invalidate(); } } //--------------------------------------------------------------------------- bool __fastcall TPKnitImage::PositionChange() { int m = FMarginY, r = FRealPosY; if (FBitmap->Handle) { int ps = Height*FZoomOut/FZoomIn; if (ps>FBitmap->Height) { m = Height-FBitmap->Height*FZoomIn/FZoomOut; r = 0; } else { m = FZoomIn>1 ? Height-ps*FZoomIn/FZoomOut : 0; r = FBitmap->Height-FPositionY-ps; } if (m != FMarginY || r != FRealPosY) { FMarginY = m; FRealPosY = r; return true; } } return false; } //--------------------------------------------------------------------------- #define PAINT_INIT \ sx = FPositionX; \ sy = FRealPosY; \ dcDst = TGraphicControl::Canvas->Handle; \ if (FBitmap->BitsPerPixel==8) { \ hpaintpal = L_CreatePaintPalette(dcDst, bh); \ if (hpaintpal) holdpal = SelectPalette(dcDst, hpaintpal, false); \ } \ if (FParentImage) { \ FGrid = FParentImage->Grid; \ FZoomIn = FParentImage->ZoomIn; \ FZoomOut = FParentImage->ZoomOut; \ } \ pw = Width*FZoomOut/FZoomIn; \ ph = Height*FZoomOut/FZoomIn; \ if (pw>FBitmap->Width-sx) pw = FBitmap->Width-sx; \ if (ph>FBitmap->Height-sy) ph = FBitmap->Height-sy; \ if (FZoomOut>1) { \ pw -= pw%FZoomOut; \ ph -= ph%FZoomOut; \ } \ vw = pw*FZoomIn/FZoomOut; \ vh = ph*FZoomIn/FZoomOut; \ TGraphicControl::Canvas->Brush->Color = FColor; \ if (vwFillRect(Rect(vw, Height-vh, Width, Height)); \ if (vhFillRect(Rect(0, 0, Width, Height-vh)); //--------------------------------------------------------------------------- #define PAINT_GRIDLINE \ SetROP2(dcMemV, R2_COPYPEN); \ if ((hPen = CreatePen(psSolid, 1, FGridColor)) == NULL) goto fail; \ hOldPen = SelectObject(dcMemV, hPen); \ p = sx-Floor(sx/gx)*gx; \ p = p>0 ? (gx-p)*FZoomIn/FZoomOut : 0; \ while (p0 ? (gy-p)*FZoomIn/FZoomOut : 0; \ while (p0) { \ gx = FGridGapX*FGridMark; \ gy = FGridGapY*FGridMark; \ gzx = gx*FZoomIn/FZoomOut; \ gzy = gy*FZoomIn/FZoomOut; \ if ((hPen = CreatePen(psSolid, 1, FGridMarkColor)) == NULL) goto fail; \ hOldPen = SelectObject(dcMemV, hPen); \ p = sx-Floor(sx/gx)*gx; \ p = p>0 ? (gx-p)*FZoomIn/FZoomOut : 0; \ while (p0 ? (gy-p)*FZoomIn/FZoomOut : 0; \ while (pCreateDC(); \ BitBlt(dcMemV, 0, 0, pw*16, ph*16, dcMark, sx*16, sy*16, SRCINVERT); \ FMarkBitmap->DeleteDC(dcMark); \ } //--------------------------------------------------------------------------- // by playzzang for CrossLine #define PAINT_CROSSLINE \ if (FCrossLine) { \ hOldPen = SelectObject(dcMemV, GetStockObject(WHITE_PEN)); \ SetROP2(dcMemV, R2_NOT); \ MoveToEx(dcMemV, FCrossPos.x, 0, NULL); \ LineTo(dcMemV, FCrossPos.x, vh); \ MoveToEx(dcMemV, 0, FCrossPos.y, NULL); \ LineTo(dcMemV, vw, FCrossPos.y); \ SelectObject(dcMemV, hOldPen); \ } //--------------------------------------------------------------------------- void __fastcall TPKnitImage::SetCrossPos(POINT Pos) { FCrossPos.x = BitmapToCanvasX(Pos.x); FCrossPos.y = BitmapToCanvasY(Pos.y); } //--------------------------------------------------------------------------- void __fastcall TPKnitImage::PaintBottom(BITMAPHANDLE *bh) { HPALETTE hpaintpal = NULL, holdpal = NULL; int pw, ph, vw, vh, n, w, x, y; double sx, sy, syy, gx, gy, gzx, gzy, p; HDC dcSrc = NULL, dcDst, dcMemV = NULL; HBITMAP bmMemV = NULL; HPEN hPen = NULL, hOldPen; BYTE *bp; HDC dcMark; bool isStitch = FArrayIndex==1 && FZoomIn==16; PAINT_INIT; if (isStitch) { if ((dcSrc = FArrayBitmap[0].CreateDC()) == NULL) goto fail; } else { if ((dcSrc = FBitmap->CreateDC()) == NULL) goto fail; } if ((bmMemV = CreateCompatibleBitmap(dcSrc, vw, vh)) == NULL) goto fail; if ((dcMemV = CreateCompatibleDC(dcSrc)) == NULL) goto fail; SelectObject(dcMemV, bmMemV); if (hpaintpal) SelectPalette(dcMemV, hpaintpal, false); SetStretchBltMode(dcMemV, COLORONCOLOR); StretchBlt(dcMemV, 0, 0, vw, vh, dcSrc, sx, sy, pw, ph, SRCCOPY); if (FGrid) { gx = FGridGapX; gy = FGridGapY; gzx = gx*FZoomIn/FZoomOut; gzy = gy*FZoomIn/FZoomOut; if (gzx>=5 && gzy>=5) { if (FParentImage) { FGridColor = FParentImage->GridColor; FGridGapX = FParentImage->GridGapX; FGridGapY = FParentImage->GridGapY; FGridMark = FParentImage->GridMark; FGridMarkColor = FParentImage->GridMarkColor; sx = FParentImage->PositionX+(Left-FParentImage->Left)*FZoomOut/FZoomIn; sy = FParentImage->Bitmap->Height-FParentImage->PositionY- FParentImage->Height*FZoomOut/FZoomIn+(Top-FParentImage->Top)*FZoomOut/FZoomIn; if (Top < FParentImage->Top) sy--; syy = FParentImage->Bitmap->Height-ph-sy; } else { syy = FBitmap->Height-ph-sy; } PAINT_GRIDLINE; } } if (FCross) PAINT_CROSSLINE // by playzzang if (FOnPaintLocate) FOnPaintLocate(this,dcMemV);///½Ç¼±À» ±×¸®´Âµ¥ À§Ä¡´Â ±×¶§±×¶§ Á¤ÇÑ´Ù if (FOnPaintZoom) FOnPaintZoom(this, dcMemV); PAINT_STITCH; BitBlt(dcDst, 0, FMarginY, vw, vh, dcMemV, 0, 0, SRCCOPY); DeleteDC(dcMemV); DeleteObject(bmMemV); FBitmap->DeleteDC(dcSrc); if (isStitch) { FArrayBitmap[0].DeleteDC(dcSrc); } else { FBitmap->DeleteDC(dcSrc); } if (hpaintpal) { if (holdpal) SelectPalette(dcDst, holdpal, false); DeleteObject(hpaintpal); } if (FOnPaint) FOnPaint(this); return; fail: if (dcSrc) { if (bmMemV) { if (dcMemV) { if (hPen) DeleteObject(hPen); DeleteDC(dcMemV); } DeleteObject(bmMemV); } if (isStitch) { FArrayBitmap[0].DeleteDC(dcSrc); } else { FBitmap->DeleteDC(dcSrc); } } if (hpaintpal) { if (holdpal) SelectPalette(dcDst, holdpal, false); DeleteObject(hpaintpal); } } //--------------------------------------------------------------------------- void __fastcall TPKnitImage::PaintBottomMask(BITMAPHANDLE *bh) { BITMAPHANDLE *mh; HPALETTE hpaintpal = NULL, holdpal = NULL; int pw, ph, vw, vh, n, w, x, y; double sx, sy, syy, gx, gy, gzx, gzy, p; HDC dcSrc = NULL, dcDst, dcMemV = NULL, dcMSrc = NULL; HBITMAP bmMemV =NULL; HPEN hPen = NULL, hOldPen; BYTE *bp; HDC dcMark; bool isStitch = FArrayIndex==1 && FZoomIn==16; mh = FMask->Handle; if (mh) { PAINT_INIT; if ((dcMSrc = FMask->CreateDC()) == NULL) goto fail; if (isStitch) { if ((dcSrc = FArrayBitmap[0].CreateDC()) == NULL) goto fail; } else { if ((dcSrc = FBitmap->CreateDC()) == NULL) goto fail; } if ((bmMemV = CreateCompatibleBitmap(dcSrc, vw, vh)) == NULL) goto fail; if ((dcMemV = CreateCompatibleDC(dcSrc)) == NULL) goto fail; SelectObject(dcMemV, bmMemV); if (hpaintpal) SelectPalette(dcMemV, hpaintpal, false); SetStretchBltMode(dcMemV, COLORONCOLOR); StretchBlt(dcMemV, 0, 0, vw, vh, dcSrc, sx, sy, pw, ph, SRCCOPY); StretchBlt(dcMemV, 0, 0, vw, vh, dcMSrc, sx, sy, pw, ph, SRCINVERT); if (FGrid) { gx = FGridGapX; gy = FGridGapY; gzx = gx*FZoomIn/FZoomOut; gzy = gy*FZoomIn/FZoomOut; if (gzx>=5 && gzy>=5) { if (FParentImage) { FGridColor = FParentImage->GridColor; FGridGapX = FParentImage->GridGapX; FGridGapY = FParentImage->GridGapY; FGridMark = FParentImage->GridMark; FGridMarkColor = FParentImage->GridMarkColor; sx = FParentImage->PositionX+(Left-FParentImage->Left)*FZoomOut/FZoomIn; sy = FParentImage->Bitmap->Height-FParentImage->PositionY- FParentImage->Height*FZoomOut/FZoomIn+(Top-FParentImage->Top)*FZoomOut/FZoomIn; if (Top < FParentImage->Top) sy--; syy = FParentImage->Bitmap->Height-ph-sy; } else { syy = FBitmap->Height-ph-sy; } PAINT_GRIDLINE; } } if (FCross) PAINT_CROSSLINE // by playzzang if (FOnPaintLocate) FOnPaintLocate(this,dcMemV);///½Ç¼±À» ±×¸®´Âµ¥ À§Ä¡´Â ±×¶§±×¶§ Á¤ÇÑ´Ù if (FOnPaintZoom) FOnPaintZoom(this, dcMemV); PAINT_STITCH; BitBlt(dcDst, 0, FMarginY, vw, vh, dcMemV, 0, 0, SRCCOPY); DeleteDC(dcMemV); DeleteObject(bmMemV); if (isStitch) { FArrayBitmap[0].DeleteDC(dcSrc); } else { FBitmap->DeleteDC(dcSrc); } FMask->DeleteDC(dcMSrc); if (hpaintpal) { if (holdpal) SelectPalette(dcDst, holdpal, false); DeleteObject(hpaintpal); } if (FOnPaint) FOnPaint(this); } return; fail: if (dcMSrc) { if (dcSrc) { if (bmMemV) { if (dcMemV) { if (hPen) DeleteObject(hPen); DeleteDC(dcMemV); } DeleteObject(bmMemV); } if (isStitch) { FArrayBitmap[0].DeleteDC(dcSrc); } else { FBitmap->DeleteDC(dcSrc); } } FMask->DeleteDC(dcMSrc); } if (hpaintpal) { if (holdpal) SelectPalette(dcDst, holdpal, false); DeleteObject(hpaintpal); } } //--------------------------------------------------------------------------- void __fastcall TPKnitImage::PropertyChange() { if (PositionChange()) { if (FOnPositionChange) FOnPositionChange(this); } Invalidate(); } //--------------------------------------------------------------------------- // Protected Function //--------------------------------------------------------------------------- void __fastcall TPKnitImage::Paint() { BITMAPHANDLE *bh; if (ComponentState.Contains(csDesigning)) { TGraphicControl::Canvas->Pen->Style = psDash; TGraphicControl::Canvas->Brush->Style = bsClear; TGraphicControl::Canvas->Rectangle(0, 0, Width, Height); } else { bh = FBitmap->Handle; if (bh) { if (Width<=0 || Height<=0) return; if (FMaskEnabled) { PaintBottomMask(bh); } else { PaintBottom(bh); } } } } //--------------------------------------------------------------------- void __fastcall TPKnitImage::MouseDown(TMouseButton Button, TShiftState Shift, int X, int Y) { int px, py; if (FCoordinationTransfer) { if (FBitmap->Handle) { px = FPositionX+X*FZoomOut/FZoomIn; py = FRealPosY+(Y-FMarginY)*FZoomOut/FZoomIn; TControl::MouseDown(Button, Shift, px, py); } else { TControl::MouseDown(Button, Shift, X, Y); } } else { TControl::MouseDown(Button, Shift, X, Y); } } //--------------------------------------------------------------------- void __fastcall TPKnitImage::MouseMove(TShiftState Shift, int X, int Y) { int px, py; if (FCoordinationTransfer) { if (FBitmap->Handle) { px = FPositionX+X*FZoomOut/FZoomIn; py = FRealPosY+(Y-FMarginY)*FZoomOut/FZoomIn; TControl::MouseMove(Shift, px, py); } else { TControl::MouseMove(Shift, X, Y); } } else { TControl::MouseMove(Shift, X, Y); } } //--------------------------------------------------------------------- void __fastcall TPKnitImage::MouseUp(TMouseButton Button, TShiftState Shift, int X, int Y) { int px, py; if (FCoordinationTransfer) { if (FBitmap->Handle) { px = FPositionX+X*FZoomOut/FZoomIn; py = FRealPosY+(Y-FMarginY)*FZoomOut/FZoomIn; TControl::MouseUp(Button, Shift, px, py); } else { TControl::MouseUp(Button, Shift, X, Y); } } else { TControl::MouseUp(Button, Shift, X, Y); } } //--------------------------------------------------------------------- // Public Function //--------------------------------------------------------------------------- bool __fastcall TPKnitImage::Create(int x, int y, int width, int height, RGBQUAD *rgb) { ArrayBitmap[0].CoordinateSystem = csBottomLeft; if (!ArrayBitmap[0].Create(x, y, 8, rgb)) return false; ArrayBitmap[1].CoordinateSystem = csBottomLeft; if (!ArrayBitmap[1].Create(x, y, 8, rgb)) return false; ArrayBitmap[2].CoordinateSystem = csBottomLeft; if (!ArrayBitmap[2].Create(width, height, 8, rgb)) return false; CreateMark(); return true; } //--------------------------------------------------------------------------- // ÇöÀç palette¸¦ rgb·Î ¼³Á¤ÇÑ´Ù. void __fastcall TPKnitImage::ChangeRGBColors(RGBQUAD *rgb) { FBitmap->PutColors(0, 256, rgb); if (FSubBitmap) FSubBitmap->PutColors(0, 256, rgb); if (FSubMask) FSubMask->PutColors(0, 256, rgb); } //--------------------------------------------------------------------------- void __fastcall TPKnitImage::OutlineBitmapRgn(bool bComplex) { pBITMAPHANDLE bh; RGNXFORM XForm; HRGN rgn; HBRUSH hBrush; if (FBitmap->IsUse) return; bh = FBitmap->Handle; if (bh) { if (L_BitmapHasRgn(bh)) { if (FParentImage) { FZoomIn = FParentImage->ZoomIn; FZoomOut = FParentImage->ZoomOut; } nFrameRgn = (nFrameRgn+1)&7; XForm.uViewPerspective = TOP_LEFT; XForm.nXScalarNum = FZoomIn; XForm.nXScalarDen = FZoomOut; XForm.nYScalarNum = FZoomIn; XForm.nYScalarDen = FZoomOut; XForm.nXOffset = -FPositionX*FZoomIn/FZoomOut; if (ZoomIn>1) { XForm.nYOffset = FMarginY-(FRealPosY-1)*FZoomIn/FZoomOut-FZoomIn; } else { XForm.nYOffset = FMarginY-(FRealPosY-1)*FZoomIn/FZoomOut; } if (bComplex) { L_FrameBitmapRgn(TGraphicControl::Canvas->Handle, bh, &XForm, nFrameRgn); } else { L_GetBitmapRgnHandle(bh, &XForm, &rgn); if ((hBrush = CreatePatternBrush(bmFrameRgn[nFrameRgn])) != NULL) { FrameRgn(TGraphicControl::Canvas->Handle, rgn, hBrush, 1, 1); DeleteObject(hBrush); } DeleteObject(rgn); } } } } //--------------------------------------------------------------------------- #define RECTPAINT_INIT \ sx = FPositionX; \ sy = FRealPosY; \ dcDst = TGraphicControl::Canvas->Handle; \ if (FBitmap->BitsPerPixel==8) { \ hpaintpal = L_CreatePaintPalette(dcDst, bh); \ if (hpaintpal) holdpal = SelectPalette(dcDst, hpaintpal, false); \ } \ if (FParentImage) { \ FGrid = FParentImage->Grid; \ FZoomIn = FParentImage->ZoomIn; \ FZoomOut = FParentImage->ZoomOut; \ } \ Src.left -= sx; \ Src.top -= sy; \ Src.right -= sx; \ Src.bottom -= sy; \ pw = min(Width*FZoomOut/FZoomIn, int(FBitmap->Width-sx)); \ ph = min(Height*FZoomOut/FZoomIn, int(FBitmap->Height-sy)); \ if (Src.left<0) Src.left = 0; \ if (Src.top<0) Src.top = 0; \ if (Src.right>pw) Src.right = pw; \ if (Src.bottom>ph) Src.bottom = ph; \ if (FZoomOut>1) { \ Src.left -= Src.left%FZoomOut; \ Src.top -= Src.top%FZoomOut; \ Src.right = Src.right-Src.right%FZoomOut; \ Src.bottom = Src.bottom-Src.bottom%FZoomOut; \ } \ pw = Src.right-Src.left; \ ph = Src.bottom-Src.top; \ vw = pw*FZoomIn/FZoomOut; \ vh = ph*FZoomIn/FZoomOut; \ px = Src.left*FZoomIn/FZoomOut; \ py = FMarginY+Src.top*FZoomIn/FZoomOut; \ sx += Src.left; \ sy += Src.top; //--------------------------------------------------------------------------- void __fastcall TPKnitImage::RectPaint(RECT Src) { BITMAPHANDLE *bh, *mh; HPALETTE hpaintpal = NULL, holdpal = NULL; int px, py, pw, ph, vw, vh, x, y, n, w; double sx, sy, syy, gx, gy, gzx, gzy, p; HDC dcSrc = NULL, dcDst, dcMemV = NULL, dcMSrc = NULL; HBITMAP bmMemV = NULL; HPEN hPen = NULL, hOldPen; BYTE *bp; HDC dcMark; bool isStitch = FArrayIndex==1 && FZoomIn==16; bh = FBitmap->Handle; if (FMaskEnabled) { mh = FMask->Handle; if (mh && bh) { if (Src.leftWidth && Src.right>=FPositionX && Src.topHeight && Src.bottom>=FRealPosY) { RECTPAINT_INIT; if ((dcMSrc = FMask->CreateDC()) == NULL) goto fail; if (isStitch) { if ((dcSrc = FArrayBitmap[0].CreateDC()) == NULL) goto fail; } else { if ((dcSrc = FBitmap->CreateDC()) == NULL) goto fail; } if ((bmMemV = CreateCompatibleBitmap(dcSrc, vw, vh)) == NULL) goto fail; if ((dcMemV = CreateCompatibleDC(dcSrc)) == NULL) goto fail; SelectObject(dcMemV, bmMemV); if (hpaintpal) SelectPalette(dcMemV, hpaintpal, false); SetStretchBltMode(dcMemV, COLORONCOLOR); StretchBlt(dcMemV, 0, 0, vw, vh, dcSrc, sx, sy, pw, ph, SRCCOPY); StretchBlt(dcMemV, 0, 0, vw, vh, dcMSrc, sx, sy, pw, ph, SRCINVERT); if (FGrid) { gx = FGridGapX; gy = FGridGapY; gzx = gx*FZoomIn/FZoomOut; gzy = gy*FZoomIn/FZoomOut; if (gzx>=5 && gzy>=5) { if (FParentImage) { FGridColor = FParentImage->GridColor; FGridGapX = FParentImage->GridGapX; FGridGapY = FParentImage->GridGapY; FGridMark = FParentImage->GridMark; FGridMarkColor = FParentImage->GridMarkColor; sx = FParentImage->PositionX+(Left-FParentImage->Left)*FZoomOut/FZoomIn+Src.left; sy = FParentImage->Bitmap->Height-FParentImage->PositionY- FParentImage->Height*FZoomOut/FZoomIn+(Top-FParentImage->Top)*FZoomOut/FZoomIn+Src.top; if (Top < FParentImage->Top) sy--; syy = FParentImage->Bitmap->Height-ph-sy; } else { syy = FBitmap->Height-ph-sy; } PAINT_GRIDLINE; } } if (FCross) PAINT_CROSSLINE // by playzzang PAINT_STITCH; BitBlt(dcDst, px, py, vw, vh, dcMemV, 0, 0, SRCCOPY); DeleteDC(dcMemV); DeleteObject(bmMemV); if (hpaintpal) { if (holdpal) SelectPalette(dcDst, holdpal, false); DeleteObject(hpaintpal); } if (isStitch) { FArrayBitmap[0].DeleteDC(dcSrc); } else { FBitmap->DeleteDC(dcSrc); } FMask->DeleteDC(dcMSrc); } } } else { if (bh) { if (Src.leftWidth && Src.right>=FPositionX && Src.topHeight && Src.bottom>=FRealPosY) { RECTPAINT_INIT if (isStitch) { if ((dcSrc = FArrayBitmap[0].CreateDC()) == NULL) goto fail; } else { if ((dcSrc = FBitmap->CreateDC()) == NULL) goto fail; } if ((bmMemV = CreateCompatibleBitmap(dcSrc, vw, vh)) == NULL) goto fail; if ((dcMemV = CreateCompatibleDC(dcSrc)) == NULL) goto fail; SelectObject(dcMemV, bmMemV); if (hpaintpal) SelectPalette(dcMemV, hpaintpal, false); SetStretchBltMode(dcMemV, COLORONCOLOR); StretchBlt(dcMemV, 0, 0, vw, vh, dcSrc, sx, sy, pw, ph, SRCCOPY); if (FGrid) { gx = FGridGapX; gy = FGridGapY; gzx = gx*FZoomIn/FZoomOut; gzy = gy*FZoomIn/FZoomOut; if (gzx>=5 && gzy>=5) { if (FParentImage) { FGridColor = FParentImage->GridColor; FGridGapX = FParentImage->GridGapX; FGridGapY = FParentImage->GridGapY; FGridMark = FParentImage->GridMark; FGridMarkColor = FParentImage->GridMarkColor; sx = FParentImage->PositionX+(Left-FParentImage->Left)*FZoomOut/FZoomIn+Src.left; sy = FParentImage->Bitmap->Height-FParentImage->PositionY- FParentImage->Height*FZoomOut/FZoomIn+(Top-FParentImage->Top)*FZoomOut/FZoomIn+Src.top; if (Top < FParentImage->Top) sy--; syy = FParentImage->Bitmap->Height-ph-sy; } else { syy = FBitmap->Height-ph-sy; } PAINT_GRIDLINE; } } if (FCross) PAINT_CROSSLINE // by playzzang PAINT_STITCH; BitBlt(dcDst, px, py, vw, vh, dcMemV, 0, 0, SRCCOPY); DeleteDC(dcMemV); DeleteObject(bmMemV); if (hpaintpal) { if (holdpal) SelectPalette(dcDst, holdpal, false); DeleteObject(hpaintpal); } if (isStitch) { FArrayBitmap[0].DeleteDC(dcSrc); } else { FBitmap->DeleteDC(dcSrc); } } } } return; fail: if (dcSrc) { if (bmMemV) { if (dcMemV) { if (hPen) DeleteObject(hPen); DeleteDC(dcMemV); } DeleteObject(bmMemV); } if (isStitch) { FArrayBitmap[0].DeleteDC(dcSrc); } else { FBitmap->DeleteDC(dcSrc); } } if (dcMSrc) FMask->DeleteDC(dcMSrc); if (hpaintpal) { if (holdpal) SelectPalette(dcDst, holdpal, false); DeleteObject(hpaintpal); } } //---------------------------------------------------------------------------- void __fastcall TPKnitImage::RectEditPaint(RECT Src, TRectEditPaint func, void *pData) { BITMAPHANDLE *bh, *mh; HPALETTE hpaintpal = NULL, holdpal = NULL; int px, py, pw, ph, vw, vh, x, y, n, w; double sx, sy, syy, gx, gy, gzx, gzy, p; HDC dcSrc = NULL, dcDst, dcMemP = NULL, dcMemV = NULL, dcMSrc = NULL; HBITMAP bmMemP = NULL, bmMemV = NULL; HPEN hPen = NULL, hOldPen; BYTE *bp; HDC dcMark; bool isStitch = FArrayIndex==1 && FZoomIn==16; bh = FBitmap->Handle; if (FMaskEnabled) { mh = FMask->Handle; if (mh && bh) { if (Src.leftWidth && Src.right>=FPositionX && Src.topHeight && Src.bottom>=FRealPosY) { RECTPAINT_INIT; if ((dcMSrc = FMask->CreateDC()) == NULL) goto fail; if (isStitch) { if ((dcSrc = FArrayBitmap[0].CreateDC()) == NULL) goto fail; } else { if ((dcSrc = FBitmap->CreateDC()) == NULL) goto fail; } if ((bmMemP = CreateCompatibleBitmap(dcSrc, pw, ph)) == NULL) goto fail; if ((dcMemP = CreateCompatibleDC(dcSrc)) == NULL) goto fail; SelectObject(dcMemP, bmMemP); if (hpaintpal) SelectPalette(dcMemP, hpaintpal, false); BitBlt(dcMemP, 0, 0, pw, ph, dcSrc, sx, sy, SRCCOPY); BitBlt(dcMemP, 0, 0, pw, ph, dcMSrc, sx, sy, SRCINVERT); if (func) func(dcMemP, sx, sy, pData); if ((bmMemV = CreateCompatibleBitmap(dcSrc, vw, vh)) == NULL) goto fail; if ((dcMemV = CreateCompatibleDC(dcSrc)) == NULL) goto fail; SelectObject(dcMemV, bmMemV); if (hpaintpal) SelectPalette(dcMemV, hpaintpal, false); SetStretchBltMode(dcMemV, COLORONCOLOR); StretchBlt(dcMemV, 0, 0, vw, vh, dcMemP, 0, 0, pw, ph, SRCCOPY); if (FGrid) { gx = FGridGapX; gy = FGridGapY; gzx = gx*FZoomIn/FZoomOut; gzy = gy*FZoomIn/FZoomOut; if (gzx>=5 && gzy>=5) { if (FParentImage) { FGridColor = FParentImage->GridColor; FGridGapX = FParentImage->GridGapX; FGridGapY = FParentImage->GridGapY; FGridMark = FParentImage->GridMark; FGridMarkColor = FParentImage->GridMarkColor; sx = FParentImage->PositionX+(Left-FParentImage->Left)*FZoomOut/FZoomIn+Src.left; sy = FParentImage->Bitmap->Height-FParentImage->PositionY- FParentImage->Height*FZoomOut/FZoomIn+(Top-FParentImage->Top)*FZoomOut/FZoomIn+Src.top; if (Top < FParentImage->Top) sy--; syy = FParentImage->Bitmap->Height-ph-sy; } else { syy = FBitmap->Height-ph-sy; } PAINT_GRIDLINE; } } if (FCross) PAINT_CROSSLINE // by playzzang PAINT_STITCH; BitBlt(dcDst, px, py, vw, vh, dcMemV, 0, 0, SRCCOPY); DeleteDC(dcMemV); DeleteObject(bmMemV); DeleteDC(dcMemP); DeleteObject(bmMemP); if (hpaintpal) { if (holdpal) SelectPalette(dcDst, holdpal, false); DeleteObject(hpaintpal); } if (isStitch) { FArrayBitmap[0].DeleteDC(dcSrc); } else { FBitmap->DeleteDC(dcSrc); } FMask->DeleteDC(dcMSrc); } } } else { if (bh) { if (Src.leftWidth && Src.right>=FPositionX && Src.topHeight && Src.bottom>=FRealPosY) { RECTPAINT_INIT; if (isStitch) { if ((dcSrc = FArrayBitmap[0].CreateDC()) == NULL) goto fail; } else { if ((dcSrc = FBitmap->CreateDC()) == NULL) goto fail; } if ((bmMemP = CreateCompatibleBitmap(dcSrc, pw, ph)) == NULL) goto fail; if ((dcMemP = CreateCompatibleDC(dcSrc)) == NULL) goto fail; SelectObject(dcMemP, bmMemP); if (hpaintpal) SelectPalette(dcMemP, hpaintpal, false); BitBlt(dcMemP, 0, 0, pw, ph, dcSrc, sx, sy, SRCCOPY); if (func) func(dcMemP, sx, sy, pData); if ((bmMemV = CreateCompatibleBitmap(dcSrc, vw, vh)) == NULL) goto fail; if ((dcMemV = CreateCompatibleDC(dcSrc)) == NULL) goto fail; SelectObject(dcMemV, bmMemV); if (hpaintpal) SelectPalette(dcMemV, hpaintpal, false); SetStretchBltMode(dcMemV, COLORONCOLOR); StretchBlt(dcMemV, 0, 0, vw, vh, dcMemP, 0, 0, pw, ph, SRCCOPY); if (FGrid) { gx = FGridGapX; gy = FGridGapY; gzx = gx*FZoomIn/FZoomOut; gzy = gy*FZoomIn/FZoomOut; if (gzx>=5 && gzy>=5) { if (FParentImage) { FGridColor = FParentImage->GridColor; FGridGapX = FParentImage->GridGapX; FGridGapY = FParentImage->GridGapY; FGridMark = FParentImage->GridMark; FGridMarkColor = FParentImage->GridMarkColor; sx = FParentImage->PositionX+(Left-FParentImage->Left)*FZoomOut/FZoomIn+Src.left; sy = FParentImage->Bitmap->Height-FParentImage->PositionY- FParentImage->Height*FZoomOut/FZoomIn+(Top-FParentImage->Top)*FZoomOut/FZoomIn+Src.top; if (Top < FParentImage->Top) sy--; syy = FParentImage->Bitmap->Height-ph-sy; } else { syy = FBitmap->Height-ph-sy; } PAINT_GRIDLINE; } } if (FCross) PAINT_CROSSLINE // by playzzang PAINT_STITCH; BitBlt(dcDst, px, py, vw, vh, dcMemV, 0, 0, SRCCOPY); DeleteDC(dcMemV); DeleteObject(bmMemV); DeleteDC(dcMemP); DeleteObject(bmMemP); if (hpaintpal) { if (holdpal) SelectPalette(dcDst, holdpal, false); DeleteObject(hpaintpal); } if (isStitch) { FArrayBitmap[0].DeleteDC(dcSrc); } else { FBitmap->DeleteDC(dcSrc); } } } } return; fail: if (dcSrc) { if (bmMemP) { if (dcMemP) { if (bmMemV) { if (dcMemV) { if (hPen) DeleteObject(hPen); DeleteDC(dcMemV); } DeleteObject(bmMemV); } DeleteDC(dcMemP); } DeleteObject(bmMemP); } if (isStitch) { FArrayBitmap[0].DeleteDC(dcSrc); } else { FBitmap->DeleteDC(dcSrc); } } if (dcMSrc) FMask->DeleteDC(dcMSrc); if (hpaintpal) { if (holdpal) SelectPalette(dcDst, holdpal, false); DeleteObject(hpaintpal); } } //--------------------------------------------------------------------------- int __fastcall TPKnitImage::BitmapToCanvasX(int X) { return (X-FPositionX)*FZoomIn/FZoomOut; } //--------------------------------------------------------------------------- int __fastcall TPKnitImage::BitmapToCanvasY(int Y) { return FMarginY+(Y-FRealPosY)*FZoomIn/FZoomOut; } //--------------------------------------------------------------------------- int __fastcall TPKnitImage::CanvasToBitmapX(int X) { return FPositionX+X*FZoomOut/FZoomIn; } //--------------------------------------------------------------------------- int __fastcall TPKnitImage::CanvasToBitmapY(int Y) { return FRealPosY+(Y-FMarginY)*FZoomOut/FZoomIn; } //--------------------------------------------------------------------------- void __fastcall TPKnitImage::FullViewPaint(HDC dcDst, int zi, int zo) { HPALETTE hpaintpal = NULL, holdpal = NULL; int pw, ph, vw, vh; HDC dcSrc = NULL; vw = FBitmap->Width *zi / zo; vh = FBitmap->Height *zi / zo; pw = vw * zo / zi; ph = vh * zo / zi; if (FBitmap->BitsPerPixel==8) { hpaintpal = L_CreatePaintPalette(dcDst, FBitmap->Handle); if (hpaintpal) holdpal = SelectPalette(dcDst, hpaintpal, false); } if ((dcSrc = FBitmap->CreateDC()) == NULL) goto fail; SetStretchBltMode(dcDst, COLORONCOLOR); StretchBlt(dcDst, 0, 0, vw, vh, dcSrc, 0, 0, pw, ph, SRCCOPY); FBitmap->DeleteDC(dcSrc); if (hpaintpal) { if (holdpal) SelectPalette(dcDst, holdpal, false); DeleteObject(hpaintpal); } return; fail: if (dcSrc) FBitmap->DeleteDC(dcSrc); if (hpaintpal) { if (holdpal) SelectPalette(dcDst, holdpal, false); DeleteObject(hpaintpal); } } //--------------------------------------------------------------------------- bool __fastcall TPKnitImage::CreateMark() { return FMarkBitmap->Create(ArrayBitmap[0].Width*16, ArrayBitmap[0].Height*16, 1); } //--------------------------------------------------------------------------- bool __fastcall TPKnitImage::SetMark() { int x, y, n, w; HDC hDC; Byte *bp; hDC = FMarkBitmap->CreateDC(); BitBlt(hDC, 0, 0, FMarkBitmap->Width, FMarkBitmap->Height, NULL, 0, 0, BLACKNESS); FArrayBitmap[1].StartScanLine(); for (y=0; yBitmap) { BitBlt(hDC, x*16, y*16, FStitch[n]->Bitmap->Width+1, FStitch[n]->Bitmap->Height+1, FStitch[n]->DC, 0, 0, SRCCOPY); } x++; bp++; } else if ((*bp&0x3F)>1) { if ((*bp&0x80)==0) { n = *bp&0x3F; if (FStitch[n]->Bitmap) { BitBlt(hDC, x*16, y*16, FStitch[n]->Bitmap->Width+1, FStitch[n]->Bitmap->Height+1, FStitch[n]->DC, 0, 0, SRCCOPY); w = (FStitch[n]->Bitmap->Width+1)/16; x += w; bp += w; } else { x++; bp++; } } else { x++; bp++; } } else { x++; bp++; } } } FArrayBitmap[1].StopScanLine(); FMarkBitmap->DeleteDC(hDC); return true; } //--------------------------------------------------------------------------- bool __fastcall TPKnitImage::SetMark(RECT rect) { int x, y, n, w; HDC hDC; Byte *bp; hDC = FMarkBitmap->CreateDC(); // BitBlt(hDC, rect.left*16, rect.top*16, (rect.right-rect.left)*16, // (rect.bottom-rect.top)*16, NULL, 0, 0, BLACKNESS); FArrayBitmap[1].StartScanLine(); for (y=rect.top; yBitmap) { BitBlt(hDC, x*16, y*16, FStitch[n]->Bitmap->Width+1, FStitch[n]->Bitmap->Height+1, NULL, 0, 0, BLACKNESS); BitBlt(hDC, x*16, y*16, FStitch[n]->Bitmap->Width+1, FStitch[n]->Bitmap->Height+1, FStitch[n]->DC, 0, 0, SRCCOPY); } x++; bp++; } else if ((*bp&0x3F)>1) { if ((*bp&0x80)==0) { n = *bp&0x3F; if (FStitch[n]->Bitmap) { BitBlt(hDC, x*16, y*16, FStitch[n]->Bitmap->Width+1, FStitch[n]->Bitmap->Height+1, NULL, 0, 0, BLACKNESS); BitBlt(hDC, x*16, y*16, FStitch[n]->Bitmap->Width+1, FStitch[n]->Bitmap->Height+1, FStitch[n]->DC, 0, 0, SRCCOPY); w = (FStitch[n]->Bitmap->Width+1)/16; x += w; bp += w; } else { x++; bp++; } } else { x++; bp++; } } else { x++; bp++; } } } FArrayBitmap[1].StopScanLine(); FMarkBitmap->DeleteDC(hDC); return true; } //--------------------------------------------------------------------------- bool __fastcall TPKnitImage::DrawMark(int x, int y, int n) { HDC hDC = FMarkBitmap->CreateDC(); BitBlt(hDC, x*16, y*16, FStitch[n]->Bitmap->Width+1, FStitch[n]->Bitmap->Height+1, FStitch[n]->DC, 0, 0, SRCCOPY); FMarkBitmap->DeleteDC(hDC); return true; } //--------------------------------------------------------------------------- bool __fastcall TPKnitImage::ClearMark(int x, int y, int w, int h) { HDC hDC = FMarkBitmap->CreateDC(); BitBlt(hDC, x*16, y*16, w*16-1, h*16-1, NULL, 0, 0, BLACKNESS); FMarkBitmap->DeleteDC(hDC); return true; } //--------------------------------------------------------------------------- namespace Tpknitimage { void __fastcall PACKAGE Register() { TComponentClass classes[1] = {__classid(TPKnitImage)}; RegisterComponents("Texpia", classes, 0); } } //---------------------------------------------------------------------------