//--------------------------------------------------------------------------- #include #pragma hdrstop #include "YarnRepeat_F.h" #include "common.h" #include "Exception.h" #include "TClrUtil.hpp" #include "YarnDesign_F.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma link "TPImage" #pragma link "TPSelectImage" #pragma link "TPSpin" #pragma resource "*.dfm" //--------------------------------------------------------------------------- #define IDS_YARNREPEATFORM StringTable[0] #define IDS_OK StringTable[1] #define IDS_CANCEL StringTable[2] #define IDS_CHOICECOLOR StringTable[3] #define IDS_PENWIDTH StringTable[4] #define IDS_REDO StringTable[5] TYarnRepeatForm *YarnRepeatForm; //--------------------------------------------------------------------------- __fastcall TYarnRepeatForm::TYarnRepeatForm(TComponent* Owner) : TForm(Owner) { //=========================================================== StringTable.Create(DirectoryBin, Language, "YarnRepeat"); SetSmallFont(Font); Caption = IDS_YARNREPEATFORM; Label1->Caption = IDS_COMMON_ZOOM; bbOK->Caption = IDS_OK; BitBtn1->Caption = IDS_CANCEL; Label2->Caption = IDS_CHOICECOLOR; Label3->Caption = IDS_PENWIDTH; sbGrid->Hint = IDS_COMMON_GRID; sbWindow->Hint = IDS_COMMON_WINDOW; sbMove->Hint = IDS_COMMON_MOVE; sbCopy->Hint = IDS_COMMON_COPY; sbUndo->Hint = IDS_COMMON_UNDO; //=========================================================== // qe } //--------------------------------------------------------------------------- void __fastcall TYarnRepeatForm::FormCreate(TObject *Sender) { WorkArea = NULL; WorkArea = new TPWorkArea(&(Image->Bitmap)); // added qe WorkArea->undoMax = 0; YarnUndo = new TSimpleUndo; YarnUndo->Set(Image->Bitmap, WorkArea); TempBitmap = NULL; Bitmap = NULL; // if ((TempBitmap = new TTexpiaBitmap) == NULL) goto fail; // if ((Bitmap = new TTexpiaBitmap) == NULL) goto fail; // return; fail: EXCEPTION_MESSAGE_OK(EC_MEMORY_LACK); //PostMessage(hWnd, TPM_EXITFUNCTION, 0, 0); } //--------------------------------------------------------------------------- bool __fastcall TYarnRepeatForm::InitForm8(TPMemoryArea *TP, RGBQUAD *rgb) { Byte *DP, *SP; int hx, dx, w, h, x, y; TEMP = TP; trgb = rgb; w = TEMP->size.x; h = TEMP->size.y; bpp = 8; if (Image->Bitmap->Create(w, h, 8, rgb) == false) goto fail; Image->SetZoom(8, 1); ScrollBarEnabled(); CurrentZoom = 4; hx = w / 2; if (Image->Bitmap->StartScanLine() == false) goto fail; for (y = 0; y < h; y++) { DP = Image->Bitmap->GetScanLine(y); SP = TEMP->pointer(0, y); for (x = 0, dx = hx; x < w; x++, DP++, dx++) { if (dx == w) dx = 0; *DP = SP[dx]; } Image->Bitmap->PutScanLine(y); } Image->Bitmap->StopScanLine(); Image->PositionX = 0; Image->PositionY = 0; pencolor = 1; Shape->Brush->Color = clWhite; Image->Repaint(); Down = false; return true; fail: Image->Bitmap->StopScanLine(); return false; } //--------------------------------------------------------------------------- bool __fastcall TYarnRepeatForm::InitForm15(TPMemoryArea *TP) { WORD *DP, *SP; int hx, dx, w, h, x, y,i; RGBQUAD q; WTEMP = TP; w = WTEMP->size.x; h = WTEMP->size.y; bpp = 16; if (Image->Bitmap->Create(w, h, 16) == false) goto fail; Image->SetZoom(8, 1); ScrollBarEnabled(); CurrentZoom = 4; hx = w / 2; if (Image->Bitmap->StartScanLine() == false) goto fail; for (y = 0; y < h; y++) { DP = (WORD *)Image->Bitmap->GetScanLine(y); SP = WTEMP->pointer(0, y); for (x = 0, dx = hx; x < w; x++, DP++, dx++) { if (dx == w) dx = 0; *DP = SP[dx]; } Image->Bitmap->PutScanLine(y); } Image->Bitmap->StopScanLine(); Image->PositionX = 0; Image->PositionY = 0; q.rgbRed = 255; q.rgbGreen = 255; q.rgbBlue = 255; pencolor = RGBToColor15(q); Shape->Brush->Color = clWhite; Image->Repaint(); Down = false; for(i=0;i<9;i++)nearc[i]=clWhite; return true; fail: Image->Bitmap->StopScanLine(); return false; } //--------------------------------------------------------------------------- void __fastcall TYarnRepeatForm::sbZoomClick(TObject *Sender) { TSpeedButton *sb = (TSpeedButton *) Sender; if (sb->Tag) { CurrentZoom++; if (CurrentZoom > 5) CurrentZoom = 5; } else { CurrentZoom--; if (CurrentZoom < 1) CurrentZoom = 1; } switch (CurrentZoom) { case 1: Image->SetZoom(1, 1); ZoomText->Caption = "1"; break; case 2: Image->SetZoom(2, 1); ZoomText->Caption = "2"; break; case 3: Image->SetZoom(4, 1); ZoomText->Caption = "3"; break; case 4: Image->SetZoom(8, 1); ZoomText->Caption = "4"; break; case 5: Image->SetZoom(16, 1); ZoomText->Caption = "5"; break; } ScrollBarEnabled(); } //--------------------------------------------------------------------------- void __fastcall TYarnRepeatForm::ScrollBarEnabled() { int zw, zh; zw = Image->Bitmap->Width * Image->ZoomIn; zh = Image->Bitmap->Height * Image->ZoomIn; if (Image->Width < zw) { ScrollBarH->Enabled = true; ScrollBarH->Max = zw - Image->Width; } if (Image->Height < zh) { ScrollBarV->Enabled = true; ScrollBarV->Max = zh - Image->Height; } } //--------------------------------------------------------------------------- void __fastcall TYarnRepeatForm::ScrollBarHScroll(TObject *Sender, TScrollCode ScrollCode, int &ScrollPos) { Image->PositionX = ScrollBarH->Position / Image->ZoomIn; } //--------------------------------------------------------------------------- void __fastcall TYarnRepeatForm::ScrollBarVScroll(TObject *Sender, TScrollCode ScrollCode, int &ScrollPos) { Image->PositionY = ScrollBarV->Position / Image->ZoomIn; } //--------------------------------------------------------------------------- void __fastcall TYarnRepeatForm::sbGridClick(TObject *Sender) { Image->Grid = sbGrid->Down; } //--------------------------------------------------------------------------- void __fastcall TYarnRepeatForm::ImageMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) { switch (menuitem) { case YOff: int pw, bx, by; bx = X/Image->ZoomIn+Image->PositionX; by = Y/Image->ZoomIn+Image->PositionY; if (bx < 0 || bx > Image->Bitmap->Width || by < 0 || by > Image->Bitmap->Height) return; if (Button == mbMiddle || (Shift.Contains(ssShift) && Button==mbLeft)) { COLORREF c = Image->Bitmap->GetPixelColor(bx, by); for(int i=0;i<3;i++)for(int j=0;j<3;j++){ if(bx+i-1>=0 && bx+i-1<=Image->Width-1 && by+j-1>=0 && by+j-1<=Image->Height-1) nearc[i+j*3]=Image->Bitmap->GetPixelColor(bx+i-1, by+j-1); else nearc[i+j*3]=c; } if (bpp == 8) { if (c > 1) pencolor = ((c - 2) / 7) * 7 + 2; else pencolor = c; Shape->Brush->Color = (TColor) RGB(trgb[pencolor].rgbRed, trgb[pencolor].rgbGreen, trgb[pencolor].rgbBlue); } else { RGBQUAD q; TColor2RGB(TColor(c), q.rgbRed, q.rgbGreen, q.rgbBlue); pencolor = RGBToColor15(q); Shape->Brush->Color = TColor(c); } } else if (Button == mbLeft) { Down = true; pw = spPenWidth->Value; if (pw == 1) { ls = 0; rs = 1; } else { ls = pw / 2.0; rs = pw - ls; } if (Image->Bitmap->StartScanLine() == false) goto fail; YarnUndo->Save(Rect(0, 0, Image->Bitmap->Width, Image->Bitmap->Height)); DrawDot(X, Y); } return; fail: Image->Bitmap->StopScanLine(); Down = false; EXCEPTION_MESSAGE_OK(EC_MEMORY_LACK); break; case YWin: if (Button == mbLeft) WindowMouseDown(X, Y); break; case YMove: case YCopy: MoveCopyMouseDown(X, Y); break; } } //--------------------------------------------------------------------------- void __fastcall TYarnRepeatForm::ImageMouseMove(TObject *Sender, TShiftState Shift, int X, int Y) { switch(menuitem){ case YOff: if (Down) DrawDot(X, Y); break; case YWin: WindowMouseMove(X, Y); break; case YMove: case YCopy: MoveCopyMouseMove(X, Y); break; } } //--------------------------------------------------------------------------- void __fastcall TYarnRepeatForm::ImageMouseUp(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) { if (Down) { DrawDot(X, Y); Image->Bitmap->StopScanLine(); Down = false; } } //--------------------------------------------------------------------------- void __fastcall TYarnRepeatForm::bbOKClick(TObject *Sender) { Byte *dp, *sp; WORD *DP, *SP; int y; if(menuitem == TMove || menuitem == TCopy){ //qe Image->Bitmap->Copy(Bitmap, SRCCOPY); Image->Repaint(); } if (bpp == 8) { if (!(Image->Bitmap->StartScanLine())) goto fail; for (y = 0; y < TEMP->size.y; y++) { sp = (Byte *) Image->Bitmap->GetScanLine(y); dp = TEMP->pointer(0, y); memcpy(dp, sp, TEMP->size.x); } Image->Bitmap->StopScanLine(); } else { if (!(Image->Bitmap->StartScanLine())) goto fail; for (y = 0; y < WTEMP->size.y; y++) { SP = (WORD *) Image->Bitmap->GetScanLine(y); DP = WTEMP->pointer(0, y); memcpy(DP, SP, sizeof(WORD)*WTEMP->size.x); } Image->Bitmap->StopScanLine(); } return; fail: EXCEPTION_MESSAGE_OK(EC_MEMORY_LACK); } //--------------------------------------------------------------------------- void __fastcall TYarnRepeatForm::DrawDot(int X, int Y) { Byte *DP; WORD *WDP; RGBQUAD rgb; int sx, ex, sy, ey, i, j; X = X/Image->ZoomIn + Image->PositionX; Y = Y/Image->ZoomIn + Image->PositionY; sx = X - ls; ex = X + rs; sy = Y - ls; ey = Y + rs; if (sx < 0) sx = 0; if (ex > Image->Bitmap->Width) ex = Image->Bitmap->Width; if (sy < 0) sy = 0; if (ey > Image->Bitmap->Height) ey = Image->Bitmap->Height; if (bpp == 8) { for (i = sy; i < ey; i++) { DP = (Byte *) Image->Bitmap->GetScanLine(i); for (j = sx; j < ex; j++) { if (pencolor > 1) DP[j] = pencolor + (rand() % 7); else DP[j] = 1; } Image->Bitmap->PutScanLine(i); } } else { for (i = sy; i < ey; i++) { WDP = (WORD *) Image->Bitmap->GetScanLine(i); for (j = sx; j < ex; j++) { static int i=0; int mapping[9]; mapping[0]=0;mapping[1]=1;mapping[2]=2;mapping[3]=5;mapping[4]=8; mapping[5]=7;mapping[6]=6;mapping[7]=3;mapping[8]=4; TColor2RGB(TColor(nearc[mapping[ ( (i++)+rand()%2 )%9 ]]), rgb.rgbRed, rgb.rgbGreen, rgb.rgbBlue); i++; if(i==9)i=0; WDP[j] = RGBToColor15(rgb); } Image->Bitmap->PutScanLine(i); } } Image->Repaint(); } //--------------------------------------------------------------------------- //-------------------------------------------------------from this to end ..added by qe void __fastcall TYarnRepeatForm::sbWindowClick(TObject *Sender) { if(menuitem == YMove || menuitem == YCopy) ResetMoveCopyMode(); if(sbWindow->Down){ menuitem = YWin; InitWin(); WinPanel->Visible = true; sbRect->Down =true; }else{ sbWinExitClick(this); } } //-------------------------------------------------------------------------- void __fastcall TYarnRepeatForm::sbMoveCopyClick(TObject *Sender) { bool tp = false, bt = false; TSpeedButton *sp = (TSpeedButton *) Sender; if(menuitem == YMove || menuitem == YCopy) ResetMoveCopyMode(); if(sp->Down){ sbWinExitClick(this); if(sp == sbMove){ menuitem = YMove; } else if (sp == sbCopy) { menuitem = YCopy; } RECT rc = WorkArea->Range; if(bpp == 8){ tp = TempBitmap->Create(rc.right - rc.left, rc.bottom - rc.top, 8, trgb); if (tp == false) goto fail; TempBitmap->CopyFromRect(Image->Bitmap, rc.left, rc.top, SRCCOPY); bt = Bitmap->Create(Image->Bitmap->Width, Image->Bitmap->Height, 8, trgb); if (bt == false) goto fail; Bitmap->Copy(Image->Bitmap, SRCCOPY); }else if(bpp == 16){ tp = TempBitmap->Create(rc.right - rc.left, rc.bottom - rc.top, 16); if (tp == false) goto fail; TempBitmap->CopyFromRect(Image->Bitmap, rc.left, rc.top, SRCCOPY); bt = Bitmap->Create(Image->Bitmap->Width, Image->Bitmap->Height, 16); if (bt == false) goto fail; Bitmap->Copy(Image->Bitmap, SRCCOPY); } }else { menuitem = YOff; } return; fail: if (bt) Bitmap->Destroy(); if (tp) TempBitmap->Destroy(); EXCEPTION_MESSAGE_OK(EC_MEMORY_LACK); } //--------------------------------------------------------------------------- void __fastcall TYarnRepeatForm::ResetMoveCopyMode() { Image->Bitmap->Copy(Bitmap, SRCCOPY); Image->Repaint(); sbCopy->Down = false; sbMove->Down = false; menuitem = YOff; } //--------------------------------------------------------------------------- void __fastcall TYarnRepeatForm::InitWin() { /*WinPanel->BringToFront(); sbRect->Down =true; */ } //--------------------------------------------------------------------------- void __fastcall TYarnRepeatForm::Timer1Timer(TObject *Sender) { Image->OutlineBitmapRgn(); } //--------------------------------------------------------------------------- void __fastcall TYarnRepeatForm::sbWinExitClick(TObject *Sender) { sbWindow->Down = false; menuitem = YOff; WinStep = 0; WinPanel->Visible = false; Image->Repaint(); } //--------------------------------------------------------------------------- void __fastcall TYarnRepeatForm::WindowMouseDown(int X, int Y) //added by qe { X = X/Image->ZoomIn + Image->PositionX; Y = (Y)/Image->ZoomIn + Image->PositionY; if (WinStep == 0) { WorkArea->ResetRegion((TPItemImage*)Image); First = Point(X, Y); First.y = First.y + 1; IsDraw = false; WinStep = 1; } else { if (IsDraw) { WorkAreaDraw(); IsDraw = false; } Second = Point(X, Y); First.y = First.y - 1; //Second.y = Second.y -1; WorkArea->SetShapeRegion(Image->Bitmap, First, Second); StatusItem(1); WinStep = 0; } Image->Repaint(); } //---------------------------------------------------------------------------- void __fastcall TYarnRepeatForm::WindowMouseMove(int X, int Y) { X = X/Image->ZoomIn + Image->PositionX; Y = (Y)/Image->ZoomIn + Image->PositionY; if (WinStep > 0) { Image->Repaint(); /* if(IsDraw){ WorkAreaDraw(); IsDraw = false; } */ Second = Point(X, Y); Second.y = Second.y + 1; WorkAreaDraw(); IsDraw = true; } } //---------------------------------------------------------------------------- void __fastcall TYarnRepeatForm::MoveCopyMouseDown(int X, int Y) { int dx, dy, x, y; RECT rc = WorkArea->Range; Byte *DP; X = X/Image->ZoomIn + Image->PositionX; Y = Y/Image->ZoomIn + Image->PositionY; Image->Bitmap->Copy(Bitmap, SRCCOPY); YarnUndo->Save(Rect(0, 0, Image->Bitmap->Width, Image->Bitmap->Height)); if (menuitem == YMove) { if (Image->Bitmap->StartScanLine() == false) goto fail; for (y = rc.top; y < rc.bottom; y++) { DP = Image->Bitmap->GetScanLine(y); if(bpp == 8){memset(DP+rc.left, 1, rc.right - rc.left);} else if(bpp == 16) {memset(DP+rc.left*2, clWhite, (rc.right - rc.left)*2);} Image->Bitmap->PutScanLine(y); } Image->Bitmap->StopScanLine(); } dx = X - TempBitmap->Width / 2; dy = Y - TempBitmap->Height / 2; if (dx < 0) dx = 0; if (dx + TempBitmap->Width > Image->Bitmap->Width) dx = Image->Bitmap->Width - TempBitmap->Width; if (dy < 0) dy = 0; if (dy + TempBitmap->Height > Image->Bitmap->Height) dx = Image->Bitmap->Height - TempBitmap->Height; Image->Bitmap->CopyToRect(dx, dy, TempBitmap, SRCCOPY); if (menuitem == YMove) { sbMove->Down = false; menuitem = YOff; WorkArea->ResetRegion((TPItemImage*) Image); WorkArea->SetShapeRegion(Image->Bitmap, Point(dx, dy), Point(dx+TempBitmap->Width - 1, dy+TempBitmap->Height - 1)); } else { Bitmap->Copy(Image->Bitmap, SRCCOPY); } Image->Repaint(); return; fail: Image->Bitmap->StopScanLine(); EXCEPTION_MESSAGE_OK(EC_MEMORY_LACK); } //--------------------------------------------------------------------------- void __fastcall TYarnRepeatForm::MoveCopyMouseMove(int X, int Y) { int dx, dy; X = X/Image->ZoomIn + Image->PositionX; Y = Y/Image->ZoomIn + Image->PositionY; Image->Bitmap->Copy(Bitmap, SRCCOPY); dx = X - TempBitmap->Width / 2; dy = Y - TempBitmap->Height / 2; if (dx < 0) dx = 0; if (dx + TempBitmap->Width > Image->Bitmap->Width) dx = Image->Bitmap->Width - TempBitmap->Width; if (dy < 0) dy = 0; if (dy + TempBitmap->Height > Image->Bitmap->Height) dy = Image->Bitmap->Height - TempBitmap->Height; Image->Bitmap->CopyToRect(dx, dy, TempBitmap,SRCCOPY); Image->Repaint(); } //--------------------------------------------------------------------------- void __fastcall TYarnRepeatForm::WorkAreaDraw() { HDC hDC = NULL; HBRUSH hOldBrush; HPEN hOldPen, hPen = NULL; int nDrawMode, fx, fy, sx, sy; COLORREF clOldBk; hDC=Image->Canvas->Handle; clOldBk = SetBkColor(hDC, clWhite); hPen = CreatePen(PS_DOT, 1, clBlack); hOldPen = SelectObject(hDC, hPen); hOldBrush = SelectObject(hDC, GetStockObject(NULL_BRUSH)); fx = (First.x -Image->PositionX)*Image->ZoomIn; fy = (First.y - Image->PositionY)*Image->ZoomIn; sx = (Second.x -Image->PositionX)*Image->ZoomIn; sy = (Second.y - Image->PositionY )*Image->ZoomIn; Rectangle(hDC,min(fx, sx), min(fy, sy)-Image->ZoomIn, max(fx,sx)+Image->ZoomIn, max(fy, sy)); SetBkColor(hDC, clOldBk); SelectObject(hDC, hOldPen); DeleteObject(hPen); SelectObject(hDC, hOldBrush); // Image->Canvas->DeleteDC(hDC); return; fail: //if (hDC) Image->Canvas->DeleteDC(hDC); EXCEPTION_MESSAGE_OK(EC_MEMORY_LACK); } //--------------------------------------------------------------------------- void __fastcall TYarnRepeatForm::StatusItem(int i) { sbMove->Enabled = i; sbCopy->Enabled = i; sbOff->Enabled = i; } //--------------------------------------------------------------------------- void __fastcall TYarnRepeatForm::sbOffClick(TObject *Sender) { WorkArea->ResetRegion((TPItemImage*) Image); Image->Repaint(); StatusItem(0); sbWinExitClick(this); } //--------------------------------------------------------------------------- void __fastcall TYarnRepeatForm::sbUndoClick(TObject *Sender) { if(menuitem == YCopy || menuitem == YMove) ResetMoveCopyMode(); if (sbUndo->Tag) { sbUndo->Hint = IDS_REDO; sbUndo->Tag = 0; } else { sbUndo->Hint = IDS_COMMON_UNDO; sbUndo->Tag = 1; } YarnUndo->Read(Rect(0, 0, Image->Bitmap->Width, Image->Bitmap->Height)); Image->Repaint(); } //--------------------------------------------------------------------------- void __fastcall TYarnRepeatForm::FormClose(TObject *Sender, TCloseAction &Action) { if(WorkArea) delete WorkArea; if(YarnUndo) delete YarnUndo; if(TempBitmap) delete TempBitmap; if(Bitmap) delete Bitmap; } //---------------------------------------------------------------------------