//--------------------------------------------------------------------------- #include #pragma hdrstop #include "Separate_F.h" #include "CommonPrint.h" #include "MainImage.h" #include "Palette.h" #include "Undo.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma link "Selector" #pragma link "TPSpin" #pragma resource "*.dfm" //--------------------------------------------------------------------------- #define IDS_RESTORE StringTable[0] #define IDS_OVERLAP StringTable[1] //--------------------------------------------------------------------------- TSeparateForm *SeparateForm; //--------------------------------------------------------------------------- __fastcall TSeparateForm::TSeparateForm(TComponent* Owner) : TForm(Owner) { hWnd = ((TWinControl *)Owner)->Handle; } //--------------------------------------------------------------------------- void __fastcall TSeparateForm::FormCreate(TObject *Sender) { //========================================= StringTable.Create(DirectoryItem, Language, "Separate"); SetSmallFont(Font); SetSmallFont(Label1->Font); sbRestore->Caption = IDS_RESTORE; sbPrint->Caption = IDS_COMMON_PRINT; sbRun->Caption = IDS_COMMON_RUN; sbExit->Caption = IDS_COMMON_EXIT; Label1->Caption = IDS_OVERLAP; sbNew->Hint = IDS_COMMON_NEW; sbAll->Hint = IDS_COMMON_ALL; sbBlock->Hint = IDS_COMMON_BLOCKCOLOR; sbDelete->Hint = IDS_COMMON_INSERTCOLOR; sbBefore->Hint = IDS_COMMON_BEFORECOLOR; //========================================== } //--------------------------------------------------------------------------- void __fastcall TSeparateForm::sbRestoreClick(TObject *Sender) { Undo->Read(); ::RepaintColor(); sbRun->Enabled = true; sbRestore->Enabled = false; } //--------------------------------------------------------------------------- void __fastcall TSeparateForm::sbPrintClick(TObject *Sender) { TCommonPrintForm *Form = NULL; TCursor oldcursor; int DPI, x, y; TTexpiaBitmap *tb; TTexpiaBitmap *Bitmap = NULL; Byte *sp, *dp, * mp; RGBQUAD rgb[256]; RECT Src; if ((Form = new TCommonPrintForm((TComponent *)NULL))==NULL) goto fail; if (Form->ShowModal() == mrOk) { oldcursor = Screen->Cursor; Screen->Cursor = crHourGlass; tb = MainImageForm->iMainImage->Bitmap; DPI = MainImageForm->CanvasInfor.DotsPerInch; tb->GetColors(0, 256, rgb); if ((Bitmap = new TTexpiaBitmap)==NULL) goto fail; if (MainImageForm->WorkArea->Mask) Src = MainImageForm->WorkArea->Range; else Src = Rect(0, 0, tb->Width, tb->Height); if (!Bitmap->Create(Src.right-Src.left, Src.bottom-Src.top, 8, rgb)) goto fail; if (!tb->StartScanLine()) goto fail; if (!Bitmap->StartScanLine()) goto fail; if (MainImageForm->WorkArea->Mask) { if (!MainImageForm->WorkArea->Mask->StartScanLine()) goto fail; for (y=0; yHeight; y++) { sp = tb->GetScanLine(Src.top+y); dp = (Byte *)Bitmap->GetScanLine(y); mp = MainImageForm->WorkArea->Mask->GetScanLine(y); for (x=0; xWidth; x++) { dp[x] = sp[Src.left+x]==BackgroundIndex || mp[x]==0 ? 255 : 0; } Bitmap->PutScanLine(y); } MainImageForm->WorkArea->Mask->StopScanLine(); } else { for (y=0; yHeight; y++) { sp = tb->GetScanLine(Src.top+y); dp = (Byte *)Bitmap->GetScanLine(y); for (x=0; xWidth; x++) { dp[x] = sp[Src.left+x]==BackgroundIndex ? 255 : 0; } Bitmap->PutScanLine(y); } } Bitmap->StopScanLine(); tb->StopScanLine(); if (!SendToPrinter(Bitmap, DPI, "Separate - "+MainImageForm->FileName, Form->PageEdit->Value, Form->cbMethod->ItemIndex)) goto fail; delete Bitmap; Bitmap = NULL; Screen->Cursor = oldcursor; } delete Form; return; fail: if (Bitmap) { if (MainImageForm->WorkArea->Mask) MainImageForm->WorkArea->Mask->StopScanLine(); tb->StopScanLine(); delete Bitmap; } if (Form) delete Form; Screen->Cursor = oldcursor; EXCEPTION_MESSAGE_OK(EC_MEMORY_LACK); } //---------------------------------------------------------------------------- void __fastcall TSeparateForm::sbRunClick(TObject *Sender) { int i, j, x, y; TCursor oldcursor; Byte *mp, *dp[11], *sp, *mp1; TTexpiaBitmap *tb; TTexpiaBitmap *Bitmap; RGBQUAD rgb[256]; RECT Src; bool undoSw = false; oldcursor = Screen->Cursor; Screen->Cursor = crHourGlass; MainImageForm->Palette->ToRGBQUAD(rgb, 256); Overlap = spEditOverlap->Value; tb = MainImageForm->iMainImage->Bitmap; if (MainImageForm->WorkArea->Mask) Src = MainImageForm->WorkArea->Range; else Src = Rect(0, 0, tb->Width, tb->Height); if ((undoSw = MainImageForm->UndoSave(UK_ALL, Src)) == false) goto fail; Bitmap = Undo->Last->Bitmap; if (MainImageForm->WorkArea->Mask) { if (!(Bitmap->StartScanLine())) goto fail; if (!(MainImageForm->WorkArea->Mask->StartScanLineN(2))) goto fail; if (Overlap>0) { if (!(tb->StartScanLineN(2*Overlap+1))) goto fail; for (y=0; yHeight; y++) { memset(tb->GetScanLine(Src.top+y)+Src.left, BackgroundIndex, Bitmap->Width); tb->PutScanLine(Src.top+y); } for (i=0; i<2*Overlap; i++) { j = i-Overlap; dp[i] = j>=0 && jHeight ? tb->GetScanLineN(Src.top+j, j)+Src.left : NULL; } for (y=0; yHeight; y++) { sp = Bitmap->GetScanLine(y); dp[2*Overlap] = y+OverlapHeight ? tb->GetScanLineN(Src.top+y+Overlap, (y+Overlap)%(2*Overlap+1))+Src.left : NULL; mp1 = MainImageForm->WorkArea->Mask->GetScanLineN(y, 0); for (x=0; xWidth; x++) { if (*(mp1+x)) { if (SepSelector->ChoiceColor->Number[*(sp+x)]<255) { for (i=0; i<2*Overlap+1; i++) { if (dp[i]) { #ifdef TPDEBUG if (y-Overlap+i>=0 && y-Overlap+iHeight) mp = MainImageForm->WorkArea->Mask->GetScanLine(y-Overlap+i, 1); else SHOWDEBUG; #else mp = MainImageForm->WorkArea->Mask->GetScanLineN(y-Overlap+i, 1); #endif for (j=x-Overlap; j<=x+Overlap; j++) { if (j>=0 && jWidth) { if (*(mp+j)) *(dp[i]+j) = *(sp+x); } } } } } } else { *(dp[Overlap]+x) = *(sp+x); } } if (dp[0]) tb->PutScanLine(Src.top+y-Overlap, dp[0]-Src.left); for (i=0; i<2*Overlap; i++) dp[i] = dp[i+1]; } for (i = 0; i < Overlap; i++) if (dp[i]) tb->PutScanLine(Src.top+y-Overlap+i, dp[i]-Src.left); tb->StopScanLine(); } else { if (!(tb->StartScanLine())) goto fail; for (y=0; yHeight; y++) { sp = Bitmap->GetScanLine(y); dp[0] = tb->GetScanLine(Src.top+y)+Src.left; mp = MainImageForm->WorkArea->Mask->GetScanLine(y); for (x=0; xWidth; x++, mp++) { if (*mp) { *(dp[0]+x) = SepSelector->Number[*(sp+x)]Count ? *(sp+x) : BackgroundIndex; } else { *(dp[0]+x) = *(sp+x); } } tb->PutScanLine(Src.top+y); } tb->StopScanLine(); } Bitmap->StopScanLine(); MainImageForm->WorkArea->Mask->StopScanLine(); } else { if (!(Bitmap->StartScanLine())) goto fail; if (Overlap>0) { if (!(tb->StartScanLineN(2*Overlap+1))) goto fail; for (y=0; yHeight; y++) { memset(tb->GetScanLine(y), BackgroundIndex, Bitmap->Width); tb->PutScanLine(y); } for (i=0; i<2*Overlap; i++) { j = i-Overlap; dp[i] = j>=0 && jHeight ? tb->GetScanLineN(j, j) : NULL; } for (y=0; yHeight; y++) { sp = Bitmap->GetScanLine(y); dp[2*Overlap] = y+OverlapHeight ? tb->GetScanLineN(y+Overlap, (y+Overlap)%(2*Overlap+1)) : NULL; for (x=0; xWidth; x++) { if (SepSelector->ChoiceColor->Number[*(sp+x)]<255) { for (i=0; i<2*Overlap+1; i++) { if (dp[i]) { for (j=x-Overlap; j<=x+Overlap; j++) { if (j>=0 && jWidth) { *(dp[i]+j) = *(sp+x); } } } } } } if (dp[0]) tb->PutScanLine(y-Overlap, dp[0]); for (i=0; i<2*Overlap; i++) dp[i] = dp[i+1]; } for (i=0; iPutScanLineN(y-Overlap+i, i); tb->StopScanLine(); } else { if (!(tb->StartScanLine())) goto fail; for (y=0; yHeight; y++) { sp = Bitmap->GetScanLine(y); dp[0] = tb->GetScanLine(y); for (x=0; xWidth; x++) { *(dp[0]+x) = SepSelector->Number[*(sp+x)]Count ? *(sp+x) : BackgroundIndex; } tb->PutScanLine(y); } tb->StopScanLine(); } Bitmap->StopScanLine(); } ::RepaintImage(); sbRun->Enabled = false; sbRestore->Enabled = true; Screen->Cursor = oldcursor; return; fail: tb->StopScanLine(); Bitmap->StopScanLine(); MainImageForm->WorkArea->Mask->StopScanLine(); Screen->Cursor = oldcursor; if (undoSw == true) Undo->RemoveLast(); EXCEPTION_MESSAGE_OK(EC_MEMORY_LACK); } //--------------------------------------------------------------------------- void __fastcall TSeparateForm::sbExitClick(TObject *Sender) { PostMessage(hWnd, TPM_EXITFUNCTION, 0, 0); } //--------------------------------------------------------------------------- void __fastcall TSeparateForm::sbNewClick(TObject *Sender) { SepSelector->Initial(); } //--------------------------------------------------------------------------- void __fastcall TSeparateForm::sbAllClick(TObject *Sender) { TCursor cursor; cursor = Screen->Cursor; Screen->Cursor = crHourGlass; if (MainImageForm->WorkArea->Mask) MainImageForm->SearchWorkAreaColor(SepSelector->ChoiceColor); else MainImageForm->SearchWholeColor(SepSelector->ChoiceColor); SepSelector->Invalid(); Screen->Cursor = cursor; } //--------------------------------------------------------------------------- void __fastcall TSeparateForm::sbDeleteClick(TObject *Sender) { if (sbDelete->Down) sbDelete->Hint = IDS_COMMON_DELETECOLOR; else sbDelete->Hint = IDS_COMMON_INSERTCOLOR; } //--------------------------------------------------------------------------- void __fastcall TSeparateForm::sbBlockClick(TObject *Sender) { if (sbBlock->Down) sbBlock->Hint = IDS_COMMON_ONECOLOR; else sbBlock->Hint = IDS_COMMON_BLOCKCOLOR; } //--------------------------------------------------------------------------- void __fastcall TSeparateForm::sbBeforeClick(TObject *Sender) { SepSelector->LoadFromFile(DirectoryItem+"\\Separate.Ini", "ColorSelector"); } //--------------------------------------------------------------------------- // Public Function //--------------------------------------------------------------------------- void __fastcall TSeparateForm::InitForm() { ParentHeight = Parent->Height+20; ClientHeight = 153; Parent->Height = ParentHeight+ClientHeight; sbRestore->Enabled = false; sbRun->Enabled = true; SepSelector->SetPalette(MainImageForm->Palette); SepSelector->Initial(); } //--------------------------------------------------------------------------- void __fastcall TSeparateForm::ExitForm() { SepSelector->SaveToFile(DirectoryItem+"\\Separate.Ini", "ColorSelector"); } //--------------------------------------------------------------------------- void __fastcall TSeparateForm::SepSelectorColor(int Value) { SepSelector->AddColor(Value); sbRun->Enabled = true; } //--------------------------------------------------------------------------- void __fastcall TSeparateForm::iMainImageMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) { TRect r; Byte *scan, c; int ix, iy; TPItemImage *MainImage = (TPItemImage *) Sender; if (Button==mbMiddle || (Shift.Contains(ssShift) && Button==mbLeft)) { if (sbBlock->Down) { c = MainImage->Bitmap->GetPixelColor(X, Y); if (sbDelete->Down) { SepSelector->DeleteColor(c); } else { SepSelector->AddColor(c); } } else { if (step>0) { Temp.right = X; Temp.bottom = Y; MainImageForm->DrawRectangleLocate(Temp); if (Temp.left>Temp.right) { r.Left = Temp.right; r.Right = Temp.left; } else { r.Left = Temp.left; r.Right = Temp.right; } if (Temp.top>Temp.bottom) { r.Top = Temp.bottom; r.Bottom = Temp.top; } else { r.Top = Temp.top; r.Bottom = Temp.bottom; } if (!(MainImage->Bitmap->StartScanLine())) goto fail; if (sbDelete->Down) { for (iy=r.Top; iyBitmap->GetScanLine(iy); for (ix=r.Left; ixDeleteColor(*(scan+ix)); } } } else { for (iy=r.Top; iyBitmap->GetScanLine(iy); for (ix=r.Left; ixAddColor(*(scan+ix)); } } } MainImage->Bitmap->StopScanLine(); step = 0; IsDraw = false; } else { Temp.left = X; Temp.top = Y; Temp.bottom = Temp.top; Temp.right = Temp.left; MainImageForm->DrawRectangleLocate(Temp); IsDraw = true; step = 1; } } } return; fail: MainImage->Bitmap->StopScanLine(); EXCEPTION_MESSAGE_OK(EC_MEMORY_LACK); } //--------------------------------------------------------------------------- void __fastcall TSeparateForm::iMainImageMouseMove(TObject *Sender, TShiftState Shift, int X, int Y) { if (step > 0) { if (IsDraw) MainImageForm->DrawRectangleLocate(Temp); Temp.right = X; Temp.bottom = Y; MainImageForm->DrawRectangleLocate(Temp); IsDraw = true; } } //--------------------------------------------------------------------------- void __fastcall TSeparateForm::iMainImagePaint() { IsDraw = false; } //--------------------------------------------------------------------------- void __fastcall TSeparateForm::spEditOverlapChange(TObject *Sender) { if (spEditOverlap->Text.Length() > 0) { if (spEditOverlap->Value > spEditOverlap->MaxValue) spEditOverlap->Value = spEditOverlap->MaxValue; else if (spEditOverlap->Value < spEditOverlap->MinValue) spEditOverlap->Value = spEditOverlap->MinValue; } } //---------------------------------------------------------------------------