//--------------------------------------------------------------------------- #include #include #pragma hdrstop #include "TPLayerImage.h" #pragma link "TPImage" #define min(a, b) (((a) < (b)) ? (a) : (b)) #define max(a, b) (((a) > (b)) ? (a) : (b)) //--------------------------------------------------------------------------- #pragma package(smart_init) //--------------------------------------------------------------------------- // À̸§ Á¤ÇÏ´Â ÇÔ¼ö //--------------------------------------------------------------------------- static AnsiString __fastcall TempLayerFilename() { char tp[MAX_PATH+1], fn[MAX_PATH+1]; GetTempPath(MAX_PATH, tp); if (tp[strlen(tp)-1]=='\\') tp[strlen(tp)-1] = 0; while (1) { sprintf(fn, "%s\\TPL%05X.TMP", tp, rand() & 0xFFFFF); if (!FileExists(fn)) break; } // Warning ¼öÁ¤ (2007. 05. 17 Annualring) return ((AnsiString)fn); } //--------------------------------------------------------------------------- // TPLayerPart Class ------------ each layer property //--------------------------------------------------------------------------- __fastcall TPLayer::TPLayer(TLayerType lt) { FLayerType = lt; Visible = true; LBitmap = new TTexpiaBitmap; LMask = FLayerType==LTInitial ? NULL : new TTexpiaBitmap; MiniBitmap = new TTexpiaBitmap; } //--------------------------------------------------------------------------- __fastcall TPLayer::TPLayer(AnsiString str, AnsiString fn, TLayerType lt) { FLayerType = lt; Name = str; FileName = fn; Visible = true; LBitmap = new TTexpiaBitmap; LMask = FLayerType==LTInitial ? NULL : new TTexpiaBitmap; MiniBitmap = new TTexpiaBitmap; } //--------------------------------------------------------------------------- __fastcall TPLayer::~TPLayer() { if (FileExists(FileName)) DeleteFile(FileName); if (LMask) delete LMask; if (LBitmap) delete LBitmap; if (MiniBitmap) { MiniBitmap->Destroy(); delete MiniBitmap; } } //--------------------------------------------------------------------------- bool __fastcall TPLayer::LoadFromFile(HANDLE hFile, RGBQUAD *rgb) { DWORD dwRead; int w, h; Byte bpp; if (!ReadFile(hFile, &w, sizeof(int), &dwRead, NULL)) return false; if (!ReadFile(hFile, &h, sizeof(int), &dwRead, NULL)) return false; if (!ReadFile(hFile, &bpp, sizeof(Byte), &dwRead, NULL)) return false; if (LMask) { if (bpp == 8) { if (!LBitmap->Create(w, h, 8, rgb)) return false; if (!LMask->Create(w, h, 8, rgb)) return false; } else { if (!LBitmap->Create(w, h, 24)) return false; if (!LMask->Create(w, h, 1)) return false; } if (!LBitmap->LoadFromTexpiaFile(hFile, cmZLib)) return false; if (!LMask->LoadFromTexpiaFile(hFile, cmZLib)) return false; } else { if (bpp == 8) { if (!LBitmap->Create(w, h, 8, rgb)) return false; } else { if (!LBitmap->Create(w, h, 24)) return false; } if (!LBitmap->LoadFromTexpiaFile(hFile, cmZLib)) return false; } return true; } //--------------------------------------------------------------------------- bool __fastcall TPLayer::SaveToFile(HANDLE hFile) { DWORD dwWrite; Byte bpp; int w, h; w = LBitmap->Width; h = LBitmap->Height; bpp = LBitmap->BitsPerPixel; if (!WriteFile(hFile, &w, sizeof(int), &dwWrite, NULL)) return false; if (!WriteFile(hFile, &h, sizeof(int), &dwWrite, NULL)) return false; if (!WriteFile(hFile, &bpp, sizeof(Byte), &dwWrite, NULL)) return false; if (!LBitmap->SaveToTexpiaFile(hFile, cmZLib)) return false; if (LMask) { if (!LMask->SaveToTexpiaFile(hFile, cmZLib)) return false; } return true; } //--------------------------------------------------------------------------- bool __fastcall TPLayer::LoadFromLayerFile(RGBQUAD *rgb) { HANDLE hFile = INVALID_HANDLE_VALUE; if ((hFile = CreateFile(FileName.c_str(), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) goto fail; if (!LoadFromFile(hFile, rgb)) goto fail; CloseHandle(hFile); return true; fail: if (hFile != INVALID_HANDLE_VALUE) CloseHandle(hFile); return false; } //--------------------------------------------------------------------------- bool __fastcall TPLayer::SaveToLayerFile() { HANDLE hFile = INVALID_HANDLE_VALUE; if ((hFile = CreateFile(FileName.c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) goto fail; if (!SaveToFile(hFile)) goto fail; CloseHandle(hFile); LightWeight(); return true; fail: if (hFile != INVALID_HANDLE_VALUE) CloseHandle(hFile); return false; } //--------------------------------------------------------------------------- bool __fastcall TPLayer::LoadFromLayerFile(HANDLE FH, RGBQUAD *rgb) { DWORD dwRead; char *ch = NULL; int size; if (!ReadFile(FH, &size, sizeof(int), &dwRead, NULL)) goto fail; if ((ch = new char[size]) == NULL) goto fail; if (!ReadFile(FH, ch, size, &dwRead, NULL)) goto fail; Name = AnsiString(ch, size); delete[] ch; ch = NULL; FileName = TempLayerFilename(); if (!ReadFile(FH, &Visible, sizeof(bool), &dwRead, NULL)) goto fail; if (!ReadFile(FH, &FLayerType, sizeof(bool), &dwRead, NULL)) goto fail; if (!LoadFromFile(FH, rgb)) goto fail; return true; fail: if (ch) delete[] ch; return false; } //--------------------------------------------------------------------------- bool __fastcall TPLayer::SaveToLayerFile(HANDLE FH, RECT r, RGBQUAD *rgb, bool current) { DWORD dwRead; Byte bpp; int w, h, size; size = Name.Length(); if (!WriteFile(FH, &size, sizeof(int), &dwRead, NULL)) return false; if (!WriteFile(FH, Name.c_str(), size, &dwRead, NULL)) return false; if (!WriteFile(FH, &Visible, sizeof(bool), &dwRead, NULL)) return false; if (!WriteFile(FH, &FLayerType, sizeof(bool), &dwRead, NULL)) return false; if (!current) { if (!LoadFromLayerFile(rgb)) return false; } w = r.right - r.left; h = r.bottom - r.top; bpp = LBitmap->BitsPerPixel; if (!WriteFile(FH, &w, sizeof(int), &dwRead, NULL)) return false; if (!WriteFile(FH, &h, sizeof(int), &dwRead, NULL)) return false; if (!WriteFile(FH, &bpp, sizeof(Byte), &dwRead, NULL)) return false; if (!LBitmap->SaveToTexpiaFile(FH, r, cmZLib)) return false; if (LMask) { if (!LMask->SaveToTexpiaFile(FH, r, cmZLib)) return false; } if (!current) LightWeight(); return true; } //--------------------------------------------------------------------------- bool __fastcall TPLayer::SaveToLayerFile(HANDLE FH, RGBQUAD *rgb, bool current) { DWORD dwWrite; int size; size = Name.Length(); if (!WriteFile(FH, &size, sizeof(int), &dwWrite, NULL)) return false; if (!WriteFile(FH, Name.c_str(), size, &dwWrite, NULL)) return false; if (!WriteFile(FH, &Visible, sizeof(bool), &dwWrite, NULL)) return false; if (!WriteFile(FH, &FLayerType, sizeof(bool), &dwWrite, NULL)) return false; if (!current) { if (!LoadFromLayerFile(rgb)) return false; } if (!SaveToFile(FH)) return false; if (!current) LightWeight(); return true; } //--------------------------------------------------------------------------- void __fastcall TPLayer::LightWeight()//////////100°³ÀÇ ·¹À̾ ½ÇÁ¦ÀûÀÎ ÅØ½ºÇÇ¾Æ ºñÆ®¸ÊÀ» ÇÒ´çÇϱ⿡´Â ¸Þ¸ð¸®ÀÇ ¼Ò¸ð°¡ { // ³Ê¹« ¸¹´Ù µû¶ó¼­ ÇöÀç ¼±ÅÃµÈ ·¹À̾ Á¦¿ÜÇÑ ³ª¸ÓÁö ·¹À̾îÀÇ µ¥ÀÌŸ´Â È­ÀÏ¿¡ ///ÀúÀåµÇ°í °¢°¢ÀÇ ÅØ½ºÇÇ¾Æ ºñÆ®¸ÊÀº Áö¿î´Ù if (LMask) LMask->Destroy(); ///±×·¡¼­ ¹«°Ô¸¦ ´ú¾î Áشٴ ¶æÀ¸·Î LightWeight¶ó°í ÃßÃøÇÑ´Ù--; by jeegeo LBitmap->Destroy(); } //--------------------------------------------------------------------------- void __fastcall TPLayer::UpdateMiniBitmap(RGBQUAD *rgb, RECT rc) { ///////////////////////////////////////////////minibitmap // º¸ÅëÀÇ StretchBlt¿¡¼­ Ãà¼Ò ºñÀ²ÀÌ ³Ê¹« ÀÛÀ¸¸é °Ë°Ô º¯ÇÑ´Ù ///////////////////////////////////////////////minibitmap int mw=500,mh=500; if(MiniBitmap->Handle==NULL){ if (LBitmap->BitsPerPixel == 8) { MiniBitmap->Create(mw, mh, 8, rgb); } else { MiniBitmap->Create(mw, mh, 24); } } HPALETTE hpaintpal = NULL, holdpal = NULL; int pw, ph, vw, vh; HDC dcSrc = NULL, dcDst = MiniBitmap->CreateDC(); if(dcDst==NULL)return; pw = LBitmap->Width; ph = LBitmap->Height; vw = MiniBitmap->Width; vh = MiniBitmap->Height; if (LBitmap->BitsPerPixel==8) { hpaintpal = L_CreatePaintPalette(dcDst, LBitmap->Handle); if (hpaintpal) holdpal = SelectPalette(dcDst, hpaintpal, false); } if ((dcSrc = LBitmap->CreateDC()) == NULL) { if (dcSrc) LBitmap->DeleteDC(dcSrc); if (hpaintpal) { if (holdpal) SelectPalette(dcDst, holdpal, false); DeleteObject(hpaintpal); } } else { SetStretchBltMode(dcDst, COLORONCOLOR); if(rc.left==0&&rc.right==0&&rc.top==0&&rc.bottom==0){ StretchBlt(dcDst, 0, 0, vw, vh, dcSrc, 0, 0, pw, ph, SRCCOPY); } else { if(ph==0||pw==0)return; StretchBlt(dcDst, rc.left*vw/pw, rc.top*vh/ph, (rc.right-rc.left)*vw/pw, (rc.bottom-rc.top)*vh/ph, dcSrc, rc.left, rc.top, (rc.right-rc.left), (rc.bottom-rc.top), SRCCOPY); } LBitmap->DeleteDC(dcSrc); if (hpaintpal) { if (holdpal) SelectPalette(dcDst, holdpal, false); DeleteObject(hpaintpal); } } MiniBitmap->DeleteDC(dcDst); } //--------------------------------------------------------------------------- // TPLayerImage //--------------------------------------------------------------------------- // ValidCtrCheck is used to assure that the components created do not have // any pure virtual functions. // //--------------------------------------------------------------------------- static inline void ValidCtrCheck(TPLayerImage *) { new TPLayerImage(NULL); } //--------------------------------------------------------------------------- __fastcall TPLayerImage::TPLayerImage(TComponent* Owner) : TPImage(Owner) { TPLayer *lay; FBackGroundLock = 0; FIndex = -1; FPositionX = FPositionY = 0; LayerList = new TList; FBeforeBitmap = FAfterBitmap = FAfterMask = NULL; FLayerVisible = true; FBackGroundLock = 0; FOnPaintRange = NULL; FOnPaintDirectly = NULL; FOnPaintVector = NULL; FOnFullViewRectPaint=NULL; FOnLayerGridRectPaint=NULL; FIsActivate = true; lay = new TPLayer("Background", TempLayerFilename(), LTInitial); LayerList->Add(lay); SetIndex(LayerList->Count - 1); ChangeBitmap(lay); } //--------------------------------------------------------------------------- __fastcall TPLayerImage::~TPLayerImage() { TPLayer *lay; while (LayerList->Count) { lay = (TPLayer *) LayerList->Last(); LayerList->Remove(lay); delete lay; } delete LayerList; ::doDestroy(FAfterMask); ::doDestroy(FAfterBitmap); ::doDestroy(FBeforeBitmap); } //--------------------------------------------------------------------------- bool __fastcall TPLayerImage::Create(int width, int height, int bpp, COLORREF bgcolor, RGBQUAD *rgb) { if (!FBitmap->Create(width, height, bpp)) return false; if (bpp == 8) { if (FLayerMask) if (!FLayerMask->Create(width, height, 8)) return false; memcpy(Frgb, rgb, sizeof(RGBQUAD) * 256); ChangeRGBColors(rgb); } else { if (FLayerMask) if (!FLayerMask->Create(width, height, 1)) return false; } FLayerBGColor = bgcolor; MakeBeforeBitmap(); return true; } //--------------------------------------------------------------------------- // ÀÌÇÏ Repaint °ü·Ã //--------------------------------------------------------------------------- #define PAINT_LAYER \ \ if(FBitmap->Width<=pw){ /* ¹èÀ²¿¡ µû¶ó ¼Óµµ°¡ ºü¸¥ ¹æ¹ýÀÌ ´Ù¸£´Ù */ \ \ if (FBeforeBitmap) { \ if ((dcBeforeBitmap = FBeforeBitmap->CreateDC()) == NULL) goto fail; \ if ((bmMemB = CreateCompatibleBitmap(dcBeforeBitmap, pw, ph)) == NULL) goto fail; \ if ((dcSrc = CreateCompatibleDC(dcBeforeBitmap)) == NULL) goto fail; \ SelectObject(dcSrc, bmMemB); \ if (hpaintpal) SelectPalette(dcSrc, hpaintpal, false); \ BitBlt(dcSrc, 0, 0, pw, ph, dcBeforeBitmap, sx, sy, SRCCOPY); \ FBeforeBitmap->DeleteDC(dcBeforeBitmap); dcBeforeBitmap = NULL; \ \ if ((bmMemL = CreateCompatibleBitmap(dcSrc, pw, ph)) == NULL) goto fail; \ if ((dcMemL = CreateCompatibleDC(dcSrc)) == NULL) goto fail; \ SelectObject(dcMemL, bmMemL); \ if (hpaintpal) SelectPalette(dcMemL, hpaintpal, false); \ \ if (FLayerVisible) { \ if ((dcMask = FLayerMask->CreateDC()) == NULL) goto fail; \ BitBlt(dcSrc, 0, 0, pw, ph, dcMask, sx, sy, SRCAND); \ BitBlt(dcMemL, 0, 0, pw, ph, dcMask, sx, sy, NOTSRCCOPY); \ FLayerMask->DeleteDC(dcMask); dcMask = NULL; \ if ((dcBitmap = FBitmap->CreateDC()) == NULL) goto fail; \ BitBlt(dcMemL, 0, 0, pw, ph, dcBitmap, sx, sy, SRCAND); \ BitBlt(dcSrc, 0, 0, pw, ph, dcMemL, 0, 0, SRCPAINT); \ FBitmap->DeleteDC(dcBitmap); dcBitmap = NULL; \ } \ } else { \ if ((dcBitmap = FBitmap->CreateDC()) == NULL) goto fail; \ if ((bmMemB = CreateCompatibleBitmap(dcBitmap, pw, ph)) == NULL) goto fail; \ if ((dcSrc = CreateCompatibleDC(dcBitmap)) == NULL) goto fail; \ SelectObject(dcSrc, bmMemB); \ if (hpaintpal) SelectPalette(dcSrc, hpaintpal, false); \ FBitmap->DeleteDC(dcBitmap); dcBitmap = NULL; \ \ if (FLayerVisible) { \ if ((dcBitmap = FBitmap->CreateDC()) == NULL) goto fail; \ BitBlt(dcSrc, 0, 0, pw, ph, dcBitmap, sx, sy, SRCCOPY); \ FBitmap->DeleteDC(dcBitmap); dcBitmap = NULL; \ } else { \ BitBlt(dcSrc, 0, 0, pw, ph, NULL, 0, 0, WHITENESS); \ } \ } \ \ if (FSubEnabled && FSubVisible) SubPaint(dcSrc, hpaintpal, sx, sy, pw, ph); \ \ if (FAfterBitmap) { \ if(bmMemL==NULL){ \ if ((bmMemL = CreateCompatibleBitmap(dcSrc, pw, ph)) == NULL) goto fail; \ if ((dcMemL = CreateCompatibleDC(dcSrc)) == NULL) goto fail; \ SelectObject(dcMemL, bmMemL); \ if (hpaintpal) SelectPalette(dcMemL, hpaintpal, false); \ } \ if ((dcAfterMask = FAfterMask->CreateDC()) == NULL) goto fail; \ BitBlt(dcSrc, 0, 0, pw, ph, dcAfterMask, sx, sy, SRCAND); \ BitBlt(dcMemL, 0, 0, pw, ph, dcAfterMask, sx, sy, NOTSRCCOPY); \ FAfterMask->DeleteDC(dcAfterMask); dcAfterMask = NULL; \ if ((dcAfterBitmap = FAfterBitmap->CreateDC()) == NULL) goto fail; \ BitBlt(dcMemL, 0, 0, pw, ph, dcAfterBitmap, sx, sy, SRCAND); \ BitBlt(dcSrc, 0, 0, pw, ph, dcMemL, 0, 0, SRCPAINT); \ FAfterBitmap->DeleteDC(dcAfterBitmap); dcAfterBitmap = NULL; \ } \ DeleteDC(dcMemL); dcMemL = NULL; \ DeleteObject(bmMemL); bmMemL = NULL; \ fx = fy = 0; \ \ } else { \ \ temp = new TTexpiaBitmap; \ TTexpiaBitmap *temp2 = new TTexpiaBitmap; \ \ if (FBeforeBitmap) { \ temp->Create(pw, ph, FBeforeBitmap->BitsPerPixel, Frgb); \ temp2->Create(pw, ph, FBeforeBitmap->BitsPerPixel, Frgb); \ temp->FastBitBlt(0, 0, pw, ph, FBeforeBitmap, sx, sy, SRCCOPY); \ if (FLayerVisible) { \ temp->FastBitBlt(0, 0, pw, ph, FLayerMask, sx, sy, SRCAND); \ temp2->FastBitBlt(0, 0, pw, ph, FLayerMask, sx, sy, NOTSRCCOPY); \ temp2->FastBitBlt(0, 0, pw, ph, FBitmap, sx, sy, SRCAND); \ temp->FastBitBlt(0, 0, pw, ph, temp2, 0, 0, SRCPAINT); \ } \ } else { \ temp->Create(pw, ph, FBitmap->BitsPerPixel, Frgb); \ temp->FastBitBlt(0, 0, pw, ph, FBitmap, sx, sy, SRCCOPY); \ if (FLayerVisible) { \ temp->FastBitBlt(0, 0, pw, ph, FBitmap, sx, sy, SRCCOPY); \ } else { \ temp->FastBitBlt(0, 0, pw, ph, NULL, 0, 0, WHITENESS); \ } \ } \ \ dcSrc = temp->CreateDC(); \ if (FSubEnabled && FSubVisible) SubPaint(dcSrc, hpaintpal, sx, sy, pw, ph); \ \ if (FAfterBitmap) { \ if(!temp2->Handle) temp2->Create(pw, ph, FBitmap->BitsPerPixel, Frgb); \ temp->FastBitBlt(0, 0, pw, ph, FAfterMask, sx, sy, SRCAND); \ temp2->FastBitBlt(0, 0, pw, ph, FAfterMask, sx, sy, NOTSRCCOPY); \ temp2->FastBitBlt(0, 0, pw, ph, FAfterBitmap, sx, sy, SRCAND); \ temp->FastBitBlt(0, 0, pw, ph, temp2, 0, 0, SRCPAINT); \ } \ \ delete temp2; temp2 = NULL; \ fx = fy = 0; \ \ } //--------------------------------------------------------------------------- void __fastcall TPLayerImage::paint_init(double &sx, double &sy, int &pw, int &ph, int &vw, int &vh){ sx = FPositionX; sy = FPositionY; 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; } //--------------------------------------------------------------------------- void __fastcall TPLayerImage::paint_init2(BITMAPHANDLE *bh, HDC &dcDst, HPALETTE &hpaintpal, HPALETTE &holdpal, int &vw, int &vh){ dcDst = TGraphicControl::Canvas->Handle; if (FBitmap->BitsPerPixel==8) { hpaintpal = L_CreatePaintPalette(dcDst, bh); if (hpaintpal) holdpal = SelectPalette(dcDst, hpaintpal, false); } TGraphicControl::Canvas->Brush->Color = FColor; if (vwFillRect(Rect(vw, 0, Width, vh)); if (vhFillRect(Rect(0, vh, Width, Height)); } //--------------------------------------------------------------------------- #define PAINT_INIT \ paint_init(sx, sy, pw, ph, vw, vh); \ paint_init2(bh, dcDst, hpaintpal, holdpal, vw, vh); //--------------------------------------------------------------------------- void __fastcall TPLayerImage::rectpaint_init(RECT &Src, double &sx, double &sy, int &px, int &py, int &pw, int &ph, int &vw, int &vh){ try { sx = FPositionX; sy = FPositionY; 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 && FZoomIn == 1) { Src.left -= Src.left%FZoomOut; Src.top -= Src.top%FZoomOut; Src.right -= Src.right%FZoomOut; Src.bottom -= Src.bottom%FZoomOut; } if (FZoomIn>1 && FZoomOut != 1) { Src.left -= Src.left%FZoomOut; Src.top -= Src.top%FZoomOut; Src.right += Src.right%FZoomOut; 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 = Src.top*FZoomIn/FZoomOut; sx += Src.left; sy += Src.top; } catch (...) { Src.left = 0; Src.top = 0; Src.right = 0; Src.bottom = 0; px = 0; py = 0; } } //--------------------------------------------------------------------------- void __fastcall TPLayerImage::rectpaint_init2(BITMAPHANDLE *bh, HDC &dcDst, HPALETTE &hpaintpal, HPALETTE &holdpal, int &vw, int &vh){ dcDst = TGraphicControl::Canvas->Handle; if (FBitmap->BitsPerPixel==8) { hpaintpal = L_CreatePaintPalette(dcDst, bh); if (hpaintpal) holdpal = SelectPalette(dcDst, hpaintpal, false); } } //--------------------------------------------------------------------------- #define RECTPAINT_INIT \ rectpaint_init(Src, sx, sy, px, py, pw, ph, vw, vh); \ rectpaint_init2(bh, dcDst, hpaintpal, holdpal, vw, vh); //--------------------------------------------------------------------------- #define PAINT_END \ \ if (FAfterBitmap) { \ if (dcAfterBitmap) { \ FAfterBitmap->DeleteDC(dcAfterBitmap); \ dcAfterBitmap=NULL; \ } \ } \ if (FAfterMask) { \ if (dcAfterMask) { \ FAfterMask->DeleteDC(dcAfterMask); \ dcAfterMask=NULL; \ } \ } \ if (FBeforeBitmap) { \ if (dcBeforeBitmap) { \ FBeforeBitmap->DeleteDC(dcBeforeBitmap); \ dcBeforeBitmap=NULL; \ } \ } \ if (dcMask) { \ FLayerMask->DeleteDC(dcMask); \ dcMask=NULL; \ } \ if (dcBitmap) { \ FBitmap->DeleteDC(dcBitmap); \ dcBitmap=NULL; \ } \ if (dcMSrc) { \ FMask->DeleteDC(dcMSrc); \ dcMSrc=NULL; \ } \ if (bmMemL) { \ if (dcMemL) { \ DeleteDC(dcMemL); \ dcMemL=NULL; \ } \ DeleteObject(bmMemL); \ bmMemL=NULL; \ } \ if (bmMemV) { \ if (dcMemV) { \ DeleteDC(dcMemV); \ dcMemV=NULL; \ } \ DeleteObject(bmMemV); \ bmMemV=NULL; \ } \ if(temp) { \ if(dcSrc) { \ temp->DeleteDC(dcSrc); \ dcSrc=NULL; \ } \ delete temp; temp = NULL; \ } else { \ if (bmMemB) { \ if (dcSrc) { \ DeleteDC(dcSrc); \ dcSrc=NULL; \ } \ DeleteObject(bmMemB); \ bmMemB=NULL; \ } \ } \ if (hpaintpal) { \ if (holdpal) SelectPalette(dcDst, holdpal, false); \ DeleteObject(hpaintpal); \ hpaintpal=NULL; \ } //--------------------------------------------------------------------------- //--------------------------------------------------------------------------- //--------------------------------------------------------------------------- void __fastcall TPLayerImage::Paint() { Update(); /* HDC dc; if (ComponentState.Contains(csDesigning)) { TGraphicControl::Canvas->Pen->Style = psDash; TGraphicControl::Canvas->Brush->Style = bsClear; TGraphicControl::Canvas->Rectangle(0, 0, Width, Height); } else { if (Width<=0 || Height<=0) return; if (FBackGround&&FBackGround->Handle) { dc = FBackGround->CreateDC(); if (dc) { BitBlt(TGraphicControl::Canvas->Handle, 0, 0, FBackGround->Width, FBackGround->Height, dc, 0, 0, SRCCOPY); FBackGround->DeleteDC(dc); } } } */ } //--------------------------------------------------------------------------- void __fastcall TPLayerImage::Update() { BITMAPHANDLE *bh; HDC dc; if (ComponentState.Contains(csDesigning)) { TGraphicControl::Canvas->Pen->Style = psDash; TGraphicControl::Canvas->Brush->Style = bsClear; TGraphicControl::Canvas->Rectangle(0, 0, Width, Height); } else { if (Width<=0 || Height<=0) return; if (FIsActivate||LayerList->Count<=1) { // FBackGround´Â ·¹À̾ 2°³ ÀÌ»óÀ϶§ NULLÀÌ ¾Æ´Ñ °ªÀ» °¡Áø´Ù bh = FBitmap->Handle; if (bh) { PaintTop(bh); } } else { if (FBackGround&&FBackGround->Handle) { dc = FBackGround->CreateDC(); if (dc) { BitBlt(TGraphicControl::Canvas->Handle, 0, 0, FBackGround->Width, FBackGround->Height, dc, 0, 0, SRCCOPY); FBackGround->DeleteDC(dc); } } } } } //--------------------------------------------------------------------------- //--------------------------------------------------------------------------- //--------------------------------------------------------------------------- void __fastcall TPLayerImage::PaintTop(BITMAPHANDLE *bh) { BITMAPHANDLE *mh = NULL; HPALETTE hpaintpal = NULL, holdpal = NULL; int px=0,py=0; int pw, ph, vw, vh, cnt = LayerList->Count; double sx, sy, gx, gy, gzx, gzy, p, fx, fy; HDC dcSrc = NULL, dcDst, dcMemV = NULL, dcMSrc = NULL, dcBeforeBitmap = NULL, dcBitmap = NULL, dcMask = NULL, dcMemL = NULL, dcAfterBitmap = NULL, dcAfterMask = NULL, dctemp = NULL; HBITMAP bmMemV = NULL, bmMemB = NULL, bmMemL = NULL; HPEN hPen = NULL, hOldPen; TTexpiaBitmap *temp = NULL; PAINT_INIT; PAINT_LAYER; if (FMaskEnabled) { mh = FMask->Handle; } 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, fx, fy, pw, ph, SRCCOPY); if (FMaskEnabled&&mh) { if ((dcMSrc = FMask->CreateDC()) == NULL) goto fail; StretchBlt(dcMemV, 0, 0, vw, vh, dcMSrc, sx, sy, pw, ph, SRCINVERT); FMask->DeleteDC(dcMSrc); dcMSrc = NULL; } if (FOnPaintRange) FOnPaintRange(this, dcMemV, 0, 0); // if (FOnPaintZoom) FOnPaintZoom(this, dcMemV); if (FGrid) { if (!Paint_GridLine(dcMemV, sx, sy, vw, vh)) goto fail; } if (FOnPaintVector) FOnPaintVector(dcMemV, 0, 0, 0, 0); /* ///////////////////////////// FBackGround if(FBackGround==NULL){ if ((FBackGround = new TTexpiaBitmap) == NULL) goto fail; } if(FBackGround->Width!=vw||FBackGround->Height!=vh){ if (FBitmap->BitsPerPixel == 8) { if (!FBackGround->Create(vw, vh, 8, Frgb)) goto fail; } else { if (!FBackGround->Create(vw, vh, 24)) goto fail; } } if(FBackGround&&FBackGround->Handle){ HDC dc = FBackGround->CreateDC(); BitBlt(dc, 0, 0, vw, vh, dcMemV, 0, 0, SRCCOPY); FBackGround->DeleteDC(dc); } ///////////////////////////// */ BitBlt(dcDst, px, py, vw, vh, dcMemV, 0, 0, SRCCOPY); if(FOnPaintDirectly)FOnPaintDirectly(dcDst, px, py, vw, vh); //FOnPaint´Â TPStretchImage¿¡¼­´Â ¾²ÀÌÁö¸¸ ¿©±â¼­´Â »ç½Ç»ó ÇÊ¿ä¾ø´Ù if (FOnPaint) FOnPaint(this); PAINT_END; return; fail: PAINT_END; } //--------------------------------------------------------------------------- bool __fastcall TPLayerImage::RectPaint(RECT Src, bool ShowOnly) { BITMAPHANDLE *bh, *mh = NULL; HPALETTE hpaintpal = NULL, holdpal = NULL; int px, py, pw, ph, vw, vh, cnt = LayerList->Count; double sx, sy, gx, gy, gzx, gzy, p, fx, fy; HDC dcSrc = NULL, dcDst, dcMemV = NULL, dcMSrc = NULL, dcBeforeBitmap = NULL, dcMemL = NULL, dcBitmap = NULL, dcMask = NULL, dcAfterBitmap = NULL, dcAfterMask = NULL, dctemp = NULL; HBITMAP bmMemV = NULL, bmMemB = NULL, bmMemL = NULL; HPEN hPen = NULL, hOldPen; TTexpiaBitmap *temp = NULL; bh = FBitmap->Handle; if(!bh) return false; if (FMaskEnabled) { mh = FMask->Handle; } if (Src.leftWidth && Src.right>=FPositionX && Src.topHeight && Src.bottom>=FPositionY) { RECTPAINT_INIT; PAINT_LAYER; 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, fx, fy, pw, ph, SRCCOPY); if(FMaskEnabled&&mh){ if ((dcMSrc = FMask->CreateDC()) == NULL) goto fail; StretchBlt(dcMemV, 0, 0, vw, vh, dcMSrc, sx, sy, pw, ph, SRCINVERT); FMask->DeleteDC(dcMSrc); dcMSrc = NULL; } if (FOnPaintRange) FOnPaintRange(this, dcMemV, px, py); //if (FOnPaintZoom) FOnPaintZoom(this, dcMemV); if (FGrid) { if (!Paint_GridLine(dcMemV, sx, sy, vw, vh)) goto fail; } if (FOnPaintVector) FOnPaintVector(dcMemV, px, py, 0, 0); BitBlt(dcDst, px, py, vw, vh, dcMemV, 0, 0, SRCCOPY); if(FOnLayerGridRectPaint){ if ((dcBitmap = FBitmap->CreateDC()) == NULL) goto fail; FOnLayerGridRectPaint(dcBitmap,sx,sy,pw,ph);//layergrid ¾÷µ¥ÀÌÆ® FBitmap->DeleteDC(dcBitmap); dcBitmap = NULL; } if(!ShowOnly){ if (!FSubEnabled||!FSubVisible){ if(FOnFullViewRectPaint)FOnFullViewRectPaint(dcMemV,sx,sy,pw,ph);//fullview ¾÷µ¥ÀÌÆ® } } PAINT_END; } return true; fail: PAINT_END; return false; } //---------------------------------------------------------------------------- bool __fastcall TPLayerImage::RectEditPaint(RECT Src, TRectEditPaint func, void *pData) { BITMAPHANDLE *bh, *mh = NULL; HPALETTE hpaintpal = NULL, holdpal = NULL; int px, py, pw, ph, vw, vh, cnt = LayerList->Count; double sx, sy, gx, gy, gzx, gzy, p, fx, fy; HDC dcSrc = NULL, dcDst, dcMemP = NULL, dcMemV = NULL, dcMSrc = NULL, dcBeforeBitmap = NULL, dcBitmap = NULL, dcMask = NULL, dcMemL = NULL, dcAfterBitmap = NULL, dcAfterMask = NULL, dctemp = NULL; HBITMAP bmMemP = NULL, bmMemV = NULL, bmMemB = NULL, bmMemL = NULL; HPEN hPen = NULL, hOldPen; TTexpiaBitmap *temp = NULL; bh = FBitmap->Handle; if(!bh) return false; if (FMaskEnabled) { mh = FMask->Handle; } if (Src.leftWidth && Src.right>=FPositionX && Src.topHeight && Src.bottom>=FPositionY) { RECTPAINT_INIT; PAINT_LAYER; 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); 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, fx, fy, SRCCOPY); if(FMaskEnabled&&mh){ if ((dcMSrc = FMask->CreateDC()) == NULL) goto fail; StretchBlt(dcMemV, 0, 0, vw, vh, dcMSrc, sx, sy, pw, ph, SRCINVERT); FMask->DeleteDC(dcMSrc); dcMSrc = NULL; } if (func) func(dcMemP, sx, sy, pData); SetStretchBltMode(dcMemV, COLORONCOLOR); StretchBlt(dcMemV, 0, 0, vw, vh, dcMemP, 0, 0, pw, ph, SRCCOPY); if (bmMemP) { if (dcMemP) { DeleteDC(dcMemP); dcMemP = NULL; } DeleteObject(bmMemP); bmMemP = NULL; } if (FOnPaintRange) FOnPaintRange(this, dcMemV, px, py); //if (FOnPaintZoom) FOnPaintZoom(this, dcMemV); if (FGrid) { if (!Paint_GridLine(dcMemV, sx, sy, vw, vh)) goto fail; } if (FOnPaintVector) FOnPaintVector(dcMemV, px, py, 0, 0); BitBlt(dcDst, px, py, vw, vh, dcMemV, 0, 0, SRCCOPY); if(FOnLayerGridRectPaint){ if ((dcBitmap = FBitmap->CreateDC()) == NULL) goto fail; FOnLayerGridRectPaint(dcBitmap,sx,sy,pw,ph);//layergrid ¾÷µ¥ÀÌÆ® FBitmap->DeleteDC(dcBitmap); dcBitmap = NULL; } //if (!FSubEnabled||!FSubVisible){ // if(FOnFullViewRectPaint)FOnFullViewRectPaint(dcMemV,sx,sy,pw,ph);//fullview ¾÷µ¥ÀÌÆ® //} PAINT_END; } return true; fail: if (bmMemP) { if (dcMemP) { DeleteDC(dcMemP); dcMemP = NULL; } DeleteObject(bmMemP); bmMemP = NULL; } PAINT_END; return false; } //--------------------------------------------------------------------------- void __fastcall TPLayerImage::PaintImageEdit(TEditPaintEvent func) { BITMAPHANDLE *bh; HPALETTE hpaintpal = NULL, holdpal = NULL; int pw, ph, vw, vh; HDC dcDst, dcMem = NULL; HBITMAP bmMem = NULL; bh = FBitmap->Handle; if (bh) { dcDst = TGraphicControl::Canvas->Handle; if (FBitmap->BitsPerPixel==8) { hpaintpal = L_CreatePaintPalette(dcDst, bh); if (hpaintpal) holdpal = SelectPalette(dcDst, hpaintpal, false); } pw = Width*FZoomOut/FZoomIn; ph = Height*FZoomOut/FZoomIn; if (pw>FBitmap->Width-FPositionX) pw = FBitmap->Width-FPositionX; if (ph>FBitmap->Height-FPositionY) ph = FBitmap->Height-FPositionY; if (FZoomOut>1) { pw -= pw%FZoomOut; ph -= ph%FZoomOut; } vw = pw*FZoomIn/FZoomOut; vh = ph*FZoomIn/FZoomOut; if ((bmMem = CreateCompatibleBitmap(dcDst, vw, vh)) == NULL) goto fail; if ((dcMem = CreateCompatibleDC(dcDst)) == NULL) goto fail; SelectObject(dcMem, bmMem); if (hpaintpal) SelectPalette(dcMem, hpaintpal, false); BitBlt(dcMem, 0, 0, vw, vh, dcDst, 0, 0, SRCCOPY); if (func) func(this, dcMem); BitBlt(dcDst, 0, 0, vw, vh, dcMem, 0, 0, SRCCOPY); DeleteDC(dcMem); DeleteObject(bmMem); if (hpaintpal) { if (holdpal) SelectPalette(dcDst, holdpal, false); DeleteObject(hpaintpal); } } return; fail: if (bmMem) { if (dcMem) DeleteDC(dcMem); DeleteObject(bmMem); } if (hpaintpal) { if (holdpal) SelectPalette(dcDst, holdpal, false); DeleteObject(hpaintpal); } } //--------------------------------------------------------------------------- // fullviewÀÇ viewÇÔ¼ö¿¡¼­ È£ÃâµÈ´Ù// ³Ñ°Ü¹ÞÀº dcDst¿¡ Àüüº¸±â À̹ÌÁö¸¦ ±×·ÁÁØ´Ù void __fastcall TPLayerImage::FullViewPaint(HDC dcDst, int zi, int zo) { BITMAPHANDLE *bh; HPALETTE hpaintpal = NULL, holdpal = NULL; int pw, ph, vw, vh, cnt = LayerList->Count; double sx=0, sy=0, gx, gy, gzx, gzy, p, fx=0, fy=0; HDC dcSrc = NULL, dcBeforeBitmap = NULL, dcBitmap = NULL, dcMask = NULL, dcMemL = NULL, dcAfterBitmap = NULL, dcAfterMask = NULL, dctemp = NULL, dcMemV = NULL, dcMSrc = NULL; HBITMAP bmMemB = NULL, bmMemL = NULL, bmMemV = NULL; TTexpiaBitmap *temp = NULL; vw = FBitmap->Width * zi / zo; vh = FBitmap->Height * zi / zo; pw = vw * zo / zi; ph = vh * zo / zi; bh = FBitmap->Handle; if(!bh) return; if (FBitmap->BitsPerPixel==8) { hpaintpal = L_CreatePaintPalette(dcDst, FBitmap->Handle); if (hpaintpal) holdpal = SelectPalette(dcDst, hpaintpal, false); } PAINT_LAYER; SetStretchBltMode(dcDst, COLORONCOLOR); StretchBlt(dcDst, 0, 0, vw, vh, dcSrc, 0, 0, pw, ph, SRCCOPY); PAINT_END; return; fail: PAINT_END; } //--------------------------------------------------------------------------- bool __fastcall TPLayerImage::Paint_GridLine(HDC dcMemV, double sx, double sy, int vw, int vh) { double gx, gy, gzx, gzy, p; HPEN hPen = NULL, hOldPen = NULL; gx = FGridGapX; gy = FGridGapY; gzx = gx*FZoomIn/FZoomOut; gzy = gy*FZoomIn/FZoomOut; if (gzx>=5 && gzy>=5) { SetROP2(dcMemV, R2_COPYPEN); if ((hPen = CreatePen(psSolid, 1, FGridColor)) == NULL) goto fail; hOldPen = (HPEN)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 = (HPEN)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 (p FSubRange.left) { dsx = 0; ssx = sx; } else { dsx = FSubRange.left - sx; ssx = FSubRange.left; } if (sy > FSubRange.top) { dsy = 0; ssy = sy; } else { dsy = FSubRange.top - sy; ssy = FSubRange.top; } psw = FSubRange.right - ssx; psh = FSubRange.bottom - ssy; if (psw > pw - dsx) psw = pw - dsx; if (psh > ph - dsy) psh = ph - dsy; } else { // the others Expression if (sx > FSubRange.left) { dsx = 0; ssx = sx - FSubRange.left; } else { dsx = FSubRange.left - sx; ssx = 0; } if (sy > FSubRange.top) { dsy = 0; ssy = sy - FSubRange.top; } else { dsy = FSubRange.top - sy; ssy = 0; } psw = FSubBitmap->Width - ssx; psh = FSubBitmap->Height - ssy; if (psw > FBitmap->Width - ssx) psw = FBitmap->Width - ssx; if (psh > FBitmap->Height - ssy) psh = FBitmap->Height - ssy; } if ((dcSubSrc = FSubBitmap->CreateDC()) == NULL) goto fail; if ((dcSubMask = FSubMask->CreateDC()) == NULL) goto fail; SetStretchBltMode(dcSrc, COLORONCOLOR); StretchBlt(dcSrc, dsx, dsy, psw, psh, dcSubMask, ssx, ssy, psw, psh, SRCAND); if ((bmMemT = CreateCompatibleBitmap(dcSrc, psw, psh)) == NULL) goto fail; if ((dcMemT = CreateCompatibleDC(dcSrc)) == NULL) goto fail; SelectObject(dcMemT, bmMemT); if (hpaintpal) SelectPalette(dcMemT, hpaintpal, false); SetStretchBltMode(dcMemT, COLORONCOLOR); StretchBlt(dcMemT, 0, 0, psw, psh, dcSubMask, ssx, ssy, psw, psh, NOTSRCCOPY); StretchBlt(dcMemT, 0, 0, psw, psh, dcSubSrc, ssx, ssy, psw, psh, SRCAND); BitBlt(dcSrc, dsx, dsy, psw, psh, dcMemT, 0, 0, SRCPAINT); DeleteDC(dcMemT); DeleteObject(bmMemT); FSubMask->DeleteDC(dcSubMask); FSubBitmap->DeleteDC(dcSubSrc); return; fail: if (bmMemT) { if (dcMemT) DeleteDC(dcMemT); DeleteObject(bmMemT); } if (dcSubMask) FSubMask->DeleteDC(dcSubMask); if (dcSubSrc) FSubBitmap->DeleteDC(dcSubSrc); } //--------------------------------------------------------------------------- // ÀÛ¾÷±¸¿ª °ü·Ã //--------------------------------------------------------------------------- // È­¸é¿¡ ÀÛ¾÷±¸¿ªÀ» ±×·ÁÁØ´Ù void __fastcall TPLayerImage::OutlineBitmapRgn(bool bComplex) { pBITMAPHANDLE bh; RGNXFORM XForm; HRGN rgn; HBRUSH hBrush; if (FBitmap->IsUse) return; bh = FBitmap->Handle; if (bh) { if (L_BitmapHasRgn(bh)) { 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; XForm.nYOffset = -FPositionY*FZoomIn/FZoomOut; if (bComplex) { L_FrameBitmapRgn(TGraphicControl::Canvas->Handle, bh, &XForm, nFrameRgn); } else { if (L_GetBitmapRgnHandle(bh, &XForm, &rgn) == SUCCESS){ if ((hBrush = CreatePatternBrush(bmFrameRgn[nFrameRgn])) != NULL) { FrameRgn(TGraphicControl::Canvas->Handle, rgn, hBrush, 1, 1); DeleteObject(hBrush); } DeleteObject(rgn); } } } } } //--------------------------------------------------------------------------- // ³Ñ°Ü¹ÞÀº ºñÆ®¸Ê¿¡ ÀÛ¾÷±¸¿ªÀ» ±×·ÁÁØ´Ù void __fastcall TPLayerImage::WAOutlineBitmapRgn(TTexpiaBitmap *bm, bool bComplex) { pBITMAPHANDLE bh; RGNXFORM XForm; HRGN rgn; HBRUSH hBrush; if (bm->IsUse) return; bh = bm->Handle; if (bh) { if (L_BitmapHasRgn(bh)) { nFrameRgn = (nFrameRgn+1)&7; XForm.uViewPerspective = TOP_LEFT; XForm.nXScalarNum = 1;//FZoomIn; XForm.nXScalarDen = 1;//FZoomOut; XForm.nYScalarNum = 1;//FZoomIn; XForm.nYScalarDen = 1;//FZoomOut; XForm.nXOffset = 0;//-FPositionX*FZoomIn/FZoomOut; XForm.nYOffset = 0;//-FPositionY*FZoomIn/FZoomOut; if (bComplex) { HDC dc=bm->CreateDC(); L_FrameBitmapRgn(dc, bh, &XForm, nFrameRgn); bm->DeleteDC(dc); } else { if (L_GetBitmapRgnHandle(bh, &XForm, &rgn) == SUCCESS){ if ((hBrush = CreatePatternBrush(bmFrameRgn[nFrameRgn])) != NULL) { HDC dc=bm->CreateDC(); FrameRgn(dc, rgn, hBrush, 1, 1); bm->DeleteDC(dc); DeleteObject(hBrush); } DeleteObject(rgn); } } } } } //--------------------------------------------------------------------------- // ·¹ÀÌ¾î ¸®½ºÆ® °ü·Ã //--------------------------------------------------------------------------- //À̹ÌÁö°¡ ÀÖ´Â formÀÌ È°¼ºÈ­ µÉ¶§ È£ÃâÇØ¾ß ÇÑ´Ù. bool __fastcall TPLayerImage::Activate() { TPLayer *lay; if (LayerList->Count > 1) { ::doDestroy(FBackGround); lay = (TPLayer *)LayerList->Items[FIndex]; lay->LoadFromLayerFile(Frgb); MakeBeforeBitmap(); if (FBackGroundLock) { if ((FBackGround = new TTexpiaBitmap) == NULL) goto fail; if (!MakeBackGround()) goto fail; } } FIsActivate = true; return true; fail: ::doDestroy(FBackGround); return false; } //--------------------------------------------------------------------------- //À̹ÌÁö°¡ ÀÖ´Â formÀÌ ºñȰ¼ºÈ­ µÉ¶§ È£ÃâÇØ¾ß ÇÑ´Ù. bool __fastcall TPLayerImage::Deactivate() { BITMAPHANDLE *bh, *mh; HPALETTE hpaintpal = NULL, holdpal = NULL; int pw, ph, vw, vh, n; double sx, sy, gx, gy, gzx, gzy, p, fx, fy; HDC dcSrc = NULL, dcDst, dcMemV = NULL, dcMSrc = NULL, dcBeforeBitmap = NULL, dcBitmap = NULL, dcMask = NULL, dcMemL = NULL, dcAfterBitmap = NULL, dcAfterMask = NULL, dctemp = NULL, dcBackGround = NULL; HBITMAP bmMemB = NULL, bmMemL = NULL, bmMemV = NULL; HPEN hPen = NULL, hOldPen; RGBQUAD r[256]; TPLayer *lay; TTexpiaBitmap *temp = NULL; FIsActivate = false; if (LayerList->Count > 1) { if (FBackGroundLock) { ::doDestroy(FBackGround); } bh = FBitmap->Handle; if (bh) { if (FMaskEnabled) { mh = FMask->Handle; if (mh) { PAINT_INIT; PAINT_LAYER; if ((FBackGround = new TTexpiaBitmap) == NULL) goto fail; if (FBitmap->BitsPerPixel == 8) { FBitmap->GetColors(0, 256, r); if (!FBackGround->Create(vw, vh, 8, r)) goto fail; } else { if (!FBackGround->Create(vw, vh, 24)) goto fail; } if ((dcBackGround = FBackGround->CreateDC()) == NULL) goto fail; SetStretchBltMode(dcBackGround, COLORONCOLOR); StretchBlt(dcBackGround, 0, 0, vw, vh, dcSrc, fx, fy, pw, ph, SRCCOPY); if ((dcMSrc = FMask->CreateDC()) == NULL) goto fail; StretchBlt(dcBackGround, 0, 0, vw, vh, dcMSrc, sx, sy, pw, ph, SRCINVERT); FMask->DeleteDC(dcMSrc); dcMSrc = NULL; if (FGrid) { if (!Paint_GridLine(dcBackGround, sx, sy, vw, vh)) goto fail; } FBackGround->DeleteDC(dcBackGround); PAINT_END; } } else { PAINT_INIT; PAINT_LAYER; if ((FBackGround = new TTexpiaBitmap) == NULL) goto fail; if (FBitmap->BitsPerPixel == 8) { FBitmap->GetColors(0, 256, r); if (!FBackGround->Create(vw, vh, 8, r)) goto fail; } else { if (!FBackGround->Create(vw, vh, 24)) goto fail; } if ((dcBackGround = FBackGround->CreateDC()) == NULL) goto fail; SetStretchBltMode(dcBackGround, COLORONCOLOR); StretchBlt(dcBackGround, 0, 0, vw, vh, dcSrc, fx, fy, pw, ph, SRCCOPY); if (FGrid) { if (!Paint_GridLine(dcBackGround, sx, sy, vw, vh)) goto fail; } FBackGround->DeleteDC(dcBackGround); PAINT_END; } } ::doDestroy(FBeforeBitmap); ::doDestroy(FAfterBitmap); ::doDestroy(FAfterMask); lay = (TPLayer *)LayerList->Items[FIndex]; lay->SaveToLayerFile(); } return true; fail: PAINT_END; if (FBackGround) { if (dcBackGround) FBackGround->DeleteDC(dcBackGround); delete FBackGround; FBackGround = NULL; } return false; } //--------------------------------------------------------------------------- void __fastcall TPLayerImage::MergeVisibleList() { TPLayer *dstlay, *srclay; int i, j, k, viscnt = 0, cnt = LayerList->Count; Byte vis[MAX_LAYER_NUM]; memset(vis, 0, 100); for (i = cnt - 1; i >= 0; i--) { srclay = (TPLayer *) LayerList->Items[i]; if (srclay->Visible) { vis[viscnt] = i; viscnt++; } } // »èÁ¦Àü¿¡ Region copy... k = vis[viscnt - 1]; dstlay = (TPLayer *) LayerList->Items[k]; dstlay->LoadFromLayerFile(Frgb); CopyRGN(dstlay); // »èÁ¦ for (i = 0; i < viscnt - 1; i++) { j = vis[i]; srclay = (TPLayer *) LayerList->Items[j]; if (srclay->Visible) { LayerList->Remove(srclay); delete srclay; } } // Àç¼³Á¤ ChangeBitmap(dstlay); if (k != FIndex) dstlay->LoadFromLayerFile(Frgb); FIndex = k; MakeBeforeBitmap(); } //--------------------------------------------------------------------------- void __fastcall TPLayerImage::FlattenList() { TPLayer *dstlay, *srclay; dstlay = (TPLayer *) LayerList->Items[0]; dstlay->LoadFromLayerFile(Frgb); CopyRGN(dstlay); while (LayerList->Count > 1) { srclay = (TPLayer *) LayerList->Last(); LayerList->Remove(srclay); delete srclay; } ChangeBitmap(dstlay); FIndex = 0; MakeBeforeBitmap(); } //--------------------------------------------------------------------------- //ÇöÀç ¼³Á¤µÈ layerÀÇ ÀÌÀü layer¸¦ ¸ðµÎ ÇÕ¼º½ÃÄѼ­ ÇϳªÀÇ bitmapÀ» ¸¸µç´Ù. bool __fastcall TPLayerImage::MakeBeforeBitmap() { int i, cnt, w, h, bpp; TPLayer *lay; AnsiString fn; BITMAPHANDLE *bh = FBitmap->Handle; HBITMAP bmLayer = NULL; HPALETTE hpal = NULL; HDC dcBeforeBitmap = NULL, dcAfterBitmap = NULL, dcAfterMask = NULL, dcBitmap = NULL, dcMask = NULL, dcMem = NULL; cnt = LayerList->Count; if (cnt > 1) { w = FBitmap->Width; h = FBitmap->Height; if (FIndex > 0) { if (FBeforeBitmap == NULL) { if ((FBeforeBitmap = new TTexpiaBitmap) == NULL) goto fail; } if (FBitmap->BitsPerPixel == 8) { if (!FBeforeBitmap->Create(w, h, 8, Frgb)) goto fail; FBeforeBitmap->FillRect(Rect(0, 0, w, h), PALETTERGB(Frgb[1].rgbRed, Frgb[1].rgbGreen, Frgb[1].rgbBlue)); } else { if (!FBeforeBitmap->Create(w, h, 24)) goto fail; FBeforeBitmap->FillRect(Rect(0, 0, w, h), FLayerBGColor); } if ((dcBeforeBitmap = FBeforeBitmap->CreateDC()) == NULL) goto fail; if (FBeforeBitmap->BitsPerPixel == 8) hpal = L_CreatePaintPalette(dcBeforeBitmap, bh); lay = (TPLayer *) LayerList->First(); if (lay->Visible) { lay->LoadFromLayerFile(Frgb); if ((dcBitmap = lay->LBitmap->CreateDC()) == NULL) goto fail; BitBlt(dcBeforeBitmap, 0, 0, w, h, dcBitmap, 0, 0, SRCCOPY); lay->LBitmap->DeleteDC(dcBitmap); dcBitmap = NULL; lay->LightWeight(); } if ((bmLayer = CreateCompatibleBitmap(dcBeforeBitmap, w, h)) == NULL) goto fail; if ((dcMem = CreateCompatibleDC(dcBeforeBitmap)) == NULL) goto fail; SelectObject(dcMem, bmLayer); if (hpal) SelectPalette(dcMem, hpal, false); for (i = 1; i < FIndex; i++) { lay = (TPLayer *) LayerList->Items[i]; if (lay->Visible) { lay->LoadFromLayerFile(Frgb); if ((dcMask = lay->LMask->CreateDC()) == NULL) goto fail; BitBlt(dcBeforeBitmap, 0, 0, w, h, dcMask, 0, 0, SRCAND); BitBlt(dcMem, 0, 0, w, h, dcMask, 0, 0, NOTSRCCOPY); lay->LMask->DeleteDC(dcMask); dcMask = NULL; if ((dcBitmap = lay->LBitmap->CreateDC()) == NULL) goto fail; BitBlt(dcMem, 0, 0, w, h, dcBitmap, 0, 0, SRCAND); BitBlt(dcBeforeBitmap, 0, 0, w, h, dcMem, 0, 0, SRCPAINT); lay->LBitmap->DeleteDC(dcBitmap); dcBitmap = NULL; lay->LightWeight(); } } DeleteDC(dcMem); dcMem = NULL; DeleteObject(bmLayer); bmLayer = NULL; if (hpal) { DeleteObject(hpal); hpal = NULL; } FBeforeBitmap->DeleteDC(dcBeforeBitmap); dcBeforeBitmap = NULL; } else { ::doDestroy(FBeforeBitmap); } if (FIndex < cnt - 1) { if (FAfterBitmap == NULL) { if ((FAfterBitmap = new TTexpiaBitmap) == NULL) goto fail; } if (FAfterMask == NULL) { if ((FAfterMask = new TTexpiaBitmap) == NULL) goto fail; } if (FBitmap->BitsPerPixel == 8) { if (!FAfterBitmap->Create(FBitmap->Width, FBitmap->Height, 8, Frgb)) goto fail; FAfterBitmap->FillRect(Rect(0, 0, w, h), PALETTERGB(Frgb[1].rgbRed, Frgb[1].rgbGreen, Frgb[1].rgbBlue)); if (!FAfterMask->Create(FBitmap->Width, FBitmap->Height, 8, Frgb)) goto fail; FAfterMask->FillRect(Rect(0, 0, w, h), PALETTEINDEX(255)); } else { if (!FAfterBitmap->Create(FBitmap->Width, FBitmap->Height, 24)) goto fail; FAfterBitmap->FillRect(Rect(0, 0, w, h), FLayerBGColor); if (!FAfterMask->Create(FBitmap->Width, FBitmap->Height, 1)) goto fail; FAfterMask->FillRect(Rect(0, 0, w, h), clWhite); } if ((dcAfterBitmap = FAfterBitmap->CreateDC()) == NULL) goto fail; if (FAfterBitmap->BitsPerPixel == 8) hpal = L_CreatePaintPalette(dcAfterBitmap, bh); if ((dcAfterMask = FAfterMask->CreateDC()) == NULL) goto fail; if ((bmLayer = CreateCompatibleBitmap(dcAfterBitmap, w, h)) == NULL) goto fail; if ((dcMem = CreateCompatibleDC(dcAfterBitmap)) == NULL) goto fail; SelectObject(dcMem, bmLayer); if (hpal) SelectPalette(dcMem, hpal, false); for (i = FIndex + 1; i < cnt; i++) { lay = (TPLayer *) LayerList->Items[i]; if (lay->Visible) { lay->LoadFromLayerFile(Frgb); if ((dcMask = lay->LMask->CreateDC()) == NULL) goto fail; BitBlt(dcAfterBitmap, 0, 0, w, h, dcMask, 0, 0, SRCAND); BitBlt(dcMem, 0, 0, w, h, dcMask, 0, 0, NOTSRCCOPY); BitBlt(dcAfterMask, 0, 0, w, h, dcMask, 0, 0, SRCAND); lay->LMask->DeleteDC(dcMask); dcMask = NULL; if ((dcBitmap = lay->LBitmap->CreateDC()) == NULL) goto fail; BitBlt(dcMem, 0, 0, w, h, dcBitmap, 0, 0, SRCAND); BitBlt(dcAfterBitmap, 0, 0, w, h, dcMem, 0, 0, SRCPAINT); lay->LBitmap->DeleteDC(dcBitmap); dcBitmap = NULL; lay->LightWeight(); } } DeleteDC(dcMem); DeleteObject(bmLayer); if (hpal) DeleteObject(hpal); FAfterMask->DeleteDC(dcAfterMask); FAfterBitmap->DeleteDC(dcAfterBitmap); } else { ::doDestroy(FAfterMask); ::doDestroy(FAfterBitmap); } } else { ::doDestroy(FAfterMask); ::doDestroy(FAfterBitmap); ::doDestroy(FBeforeBitmap); } return true; fail: if (dcMask) lay->LMask->DeleteDC(dcMask); if (dcBitmap) lay->LBitmap->DeleteDC(dcBitmap); if (bmLayer) { if (dcMem) DeleteDC(dcMem); DeleteObject(bmLayer); } if (hpal) DeleteObject(hpal); if (FAfterMask) { if (dcAfterMask) FAfterMask->DeleteDC(dcAfterMask); delete FAfterMask; FAfterMask = NULL; } if (FAfterBitmap) { if (dcAfterBitmap) FAfterBitmap->DeleteDC(dcAfterBitmap); delete FAfterBitmap; FAfterBitmap = NULL; } if (FBeforeBitmap) { if (dcBeforeBitmap) FBeforeBitmap->DeleteDC(dcBeforeBitmap); delete FBeforeBitmap; FBeforeBitmap = NULL; } return false; } //--------------------------------------------------------------------------- //layer¸¦ Ãß°¡ÇÑ´Ù. (nameÀº layerÀÇ À̸§À̰í, w,h´Â layerÀÇ Å©±âÀÌ´Ù. //bpp´Â bits/pixelÀ̰í bg´Â background colorÀ̰í rgb´Â bpp°¡ 8Àϰæ¿ì¿¡ paletteÀÌ´Ù. bool __fastcall TPLayerImage::AddLayer(AnsiString name, int w, int h, int bpp, COLORREF bg, RGBQUAD *rgb) { if(LayerList->Count>=MAX_LAYER_NUM-1) return false; TPLayer *lay = NULL; if (LayerList->Count) { if ((lay = new TPLayer(name, TempLayerFilename(), LTTransparency)) == NULL) goto fail; } else { if ((lay = new TPLayer(name, TempLayerFilename(), LTInitial)) == NULL) goto fail; } if (bpp == 8 && rgb) { memcpy(Frgb, rgb, sizeof(RGBQUAD) * 256); if (!lay->LBitmap->Create(w, h, 8, rgb)) goto fail; lay->LBitmap->FillRect(Rect(0, 0, w, h), bg); if (lay->LMask) { if (!lay->LMask->Create(w, h, 8, rgb)) goto fail; lay->LMask->FillRect(Rect(0, 0, w, h), PALETTEINDEX(255)); // Transparency } } else { if (!lay->LBitmap->Create(w, h, 24)) goto fail; lay->LBitmap->FillRect(Rect(0, 0, w, h), bg); if (lay->LMask) { if (!lay->LMask->Create(w, h, 1)) goto fail; lay->LMask->FillRect(Rect(0, 0, w, h), clWhite); // Transparency } } LayerList->Add(lay); SetIndex(LayerList->Count - 1); ChangeBitmap(lay); if (FBackGroundLock) InitBackGround(); return true; fail: if (lay) delete lay; return false; } //--------------------------------------------------------------------------- // layer¸¦ ÇöÀçÀ§Ä¡¿¡ »ðÀÔÇÑ´Ù. (nameÀº layerÀÇ À̸§À̰í, w,h´Â layerÀÇ Å©±âÀ̰í, // bpp´Â bits / pixelÀ̰í bg´Â background colorÀ̰í rgb´Â bpp°¡ 8ÀÏ °æ¿ì¿¡ paletteÀÌ´Ù. bool __fastcall TPLayerImage::InsertLayer(AnsiString name, int w, int h, int bpp, COLORREF bg, RGBQUAD *rgb) { TPLayer *lay = NULL, *srclay; if ((lay = new TPLayer(name, TempLayerFilename(), LTTransparency)) == NULL) goto fail; if (bpp == 8 && rgb) { memcpy(Frgb, rgb, sizeof(RGBQUAD) * 256); if (!lay->LBitmap->Create(w, h, 8, rgb)) goto fail; lay->LBitmap->FillRect(Rect(0, 0, w, h), bg); if (lay->LMask) { if (!lay->LMask->Create(w, h, 8, rgb)) goto fail; lay->LMask->FillRect(Rect(0, 0, w, h), PALETTEINDEX(255)); // Transparency } } else { if (!lay->LBitmap->Create(w, h, 24)) goto fail; lay->LBitmap->FillRect(Rect(0, 0, w, h), bg); if (lay->LMask) { if (!lay->LMask->Create(w, h, 1)) goto fail; lay->LMask->FillRect(Rect(0, 0, w, h), clWhite); // Transparency } } LayerList->Insert(FIndex, lay); CopyRGN(lay); srclay = (TPLayer *) LayerList->Items[FIndex + 1]; srclay->SaveToLayerFile(); ChangeBitmap(lay); MakeBeforeBitmap(); if (FBackGroundLock) InitBackGround(); return true; fail: if (lay) delete lay; return false; } //--------------------------------------------------------------------------- // ÇöÀç À§Ä¡ÀÇ layer¸¦ Á¦°ÅÇÑ´Ù. void __fastcall TPLayerImage::DeleteLayer() { int i; TPLayer *srclay, *dstlay; if (FIndex > 1) i = FIndex - 1; else i = 0; dstlay = (TPLayer *) LayerList->Items[i]; dstlay->LoadFromLayerFile(Frgb); CopyRGN(dstlay); srclay = (TPLayer *) LayerList->Items[FIndex]; LayerList->Remove(srclay); delete srclay; FIndex = i; if (FIndex == 0) { if (dstlay->LMask) dstlay->LMask->FillRect(Rect(0, 0, dstlay->LMask->Width, dstlay->LMask->Height), 0); // Transparency --> BackGround } ChangeBitmap(dstlay); MakeBeforeBitmap(); if (FBackGroundLock) InitBackGround(); } //--------------------------------------------------------------------------- void __fastcall TPLayerImage::LayerVisibleChange(int Value, bool IsView) { if (FIndex == Value) FLayerVisible = IsView; MakeBeforeBitmap(); if (FBackGroundLock) InitBackGround(); } //--------------------------------------------------------------------------- // ÇöÀç visibleµÈ layer¸¦ ¸ðµÎ ÇÕ¼ºÇÑ´Ù. bool __fastcall TPLayerImage::MergeVisibleLayer() { int i, j, n, cnt, w, h; TPLayer *lay; BITMAPHANDLE *bh = FBitmap->Handle; HBITMAP bmMergeL = NULL, bmMergeM = NULL, bmTemp = NULL; HDC dcMergeL = NULL, dcMergeM = NULL, dcLBitmap = NULL, dcLMask = NULL, dcBitmap = NULL, dcMask = NULL, dcTemp = NULL; HPALETTE hpal = NULL; RGBQUAD rgb[256]; HRGN hRgn = NULL; bool bInit; cnt = LayerList->Count; i = -1; n = 0; for (j = 0; j < cnt; j++) { lay = (TPLayer *) LayerList->Items[j]; if (lay->Visible) { if (i == -1) i = j; n++; } } if (n <= 1) return true; w = FBitmap->Width; h = FBitmap->Height; if (i) { if ((dcBitmap = FBitmap->CreateDC()) == NULL) goto fail; if ((dcMask = FLayerMask->CreateDC()) == NULL) goto fail; if (FBitmap->BitsPerPixel == 8) { hpal = L_CreatePaintPalette(dcBitmap, bh); } if ((bmMergeL = CreateCompatibleBitmap(dcBitmap, w, h)) == NULL) goto fail; if ((dcMergeL = CreateCompatibleDC(dcBitmap)) == NULL) goto fail; SelectObject(dcMergeL, bmMergeL); if ((bmMergeM = CreateCompatibleBitmap(dcMask, w, h)) == NULL) goto fail; if ((dcMergeM = CreateCompatibleDC(dcMask)) == NULL) goto fail; SelectObject(dcMergeM, bmMergeM); if ((bmTemp = CreateCompatibleBitmap(dcBitmap, w, h)) == NULL) goto fail; if ((dcTemp = CreateCompatibleDC(dcBitmap)) == NULL) goto fail; SelectObject(dcTemp, bmTemp); if (hpal) { SelectPalette(dcMergeL, hpal, false); SelectPalette(dcMergeM, hpal, false); SelectPalette(dcTemp, hpal, false); } FBitmap->DeleteDC(dcBitmap); dcBitmap = NULL; FLayerMask->DeleteDC(dcMask); dcMask = NULL; bInit = true; for (i = 1; i < cnt; i++) { if (i != FIndex) { lay = (TPLayer *) LayerList->Items[i]; if (lay->Visible) { lay->LoadFromLayerFile(Frgb); lay->LBitmap->RemoveRegion(); if ((dcLBitmap = lay->LBitmap->CreateDC()) == NULL) goto fail; if ((dcLMask = lay->LMask->CreateDC()) == NULL) goto fail; if (bInit) { BitBlt(dcMergeL, 0, 0, w, h, dcLBitmap, 0, 0, SRCCOPY); BitBlt(dcMergeM, 0, 0, w, h, dcLMask, 0, 0, SRCCOPY); bInit = false; } else { BitBlt(dcMergeL, 0, 0, w, h, dcLMask, 0, 0, SRCAND); BitBlt(dcTemp, 0, 0, w, h, dcLMask, 0, 0, NOTSRCCOPY); BitBlt(dcTemp, 0, 0, w, h, dcLBitmap, 0, 0, SRCAND); BitBlt(dcMergeL, 0, 0, w, h, dcTemp, 0, 0, SRCPAINT); BitBlt(dcMergeM, 0, 0, w, h, dcLMask, 0, 0, SRCAND); } lay->LMask->DeleteDC(dcLMask); dcLMask = NULL; lay->LBitmap->DeleteDC(dcLBitmap); dcLBitmap = NULL; lay->LightWeight(); } } else { if (FLayerVisible) { hRgn = FBitmap->GetRegion(true); if ((dcBitmap = FBitmap->CreateDC()) == NULL) goto fail; if ((dcMask = FLayerMask->CreateDC()) == NULL) goto fail; if (bInit) { BitBlt(dcMergeL, 0, 0, w, h, dcBitmap, 0, 0, SRCCOPY); BitBlt(dcMergeM, 0, 0, w, h, dcMask, 0, 0, SRCCOPY); bInit = false; } else { BitBlt(dcMergeL, 0, 0, w, h, dcMask, 0, 0, SRCAND); BitBlt(dcTemp, 0, 0, w, h, dcMask, 0, 0, NOTSRCCOPY); BitBlt(dcTemp, 0, 0, w, h, dcBitmap, 0, 0, SRCAND); BitBlt(dcMergeL, 0, 0, w, h, dcTemp, 0, 0, SRCPAINT); BitBlt(dcMergeM, 0, 0, w, h, dcMask, 0, 0, SRCAND); } FLayerMask->DeleteDC(dcMask); dcMask = NULL; FBitmap->DeleteDC(dcBitmap); dcBitmap = NULL; } } } DeleteDC(dcTemp); dcTemp = NULL; DeleteObject(bmTemp); bmTemp = NULL; MergeVisibleList(); if ((dcBitmap = FBitmap->CreateDC()) == NULL) goto fail; BitBlt(dcBitmap, 0, 0, w, h, dcMergeL, 0, 0, SRCCOPY); FBitmap->DeleteDC(dcBitmap); if (FLayerMask) { if ((dcMask = FLayerMask->CreateDC()) == NULL) goto fail; BitBlt(dcMask, 0, 0, w, h, dcMergeM, 0, 0, SRCCOPY); FLayerMask->DeleteDC(dcMask); } if (hRgn) { FBitmap->PutRegion(hRgn, L_RGN_SET); DeleteObject(hRgn); } DeleteDC(dcMergeL); DeleteObject(bmMergeL); DeleteDC(dcMergeM); DeleteObject(bmMergeM); if (hpal) DeleteObject(hpal); } else { if ((dcBitmap = FBitmap->CreateDC()) == NULL) goto fail; if (FBitmap->BitsPerPixel == 8) { hpal = L_CreatePaintPalette(dcBitmap, bh); } if ((bmMergeL = CreateCompatibleBitmap(dcBitmap, w, h)) == NULL) goto fail; if ((dcMergeL = CreateCompatibleDC(dcBitmap)) == NULL) goto fail; SelectObject(dcMergeL, bmMergeL); if ((bmTemp = CreateCompatibleBitmap(dcBitmap, w, h)) == NULL) goto fail; if ((dcTemp = CreateCompatibleDC(dcBitmap)) == NULL) goto fail; SelectObject(dcTemp, bmTemp); if (hpal) { SelectPalette(dcMergeL, hpal, false); SelectPalette(dcTemp, hpal, false); } FBitmap->DeleteDC(dcBitmap); dcBitmap = NULL; for (i = 0; i < cnt; i++) { if (i != FIndex) { lay = (TPLayer *) LayerList->Items[i]; if (lay->Visible) { lay->LoadFromLayerFile(Frgb); lay->LBitmap->RemoveRegion(); if ((dcLBitmap = lay->LBitmap->CreateDC()) == NULL) goto fail; if (lay->LMask) { if ((dcLMask = lay->LMask->CreateDC()) == NULL) goto fail; BitBlt(dcMergeL, 0, 0, w, h, dcLMask, 0, 0, SRCAND); BitBlt(dcTemp, 0, 0, w, h, dcLMask, 0, 0, NOTSRCCOPY); BitBlt(dcTemp, 0, 0, w, h, dcLBitmap, 0, 0, SRCAND); BitBlt(dcMergeL, 0, 0, w, h, dcTemp, 0, 0, SRCPAINT); lay->LMask->DeleteDC(dcLMask); dcLMask = NULL; } else { BitBlt(dcMergeL, 0, 0, w, h, dcLBitmap, 0, 0, SRCCOPY); } lay->LBitmap->DeleteDC(dcLBitmap); dcLBitmap = NULL; lay->LightWeight(); } } else { if (FLayerVisible) { hRgn = FBitmap->GetRegion(true); if ((dcBitmap = FBitmap->CreateDC()) == NULL) goto fail; if (FLayerMask) { if ((dcMask = FLayerMask->CreateDC()) == NULL) goto fail; BitBlt(dcMergeL, 0, 0, w, h, dcMask, 0, 0, SRCAND); BitBlt(dcTemp, 0, 0, w, h, dcMask, 0, 0, NOTSRCCOPY); BitBlt(dcTemp, 0, 0, w, h, dcBitmap, 0, 0, SRCAND); BitBlt(dcMergeL, 0, 0, w, h, dcTemp, 0, 0, SRCPAINT); FLayerMask->DeleteDC(dcMask); dcMask = NULL; } else { BitBlt(dcMergeL, 0, 0, w, h, dcBitmap, 0, 0, SRCCOPY); } FBitmap->DeleteDC(dcBitmap); dcBitmap = NULL; } } } DeleteDC(dcTemp); dcTemp = NULL; DeleteObject(bmTemp); bmTemp = NULL; MergeVisibleList(); if ((dcBitmap = FBitmap->CreateDC()) == NULL) goto fail; BitBlt(dcBitmap, 0, 0, w, h, dcMergeL, 0, 0, SRCCOPY); FBitmap->DeleteDC(dcBitmap); if (FLayerMask) { if ((dcMask = FLayerMask->CreateDC()) == NULL) goto fail; BitBlt(dcMask, 0, 0, w, h, dcMergeM, 0, 0, SRCCOPY); FLayerMask->DeleteDC(dcMask); } if (hRgn) { FBitmap->PutRegion(hRgn, L_RGN_SET); DeleteObject(hRgn); } DeleteDC(dcMergeL); DeleteObject(bmMergeL); if (hpal) DeleteObject(hpal); } if (FBackGroundLock) InitBackGround(); return true; fail: if (hRgn) DeleteObject(hRgn); if (dcLMask) { lay->LMask->DeleteDC(dcLMask); lay->LMask->Destroy(); } if (dcLBitmap) { lay->LBitmap->DeleteDC(dcLBitmap); lay->LBitmap->Destroy(); } if (bmTemp) { if (dcTemp) DeleteDC(dcTemp); DeleteObject(bmTemp); } if (bmMergeM) { if (dcMergeM) DeleteDC(dcMergeM); DeleteObject(bmMergeM); } if (bmMergeL) { if (dcMergeL) DeleteDC(dcMergeL); DeleteObject(bmMergeL); } if (hpal) DeleteObject(hpal); if (dcMask) FLayerMask->DeleteDC(dcMask); if (dcBitmap) FBitmap->DeleteDC(dcBitmap); return false; } //--------------------------------------------------------------------------- // layer¸ðµÎ ÇÕ¼ºÇÑ´Ù. bool __fastcall TPLayerImage::FlattenLayer() { int i, cnt, w, h; TPLayer *lay; BITMAPHANDLE *bh = FBitmap->Handle; HBITMAP bmMergeL = NULL, bmTemp = NULL; HDC dcMergeL = NULL, dcBitmap = NULL, dcMask = NULL, dcLBitmap = NULL, dcLMask = NULL, dcTemp = NULL; HPALETTE hpal = NULL; RGBQUAD rgb[256]; HRGN hRgn = NULL; cnt = LayerList->Count; w = FBitmap->Width; h = FBitmap->Height; if ((dcBitmap = FBitmap->CreateDC()) == NULL) goto fail; if (FBitmap->BitsPerPixel == 8) { hpal = L_CreatePaintPalette(dcBitmap, bh); } if ((bmMergeL = CreateCompatibleBitmap(dcBitmap, w, h)) == NULL) goto fail; if ((dcMergeL = CreateCompatibleDC(dcBitmap)) == NULL) goto fail; SelectObject(dcMergeL, bmMergeL); if ((bmTemp = CreateCompatibleBitmap(dcBitmap, w, h)) == NULL) goto fail; if ((dcTemp = CreateCompatibleDC(dcBitmap)) == NULL) goto fail; SelectObject(dcTemp, bmTemp); if (hpal) { SelectPalette(dcMergeL, hpal, false); SelectPalette(dcTemp, hpal, false); } FBitmap->DeleteDC(dcBitmap); dcBitmap = NULL; for (i = 0; i < cnt; i++) { if (i != FIndex) { lay = (TPLayer *) LayerList->Items[i]; lay->LoadFromLayerFile(Frgb); lay->LBitmap->RemoveRegion(); if ((dcLBitmap = lay->LBitmap->CreateDC()) == NULL) goto fail; if (lay->LMask) { if ((dcLMask = lay->LMask->CreateDC()) == NULL) goto fail; BitBlt(dcMergeL, 0, 0, w, h, dcLMask, 0, 0, SRCAND); BitBlt(dcTemp, 0, 0, w, h, dcLMask, 0, 0, NOTSRCCOPY); BitBlt(dcTemp, 0, 0, w, h, dcLBitmap, 0, 0, SRCAND); BitBlt(dcMergeL, 0, 0, w, h, dcTemp, 0, 0, SRCPAINT); lay->LMask->DeleteDC(dcLMask); dcLMask = NULL; } else { BitBlt(dcMergeL, 0, 0, w, h, dcLBitmap, 0, 0, SRCCOPY); } lay->LBitmap->DeleteDC(dcLBitmap); dcLBitmap = NULL; lay->LightWeight(); } else { hRgn = FBitmap->GetRegion(true); if ((dcBitmap = FBitmap->CreateDC()) == NULL) goto fail; if (FLayerMask) { if ((dcMask = FLayerMask->CreateDC()) == NULL) goto fail; BitBlt(dcMergeL, 0, 0, w, h, dcMask, 0, 0, SRCAND); BitBlt(dcTemp, 0, 0, w, h, dcMask, 0, 0, NOTSRCCOPY); BitBlt(dcTemp, 0, 0, w, h, dcBitmap, 0, 0, SRCAND); BitBlt(dcMergeL, 0, 0, w, h, dcTemp, 0, 0, SRCPAINT); FLayerMask->DeleteDC(dcMask); dcMask = NULL; } else { BitBlt(dcMergeL, 0, 0, w, h, dcBitmap, 0, 0, SRCCOPY); } FBitmap->DeleteDC(dcBitmap); dcBitmap = NULL; } } DeleteDC(dcTemp); dcTemp = NULL; DeleteObject(bmTemp); bmTemp = NULL; FlattenList(); if ((dcBitmap = FBitmap->CreateDC()) == NULL) goto fail; BitBlt(dcBitmap, 0, 0, w, h, dcMergeL, 0, 0, SRCCOPY); FBitmap->DeleteDC(dcBitmap); if (hRgn) { FBitmap->PutRegion(hRgn, L_RGN_SET); DeleteObject(hRgn); } DeleteDC(dcMergeL); DeleteObject(bmMergeL); if (hpal) DeleteObject(hpal); if (FBackGroundLock) InitBackGround(); return true; fail: if (hRgn) DeleteObject(hRgn); if (dcMask) { lay->LMask->DeleteDC(dcMask); lay->LMask->Destroy(); } if (dcBitmap) { lay->LBitmap->DeleteDC(dcBitmap); lay->LBitmap->Destroy(); } if (bmTemp) { if (dcTemp) DeleteDC(dcTemp); DeleteObject(bmTemp); } if (bmMergeL) { if (dcMergeL) DeleteDC(dcMergeL); DeleteObject(bmMergeL); } if (hpal) DeleteObject(hpal); if (dcMask) FLayerMask->DeleteDC(dcMask); if (dcBitmap) FBitmap->DeleteDC(dcBitmap); return false; } //--------------------------------------------------------------------------- // ÇöÀç layer¸¦ º¹»çÇÑ´Ù. bool __fastcall TPLayerImage::CopyLayer(AnsiString name) { RGBQUAD rgb[256]; int w, h, bpp; TPLayer *dlay = NULL, *slay; slay = (TPLayer *) LayerList->Items[FIndex]; if ((dlay = new TPLayer(name, TempLayerFilename(), LTTransparency)) == NULL) goto fail; bpp = FBitmap->BitsPerPixel; w = FBitmap->Width; h = FBitmap->Height; if (bpp == 8) { FBitmap->GetColors(0, 256, rgb); if (!dlay->LBitmap->Create(w, h, 8, rgb)) goto fail; if (!dlay->LMask->Create(w, h, 8, rgb)) goto fail; } else { if (!dlay->LBitmap->Create(w, h, 24)) goto fail; if (!dlay->LMask->Create(w, h, 1)) goto fail; } dlay->LBitmap->Copy(FBitmap, SRCCOPY); if (FLayerMask) dlay->LMask->Copy(FLayerMask, SRCCOPY); else dlay->LMask->FillRect(Rect(0, 0, dlay->LMask->Width, dlay->LMask->Height), 0); LayerList->Add(dlay); SetIndex(LayerList->Count - 1); ChangeBitmap(dlay); if (FBackGroundLock) InitBackGround(); return true; fail: if (dlay) delete dlay; return false; } //--------------------------------------------------------------------------- // µÎ layerÀÇ À§Ä¡¸¦ ±³È¯ÇÑ´Ù. void __fastcall TPLayerImage::ExchangeLayer(int srcIndex, int dstIndex) { TPLayer *dlay, *slay; dlay = (TPLayer *) LayerList->Items[dstIndex]; slay = (TPLayer *) LayerList->Items[srcIndex]; LayerList->Exchange(srcIndex, dstIndex); if (FIndex == dstIndex) FIndex = srcIndex; else if (FIndex == srcIndex) FIndex = dstIndex; MakeBeforeBitmap(); if (FBackGroundLock) InitBackGround(); } //--------------------------------------------------------------------------- // ÇöÀç ¼³Á¤µÈ size¸¦ º¯°æÇÑ´Ù. // È®´ë½Ã´Â À̹ÌÁöÀÇ º¯È­´Â ¾ø°í ºó°ø°£¿¡´Â bc¶ó´Â color·Î ä¿î´Ù. // Ãà¼Ò½Ã´Â À̹ÌÁö°¡ À߸°´Ù. void __fastcall TPLayerImage::ResizeLayer(int w, int h, COLORREF bc) { int i, bpp, cnt = LayerList->Count; TPLayer *lay; COLORREF mbc; bpp = FBitmap->BitsPerPixel; if (bpp == 8) mbc = PALETTEINDEX(255); else mbc = clWhite; FBitmap->Resize(w, h, bc); if (FBeforeBitmap) FBeforeBitmap->Resize(w, h, bc); lay = (TPLayer *) LayerList->Items[FIndex]; if (FLayerMask) { FLayerMask->Resize(w, h, mbc); } if (FAfterBitmap) FAfterBitmap->Resize(w, h, bc); if (FAfterMask) FAfterMask->Resize(w, h, mbc); for (i = 0; i < cnt; i++) { if (i != FIndex) { lay = (TPLayer *) LayerList->Items[i]; lay->LoadFromLayerFile(Frgb); lay->LBitmap->Resize(w, h, bc); if (lay->LMask) { if (bpp == 8) { lay->LMask->Resize(w, h, PALETTEINDEX(255)); } else { lay->LMask->Resize(w, h, clWhite); } } lay->SaveToLayerFile(); } } } //--------------------------------------------------------------------------- void __fastcall TPLayerImage::ResizeStretchLayer(int w, int h) { int i, cnt = LayerList->Count; TPLayer *lay; FBitmap->ResizeStretch(w, h); if (FLayerMask) FLayerMask->ResizeStretch(w, h); if (FBeforeBitmap) FBeforeBitmap->ResizeStretch(w, h); if (FAfterBitmap) FAfterBitmap->ResizeStretch(w, h); if (FAfterMask) FAfterMask->ResizeStretch(w, h); for (i = 0; i < cnt; i++) { if (i != FIndex) { lay = (TPLayer *) LayerList->Items[i]; lay->LoadFromLayerFile(Frgb); lay->LBitmap->ResizeStretch(w, h); if (lay->LMask) lay->LMask->ResizeStretch(w, h); lay->SaveToLayerFile(); } } } //--------------------------------------------------------------------------- bool __fastcall TPLayerImage::LoadFromLayerFile(HANDLE FH, RGBQUAD *rgb) { DWORD dwRead; TPLayer *lay = NULL; int i, cnt, len; while (LayerList->Count) { lay = (TPLayer *) LayerList->Last(); LayerList->Remove(lay); delete lay; lay = NULL; } if (rgb) memcpy(Frgb, rgb, sizeof(RGBQUAD) * 256); if (!ReadFile(FH, &cnt, sizeof(int), &dwRead, NULL)) goto fail; if (!ReadFile(FH, &FIndex, sizeof(int), &dwRead, NULL)) goto fail; for (i = 0; i < cnt; i++) { if ((lay = new TPLayer(i ? LTTransparency : LTInitial)) == NULL) goto fail; if (!lay->LoadFromLayerFile(FH, Frgb)) goto fail; lay->UpdateMiniBitmap(Frgb); ////////////È­ÀÏ ·Îµå½Ã ·¹ÀÌ¾î ¹Ì´Ï¸Ê º¸¿©ÁÖ±âÀ§ÇØ /by jeegeo LayerList->Add(lay); if (i != FIndex) { if (!lay->SaveToLayerFile()) goto fail; } else { ChangeBitmap(lay); } } MakeBeforeBitmap(); return true; fail: if (lay) delete lay; return false; } //--------------------------------------------------------------------------- bool __fastcall TPLayerImage::SaveToLayerFile(HANDLE FH, RECT r) { DWORD dwWrite; TPLayer *lay; int i, cnt = LayerList->Count; if (!WriteFile(FH, &cnt, sizeof(int), &dwWrite, NULL)) return false; if (!WriteFile(FH, &FIndex, sizeof(int), &dwWrite, NULL)) return false; for (i = 0; i < cnt; i++) { lay = (TPLayer *) LayerList->Items[i]; if (i == FIndex) { if (!lay->SaveToLayerFile(FH, r, Frgb, true)) return false; } else { if (!lay->SaveToLayerFile(FH, r, Frgb, false)) return false; } } return true; } //--------------------------------------------------------------------------- bool __fastcall TPLayerImage::SaveToLayerFile(HANDLE FH) { DWORD dwWrite; TPLayer *lay; int i, cnt = LayerList->Count, len; if (!WriteFile(FH, &cnt, sizeof(int), &dwWrite, NULL)) return false; if (!WriteFile(FH, &FIndex, sizeof(int), &dwWrite, NULL)) return false; for (i = 0; i < cnt; i++) { lay = (TPLayer *) LayerList->Items[i]; if (i == FIndex) { if (!lay->SaveToLayerFile(FH, Frgb, true)) return false; } else { if (!lay->SaveToLayerFile(FH, Frgb, false)) return false; } } return true; } //--------------------------------------------------------------------------- bool __fastcall TPLayerImage::MakeBackGround() { RGBQUAD r[256]; HDC dcSrc = NULL, dcBefore = NULL, dcMask = NULL, dcDst = NULL, dcMemL = NULL; HBITMAP bmMemL = NULL; HPALETTE hpal = NULL; int w, h; if (FBackGround == NULL) return false; w = FBitmap->Width; h = FBitmap->Height; if (FBitmap->BitsPerPixel == 8) { FBitmap->GetColors(0, 256, r); if (!FBackGround->Create(w, h, 8, r)) goto fail; } else { if (!FBackGround->Create(w, h, 24)) goto fail; } if ((dcDst = FBackGround->CreateDC()) == NULL) goto fail; if (FBitmap->BitsPerPixel == 8) { hpal = L_CreatePaintPalette(dcDst, FBitmap->Handle); if (hpal) SelectPalette(dcDst, hpal, false); } if ((dcBefore = FBeforeBitmap->CreateDC()) == NULL) goto fail; BitBlt(dcDst, 0, 0, w, h, dcBefore, 0, 0, SRCCOPY); FBeforeBitmap->DeleteDC(dcBefore); dcBefore = NULL; if (FLayerVisible) { if ((bmMemL = CreateCompatibleBitmap(dcDst, w, h)) == NULL) goto fail; if ((dcMemL = CreateCompatibleDC(dcDst)) == NULL) goto fail; SelectObject(dcMemL, bmMemL); if (hpal) SelectPalette(dcMemL, hpal, false); if ((dcMask = FLayerMask->CreateDC()) == NULL) goto fail; BitBlt(dcDst, 0, 0, w, h, dcMask, 0, 0, SRCAND); BitBlt(dcMemL, 0, 0, w, h, dcMask, 0, 0, NOTSRCCOPY); FLayerMask->DeleteDC(dcMask); dcMask = NULL; if ((dcSrc = FBitmap->CreateDC()) == NULL) goto fail; BitBlt(dcMemL, 0, 0, w, h, dcSrc, 0, 0, SRCAND); BitBlt(dcDst, 0, 0, w, h, dcMemL, 0, 0, SRCPAINT); FBitmap->DeleteDC(dcSrc); dcSrc = NULL; DeleteDC(dcMemL); DeleteObject(bmMemL); } if (hpal) DeleteObject(hpal); FBackGround->DeleteDC(dcDst); return true; fail: if (dcSrc) FBitmap->DeleteDC(dcSrc); if (dcMask) FLayerMask->DeleteDC(dcMask); if (bmMemL) { if (dcMemL) DeleteDC(dcMemL); DeleteObject(bmMemL); } if (FBeforeBitmap) { if (dcBefore) FBeforeBitmap->DeleteDC(dcBefore); } if (hpal) DeleteObject(hpal); if (FBackGround) { if (dcDst) FBackGround->DeleteDC(dcDst); delete FBackGround; FBackGround = NULL; } return false; } //--------------------------------------------------------------------------- bool __fastcall TPLayerImage::IsBack() { TPLayer *lay = (TPLayer *) LayerList->Items[FIndex]; return !(lay->LayerType == LTTransparency); } //--------------------------------------------------------------------------- bool __fastcall TPLayerImage::InitBackGround() { if (LayerList->Count > 1 && FIndex > 0) { if (FBackGround == NULL) { if ((FBackGround = new TTexpiaBitmap) == NULL) goto fail; } if (!MakeBackGround()) goto fail; } else { ::doDestroy(FBackGround); } return true; fail: ::doDestroy(FBackGround); return false; } //--------------------------------------------------------------------------- bool __fastcall TPLayerImage::InitBackGround(Byte lock) { if (LayerList->Count > 1 && FIndex > 0) { if (FBackGround == NULL) { if ((FBackGround = new TTexpiaBitmap) == NULL) goto fail; } if (!MakeBackGround()) goto fail; } FBackGroundLock |= lock; return true; fail: ::doDestroy(FBackGround); return false; } //--------------------------------------------------------------------------- void __fastcall TPLayerImage::ExitBackGround(Byte lock) { if (FBackGroundLock) { FBackGroundLock &= ~lock; if (FBackGroundLock == 0) ::doDestroy(FBackGround); } } //--------------------------------------------------------------------------- TTexpiaBitmap *__fastcall TPLayerImage::Composition() { int w, h; TTexpiaBitmap *bmSrc = NULL, *bmMemL = NULL; RGBQUAD rgb[256]; HDC dcSrc = NULL, dcMemL = NULL, dcBeforeBitmap = NULL, dcBitmap = NULL, dcMask = NULL, dcAfterBitmap = NULL, dcAfterMask = NULL; if ((bmSrc = new TTexpiaBitmap) == NULL) goto fail; if ((bmMemL = new TTexpiaBitmap) == NULL) goto fail; w = FBitmap->Width; h = FBitmap->Height; if (FBitmap->BitsPerPixel == 8) { FBitmap->GetColors(0, 256, rgb); if (!bmSrc->Create(w, h, 8, rgb)) goto fail; if (!bmMemL->Create(w, h, 8, rgb)) goto fail; } else { if (!bmSrc->Create(w, h, FBitmap->BitsPerPixel)) goto fail; if (!bmMemL->Create(w, h, FBitmap->BitsPerPixel)) goto fail; } if ((dcSrc = bmSrc->CreateDC()) == NULL) goto fail; if ((dcMemL = bmMemL->CreateDC()) == NULL) goto fail; if (FBeforeBitmap) { if ((dcBeforeBitmap = FBeforeBitmap->CreateDC()) == NULL) goto fail; BitBlt(dcSrc, 0, 0, w, h, dcBeforeBitmap, 0, 0, SRCCOPY); FBeforeBitmap->DeleteDC(dcBeforeBitmap); dcBeforeBitmap = NULL; if (FLayerVisible) { if ((dcMask = FLayerMask->CreateDC()) == NULL) goto fail; BitBlt(dcSrc, 0, 0, w, h, dcMask, 0, 0, SRCAND); BitBlt(dcMemL, 0, 0, w, h, dcMask, 0, 0, NOTSRCCOPY); FLayerMask->DeleteDC(dcMask); dcMask = NULL; if ((dcBitmap = FBitmap->CreateDC()) == NULL) goto fail; BitBlt(dcMemL, 0, 0, w, h, dcBitmap, 0, 0, SRCAND); BitBlt(dcSrc, 0, 0, w, h, dcMemL, 0, 0, SRCPAINT); FBitmap->DeleteDC(dcBitmap); dcBitmap = NULL; } } else { if (FLayerVisible) { if ((dcBitmap = FBitmap->CreateDC()) == NULL) goto fail; BitBlt(dcSrc, 0, 0, w, h, dcBitmap, 0, 0, SRCCOPY); FBitmap->DeleteDC(dcBitmap); dcBitmap = NULL; } else { BitBlt(dcSrc, 0, 0, w, h, NULL, 0, 0, WHITENESS); } } if (FAfterBitmap) { if ((dcAfterMask = FAfterMask->CreateDC()) == NULL) goto fail; BitBlt(dcSrc, 0, 0, w, h, dcAfterMask, 0, 0, SRCAND); BitBlt(dcMemL, 0, 0, w, h, dcAfterMask, 0, 0, NOTSRCCOPY); FAfterMask->DeleteDC(dcAfterMask); dcAfterMask = NULL; if ((dcAfterBitmap = FAfterBitmap->CreateDC()) == NULL) goto fail; BitBlt(dcMemL, 0, 0, w, h, dcAfterBitmap, 0, 0, SRCAND); BitBlt(dcSrc, 0, 0, w, h, dcMemL, 0, 0, SRCPAINT); FAfterBitmap->DeleteDC(dcAfterBitmap); dcAfterBitmap = NULL; } bmMemL->DeleteDC(dcMemL); bmSrc->DeleteDC(dcSrc); delete bmMemL; return bmSrc; fail: if (dcAfterMask) FAfterMask->DeleteDC(dcAfterMask); if (dcAfterBitmap) FAfterBitmap->DeleteDC(dcAfterBitmap); if (dcMask) FMask->DeleteDC(dcMask); if (dcBitmap) FBitmap->DeleteDC(dcBitmap); if (dcBeforeBitmap) FBeforeBitmap->DeleteDC(dcBeforeBitmap); if (bmSrc) { if (dcSrc) bmSrc->DeleteDC(dcSrc); delete bmSrc; } if (bmMemL) { if (dcMemL) bmMemL->DeleteDC(dcMemL); delete bmMemL; } return NULL; } //--------------------------------------------------------------------------- void __fastcall TPLayerImage::Composition(HDC dcDst, RECT r) { int w, h; TTexpiaBitmap *bmMemL = NULL; RGBQUAD rgb[256]; HDC dcMemL = NULL, dcBeforeBitmap = NULL, dcBitmap = NULL, dcMask = NULL, dcAfterBitmap = NULL, dcAfterMask = NULL; w = r.right - r.left; h = r.bottom - r.top; if ((bmMemL = new TTexpiaBitmap) == NULL) goto fail; if (FBitmap->BitsPerPixel == 8) { FBitmap->GetColors(0, 256, rgb); if (!bmMemL->Create(w, h, 8, rgb)) goto fail; } else { if (!bmMemL->Create(w, h, FBitmap->BitsPerPixel)) goto fail; } if ((dcMemL = bmMemL->CreateDC()) == NULL) goto fail; if (FBeforeBitmap) { if ((dcBeforeBitmap = FBeforeBitmap->CreateDC()) == NULL) goto fail; BitBlt(dcDst, 0, 0, w, h, dcBeforeBitmap, r.left, r.top, SRCCOPY); FBeforeBitmap->DeleteDC(dcBeforeBitmap); dcBeforeBitmap = NULL; if (FLayerVisible) { if ((dcMask = FLayerMask->CreateDC()) == NULL) goto fail; BitBlt(dcDst, 0, 0, w, h, dcMask, r.left, r.top, SRCAND); BitBlt(dcMemL, 0, 0, w, h, dcMask, r.left, r.top, NOTSRCCOPY); FLayerMask->DeleteDC(dcMask); dcMask = NULL; if ((dcBitmap = FBitmap->CreateDC()) == NULL) goto fail; BitBlt(dcMemL, 0, 0, w, h, dcBitmap, r.left, r.top, SRCAND); BitBlt(dcDst, 0, 0, w, h, dcMemL, 0, 0, SRCPAINT); FBitmap->DeleteDC(dcBitmap); dcBitmap = NULL; } } else { if (FLayerVisible) { if ((dcBitmap = FBitmap->CreateDC()) == NULL) goto fail; BitBlt(dcDst, 0, 0, w, h, dcBitmap, r.left, r.top, SRCCOPY); FBitmap->DeleteDC(dcBitmap); dcBitmap = NULL; } else { BitBlt(dcDst, 0, 0, w, h, NULL, 0, 0, WHITENESS); } } if (FAfterBitmap) { if ((dcAfterMask = FAfterMask->CreateDC()) == NULL) goto fail; BitBlt(dcDst, 0, 0, w, h, dcAfterMask, r.left, r.top, SRCAND); BitBlt(dcMemL, 0, 0, w, h, dcAfterMask, r.left, r.top, NOTSRCCOPY); FAfterMask->DeleteDC(dcAfterMask); dcAfterMask = NULL; if ((dcAfterBitmap = FAfterBitmap->CreateDC()) == NULL) goto fail; BitBlt(dcMemL, 0, 0, w, h, dcAfterBitmap, r.left, r.top, SRCAND); BitBlt(dcDst, 0, 0, w, h, dcMemL, 0, 0, SRCPAINT); FAfterBitmap->DeleteDC(dcAfterBitmap); dcAfterBitmap = NULL; } bmMemL->DeleteDC(dcMemL); delete bmMemL; return; fail: if (dcAfterMask) FAfterMask->DeleteDC(dcAfterMask); if (dcAfterBitmap) FAfterBitmap->DeleteDC(dcAfterBitmap); if (dcMask) FMask->DeleteDC(dcMask); if (dcBitmap) FBitmap->DeleteDC(dcBitmap); if (dcBeforeBitmap) FBeforeBitmap->DeleteDC(dcBeforeBitmap); if (bmMemL) { if (dcMemL) bmMemL->DeleteDC(dcMemL); delete bmMemL; } } //--------------------------------------------------------------------------- TTexpiaBitmap *__fastcall TPLayerImage::GetLayerBitmap(int i, TTexpiaBitmap **LMask) { TPLayer *lay = (TPLayer *)LayerList->Items[i]; if (i != FIndex) lay->LoadFromLayerFile(Frgb); if (LMask) *LMask = lay->LMask; return lay->LBitmap; } //--------------------------------------------------------------------------- void __fastcall TPLayerImage::SetLayerBitmap(int i) { if (i != FIndex) { TPLayer *lay = (TPLayer *)LayerList->Items[i]; lay->SaveToLayerFile(); } } //--------------------------------------------------------------------------- void __fastcall TPLayerImage::CopyRGN(TPLayer *lay) { BITMAPHANDLE *srcbh, *dstbh; HRGN rgn; RGNXFORM XForm; if (FBitmap) { srcbh = FBitmap->Handle; dstbh = lay->LBitmap->Handle; if (srcbh && dstbh) { if (L_BitmapHasRgn(dstbh)) L_FreeBitmapRgn(dstbh); if (L_BitmapHasRgn(srcbh)) { XForm.uViewPerspective = TOP_LEFT; XForm.nXScalarNum = 1; XForm.nXScalarDen = 1; XForm.nYScalarNum = 1; XForm.nYScalarDen = 1; XForm.nXOffset = 0; XForm.nYOffset = 0; L_GetBitmapRgnHandle(srcbh, &XForm, &rgn); L_SetBitmapRgnHandle(dstbh, &XForm, rgn, L_RGN_SET); DeleteObject(rgn); } } } } //--------------------------------------------------------------------------- void __fastcall TPLayerImage::ChangeBitmap(TPLayer *lay) { FBitmap = lay->LBitmap; FBitmap->OnPropertyChange = PropertyChange; FLayerMask = lay->LMask; FLayerVisible = lay->Visible; } //--------------------------------------------------------------------------- void __fastcall TPLayerImage::SetIndex(int Value) { TPLayer *srclay, *dstlay; if (FIndex != Value) { dstlay = (TPLayer *) LayerList->Items[Value]; dstlay->LoadFromLayerFile(Frgb); CopyRGN(dstlay); if (FIndex >= 0) { srclay = (TPLayer *) LayerList->Items[FIndex]; srclay->SaveToLayerFile(); } FIndex = Value; ChangeBitmap(dstlay); MakeBeforeBitmap(); } } //--------------------------------------------------------------------------- //--------------------------------------------------------------------------- //--------------------------------------------------------------------------- void __fastcall TPLayerImage::SetCrossPos(POINT Pos) { FCrossPos.x = BitmapToCanvasX(Pos.x); FCrossPos.y = BitmapToCanvasY(Pos.y); } //--------------------------------------------------------------------------- int __fastcall TPLayerImage::BitmapToCanvasX(int X) { return (X-FPositionX)*ZoomIn/ZoomOut; } //--------------------------------------------------------------------------- int __fastcall TPLayerImage::BitmapToCanvasY(int Y) { return (Y-FPositionY)*ZoomIn/ZoomOut; } //--------------------------------------------------------------------------- int __fastcall TPLayerImage::CanvasToBitmapX(int X) { return FPositionX+X*FZoomOut/FZoomIn; } //--------------------------------------------------------------------------- int __fastcall TPLayerImage::CanvasToBitmapY(int Y) { return FPositionY+Y*FZoomOut/FZoomIn; } //--------------------------------------------------------------------------- void __fastcall TPLayerImage::MouseDown(TMouseButton Button, TShiftState Shift, int X, int Y) { int px, py; if (FCoordinationTransfer) { if (FBitmap->Handle) { px = FPositionX+X*FZoomOut/FZoomIn; py = FPositionY+Y*FZoomOut/FZoomIn; TControl::MouseDown(Button, Shift, px, py); } else { TControl::MouseDown(Button, Shift, X, Y); } } else { TControl::MouseDown(Button, Shift, X, Y); } } //--------------------------------------------------------------------- void __fastcall TPLayerImage::MouseMove(TShiftState Shift, int X, int Y) { int px, py; if (FCoordinationTransfer) { if (FBitmap->Handle) { px = FPositionX+X*FZoomOut/FZoomIn; py = FPositionY+Y*FZoomOut/FZoomIn; TControl::MouseMove(Shift, px, py); } else { TControl::MouseMove(Shift, X, Y); } } else { TControl::MouseMove(Shift, X, Y); } } //--------------------------------------------------------------------- void __fastcall TPLayerImage::MouseUp(TMouseButton Button, TShiftState Shift, int X, int Y) { int px, py; if (FCoordinationTransfer) { if (FBitmap->Handle) { px = FPositionX+X*FZoomOut/FZoomIn; py = FPositionY+Y*FZoomOut/FZoomIn; TControl::MouseUp(Button, Shift, px, py); } else { TControl::MouseUp(Button, Shift, X, Y); } } else { TControl::MouseUp(Button, Shift, X, Y); } } //--------------------------------------------------------------------------- // ÇÁ·ÎÆÛƼ °ü·Ã //--------------------------------------------------------------------------- void __fastcall TPLayerImage::PropertyChange() { Invalidate(); } //--------------------------------------------------------------------------- void __fastcall TPLayerImage::SetBorderColor(TColor Value) { if (FBorderColor!=Value) { FBorderColor = Value; Paint(); Invalidate(); } } //--------------------------------------------------------------------------- bool __fastcall TPLayerImage::PositionChange() { return true; } //--------------------------------------------------------------------------- // ¼¼ºÎÀû ±â´É °ü·Ã //--------------------------------------------------------------------------- // Background color¸¦ ¼³Á¤ÇÑ´Ù. void __fastcall TPLayerImage::SetBackgroundColor(COLORREF bgcolor, RGBQUAD *rgb) { FLayerBGColor = bgcolor; if (FBitmap->BitsPerPixel == 8) { memcpy(Frgb, rgb, sizeof(RGBQUAD) * 256); } } //--------------------------------------------------------------------------- // ÇöÀç palette¸¦ rgb·Î ¼³Á¤ÇÑ´Ù. void __fastcall TPLayerImage::ChangeRGBColors(RGBQUAD *rgb) { memcpy(Frgb, rgb, sizeof(RGBQUAD) * 256); if (FBeforeBitmap) FBeforeBitmap->PutColors(0, 256, rgb); FBitmap->PutColors(0, 256, rgb); if (FLayerMask) FLayerMask->PutColors(0, 256, rgb); if (FAfterBitmap) FAfterBitmap->PutColors(0, 256, rgb); if (FAfterMask) FAfterMask->PutColors(0, 256, rgb); if (FSubBitmap) FSubBitmap->PutColors(0, 256, rgb); if (FSubMask) FSubMask->PutColors(0, 256, rgb); } //--------------------------------------------------------------------------- void __fastcall TPLayerImage::RearrangeColors(RGBQUAD *rgb) { memcpy(Frgb, rgb, sizeof(RGBQUAD) * 256); MakeBeforeBitmap(); } //--------------------------------------------------------------------------- void __fastcall TPLayerImage::FloodFill(TPoint start, TFloodFillReadLayer read, TFloodFillSave save, TRect &rcBound, TRect *pZone) { int x, y, savex, savey, xr, xl; TPoint now, *sp; TList *stack; Byte *p, *lp; TRect rcZone = pZone ? *pZone : Rect(0, 0, FBitmap->Width, FBitmap->Height); stack = new TList; rcBound.Left = start.x; rcBound.Right = start.x+1; rcBound.Top = start.y; rcBound.Bottom = start.y+1; stack->Add(new TPoint(start)); FBitmap->StartScanLine(); FLayerMask->StartScanLine(); while (stack->Count) { sp = (TPoint *)stack->Last(); stack->Remove(sp); x = sp->x; y = sp->y; delete sp; if (yrcBound.Bottom-1) rcBound.Bottom = y+1; p = FBitmap->GetScanLine(y); lp = FLayerMask->GetScanLine(y); save(p, x); savex = x; while (1) { x++; if (xrcBound.Right-1) rcBound.Right = xr+1; x = savex; while (1) { x--; if (x>=rcZone.Left && read(p, lp, x)) save(p, x); else break; } xl = x+1; if (xlrcBound.Right-1) rcBound.Right = xl+1; FBitmap->PutScanLine(y); x = xl; savey = y; y++; if (yGetScanLine(y); lp = FLayerMask->GetScanLine(y); // if (yAdd(new TPoint(now)); } } // } x = xl; } y = savey; y--; if (y>=rcZone.Top) { p = FBitmap->GetScanLine(y); lp = FLayerMask->GetScanLine(y); // if (y>=rcZone.Top) { while (x<=xr) { if (!read(p, lp, x)) x++; else { while (1) { if (x<=xr) { if (read(p, lp, x)) x++; else { now.x = x-1; x++; break; } } else { now.x = x-1; break; } } now.y = y; stack->Add(new TPoint(now)); } } // } } } FBitmap->StopScanLine(); FLayerMask->StopScanLine(); delete stack; } //--------------------------------------------------------------------------- bool __fastcall TPLayerImage::FloodFillMask(TPoint start, TFloodFillReadLayer read, TFloodFillSave save, TRect &rcBound, TPBitmap *PMask, TRect *pZone) { int x, y, savex, savey, xr, xl; TPoint now, *sp; TList *stack = NULL; Byte *p, *m, *lp; TRect rcZone = pZone ? *pZone : Rect(0, 0, FBitmap->Width, FBitmap->Height); if (PMask==NULL) return false; if (!PMask->Create(FBitmap->Width, FBitmap->Height, 1)) return false; if ((stack = new TList)==NULL) goto fail; rcBound.Left = start.x; rcBound.Right = start.x+1; rcBound.Top = start.y; rcBound.Bottom = start.y+1; stack->Add(new TPoint(start)); FBitmap->StartScanLine(); FLayerMask->StartScanLine(); PMask->Lock(); PMask->Fill(0); while (stack->Count) { sp = (TPoint *)stack->Last(); stack->Remove(sp); x = sp->x; y = sp->y; delete sp; if (yrcBound.Bottom-1) rcBound.Bottom = y+1; p = FBitmap->GetScanLine(y); lp = FLayerMask->GetScanLine(y); m = PMask->ScanLine(y); *(m+(x>>3)) |= 0x80>>(x&7); if (save) save(p, x); savex = x; while (1) { x++; if (x>3))&(0x80>>(x&7)))==0 && read(p, lp, x)) { *(m+(x>>3)) |= 0x80>>(x&7); if (save) save(p, x); } else break; } xr = x-1; if (xrrcBound.Right-1) rcBound.Right = xr+1; x = savex; while (1) { x--; if (x>=rcZone.Left && (*(m+(x>>3))&(0x80>>(x&7)))==0 && read(p, lp, x)) { *(m+(x>>3)) |= 0x80>>(x&7); if (save) save(p, x); } else break; } xl = x+1; if (xlrcBound.Right-1) rcBound.Right = xl+1; x = xl; savey = y; y++; if (yGetScanLine(y); lp = FLayerMask->GetScanLine(y); m = PMask->ScanLine(y); // if (y>3))&(0x80>>(x&7))) || !read(p, lp, x)) x++; else { while (1) { if (x<=xr) { if ((*(m+(x>>3))&(0x80>>(x&7)))==0 && read(p, lp, x)) x++; else { now.x = x-1; x++; break; } } else { now.x = x-1; break; } } now.y = y; stack->Add(new TPoint(now)); } } // } x = xl; } y = savey; y--; if (y>=rcZone.Top) { p = FBitmap->GetScanLine(y); lp = FLayerMask->GetScanLine(y); m = PMask->ScanLine(y); // if (y>=rcZone.Top) { while (x<=xr) { if ((*(m+(x>>3))&(0x80>>(x&7))) || !read(p, lp, x)) x++; else { while (1) { if (x<=xr) { if ((*(m+(x>>3))&(0x80>>(x&7)))==0 && read(p, lp, x)) x++; else { now.x = x-1; x++; break; } } else { now.x = x-1; break; } } now.y = y; stack->Add(new TPoint(now)); } } // } } } PMask->Unlock(); FLayerMask->StopScanLine(); FBitmap->StopScanLine(); delete stack; return true; fail: if (stack) delete stack; return false; } //--------------------------------------------------------------------------- //--------------------------------------------------------------------------- //--------------------------------------------------------------------------- namespace Tplayerimage { void __fastcall PACKAGE Register() { TComponentClass classes[1] = {__classid(TPLayerImage)}; RegisterComponents("Texpia", classes, 0); } } //---------------------------------------------------------------------------