//--------------------------------------------------------------------------- #include #include #pragma hdrstop #include "Grouping_F.h" #include "MainImage.h" #include "Palette.h" #include "StatusProgress.h" #include "Undo.h" #include "MainMenu.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma link "Selector" #pragma link "TPSpin" #pragma resource "*.dfm" //--------------------------------------------------------------------------- #define IDS_TYPE StringTable[0] #define IDS_FILLTER StringTable[1] #define IDS_USERCHOOSE StringTable[2] #define IDS_REARRANGE StringTable[3] #define IDS_COLORNUMBER StringTable[4] #define IDS_SIZE StringTable[5] #define IDS_SHAPE StringTable[6] //--------------------------------------------------------------------------- TGroupingForm *GroupingForm; //--------------------------------------------------------------------------- // TAutoGrouping //--------------------------------------------------------------------------- __fastcall TAutoGrouping::TAutoGrouping(TTexpiaBitmap *bm) { Bitmap = bm; FRed = NULL; FGreen = NULL; FBlue = NULL; #ifdef TPDEBUG if ((FRed = new TMemoryStream) == NULL) SHOWDEBUG; if ((FGreen = new TMemoryStream) == NULL) SHOWDEBUG; if ((FBlue = new TMemoryStream) == NULL) SHOWDEBUG; #else FRed = new TMemoryStream; FGreen = new TMemoryStream; FBlue = new TMemoryStream; #endif } //--------------------------------------------------------------------------- __fastcall TAutoGrouping::~TAutoGrouping() { delete FBlue; delete FGreen; delete FRed; } //--------------------------------------------------------------------------- void __fastcall TAutoGrouping::SetArea(RECT r) { FArea = r; Width = Area.right-Area.left; Height = Area.bottom-Area.top; FImageSize = Width*Height; FRed->SetSize(FImageSize); FillMemory(FRed->Memory, FRed->Size, 0); FGreen->SetSize(FImageSize); FillMemory(FGreen->Memory, FGreen->Size, 0); FBlue->SetSize(FImageSize); FillMemory(FBlue->Memory, FBlue->Size, 0); } //--------------------------------------------------------------------------- void __fastcall TAutoGrouping::SetRed(int APos, Byte Value) { FRed->Position = APos; FRed->Write(&Value, sizeof(Byte)); } //--------------------------------------------------------------------------- void __fastcall TAutoGrouping::SetGreen(int APos, Byte Value) { FGreen->Position = APos; FGreen->Write(&Value, sizeof(Byte)); } //--------------------------------------------------------------------------- void __fastcall TAutoGrouping::SetBlue(int APos, Byte Value) { FBlue->Position = APos; FBlue->Write(&Value, sizeof(Byte)); } //--------------------------------------------------------------------------- Byte __fastcall TAutoGrouping::GetRed(int APos) { Byte Result; FRed->Position = APos; FRed->Read(&Result, sizeof(Byte)); return Result; } //--------------------------------------------------------------------------- Byte __fastcall TAutoGrouping::GetGreen(int APos) { Byte Result; FGreen->Position = APos; FGreen->Read(&Result, sizeof(Byte)); return Result; } //--------------------------------------------------------------------------- Byte __fastcall TAutoGrouping::GetBlue(int APos) { Byte Result; FBlue->Position = APos; FBlue->Read(&Result, sizeof(Byte)); return Result; } //--------------------------------------------------------------------------- void __fastcall TAutoGrouping::Processing() { FRed->Position = 0; FGreen->Position = 0; FBlue->Position = 0; make_rgb_cube(); SelectColorBoxes(); SortColors(); Postprocessing(); } //--------------------------------------------------------------------------- void __fastcall TAutoGrouping::make_rgb_cube() { int i, j, k; Byte r, g, b; for (i=0; i=FBoxes[BoxIndex].Lo[ax0]+1) { ind[Fax1] = FBoxes[BoxIndex].Hi[Fax1]; while (ind[Fax1]+1>=FBoxes[BoxIndex].Lo[Fax1]+1) { ind[Fax2] = FBoxes[BoxIndex].Hi[Fax2]; while (ind[Fax2]+1>=FBoxes[BoxIndex].Lo[Fax2]+1) { if (FRGBCube[ind[0]][ind[1]][ind[2]]) { flag = true; break; } ind[Fax2]--; } if (flag) break; ind[Fax1]--; } if (flag) break; ind[ax0]--; } FBoxes[BoxIndex].Hi[ax0] = ind[ax0]; } } //--------------------------------------------------------------------------- void __fastcall TAutoGrouping::SelectColorBoxes() { int c, k, SelectedBox, TargetBox, Max, ax0, ax1, ax2, LongMax, ElementSum, PlaneSum; int ind[3]; ax1 = 0; ax2 = 0; PlaneSum = 0; for (c=0; c<=2; c++) { FBoxes[0].Lo[c] = 0; FBoxes[0].Hi[c] = ColorGrade-1; } FBoxes[0].NumElements = FImageSize; FNumBoxes = 1; Shrink(0); while (FNumBoxesLongMax) && ((FBoxes[c].Lo[0]!=FBoxes[c].Hi[0]) || (FBoxes[c].Lo[1]!=FBoxes[c].Hi[1]) || (FBoxes[c].Lo[2]!=FBoxes[c].Hi[2]))) { LongMax = FBoxes[c].NumElements; SelectedBox = c; } } if (SelectedBox==1000) break; ax0 = 0; Max = FBoxes[SelectedBox].Hi[ax0] - FBoxes[SelectedBox].Lo[ax0]; for (k=1; k<=2; k++) { c = FBoxes[SelectedBox].Hi[k] - FBoxes[SelectedBox].Lo[k]; if (Max(FBoxes[SelectedBox].NumElements/2)) break; ind[ax0]++; } if (ind[ax0]==FBoxes[SelectedBox].Hi[ax0]) { ind[ax0]--; ElementSum = ElementSum - PlaneSum; } for (c=0; c<=2; c++) { FBoxes[TargetBox].Lo[c] = FBoxes[SelectedBox].Lo[c]; FBoxes[TargetBox].Hi[c] = FBoxes[SelectedBox].Hi[c]; } FBoxes[TargetBox].Lo[ax0] = ind[ax0]+1; FBoxes[TargetBox].NumElements = FBoxes[SelectedBox].NumElements - ElementSum; FBoxes[SelectedBox].Hi[ax0] = ind[ax0]; FBoxes[SelectedBox].NumElements = ElementSum; Shrink(SelectedBox); Shrink(TargetBox); if (TargetBox==FNumBoxes) FNumBoxes++; } } } //--------------------------------------------------------------------------- void __fastcall TAutoGrouping::SortColors() { int Index, c, r, b, g, i, temp, rsum, bsum, gsum; int indices[256], weightedcolor[256]; bool flag; for (i=0; i<=255; i++) { weightedcolor[i] = 0; indices[i] = 0; } for (Index=0; Indexweightedcolor[indices[Index+1]]) { temp = indices[Index]; indices[Index] = indices[Index+1]; indices[Index+1] = temp; flag = true; } Index++; } } for (Index=0; IndexStartScanLine()) { for (RowIndex=0; RowIndexHeight) { scan = Bitmap->GetScanLine(Area.top+RowIndex); for (ColIndex=0; ColIndexWidth) { Red = GetRed(LongIndex); Blue = GetBlue(LongIndex); Green = GetGreen(LongIndex); if ((RedPutScanLine(Area.top+RowIndex); } } Bitmap->StopScanLine(); } else {EXCEPTION_MESSAGE_OK(EC_MEMORY_LACK); return;} } //--------------------------------------------------------------------------- // TGroupingForm //--------------------------------------------------------------------------- Byte Filter[3][4][9] = { { { 0, 0, 0, 1, 1, 1, 0, 0, 0 }, { 0, 0, 1, 0, 1, 0, 1, 0, 0 }, { 0, 1, 0, 0, 1, 0, 0, 1, 0 }, { 1, 0, 0, 0, 1, 0, 0, 0, 1 } }, { { 0, 1, 0, 1, 1, 1, 0, 1, 0 }, { 1, 0, 1, 0, 1, 0, 1, 0, 1 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 } }, { { 1, 1, 1, 1, 1, 1, 1, 1, 1 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 } } }; typedef struct { Byte no; Byte spread; int count; } sColorArrange; //--------------------------------------------------------------------------- __fastcall TGroupingForm::TGroupingForm(TComponent* Owner) : TForm(Owner) { hWnd = ((TWinControl *)Owner)->Handle; //=========================================================================== StringTable.Create(DirectoryBin, Language, "Grouping"); SetSmallFont(Font); SetSmallFont(atLabel1->Font); SetSmallFont(atLabel2->Font); SpeedButtonType->Caption = IDS_TYPE; SpeedButtonFilter->Caption = IDS_FILLTER; SpeedButtonUser->Caption = IDS_USERCHOOSE; SpeedButtonRearrange->Caption = IDS_REARRANGE; atLabel1->Caption = IDS_COLORNUMBER; ButtonTypeRun->Caption = IDS_COMMON_RUN; ButtonUserRun->Caption = IDS_COMMON_RUN; SpeedButtonUserNew->Hint = IDS_COMMON_NEW; SpeedButtonUserAll->Hint = IDS_COMMON_ALL; SpeedButtonUserOne->Hint = IDS_COMMON_BLOCKCOLOR; SpeedButtonUserDelete->Hint = IDS_COMMON_INSERTCOLOR; SpeedButtonUserBeforeColor->Hint = IDS_COMMON_BEFORECOLOR; rgFilterSize->Caption = AnsiString(IDS_SIZE) + ":"; Label4->Caption = AnsiString(IDS_SHAPE) + ":"; SpeedButtonFilterNew->Hint = IDS_COMMON_NEW; SpeedButtonFilterAll->Hint = IDS_COMMON_ALL; SpeedButtonFilterOne->Hint = IDS_COMMON_BLOCKCOLOR; SpeedButtonFilterDelete->Hint = IDS_COMMON_INSERTCOLOR; SpeedButtonFilterBefore->Hint = IDS_COMMON_BEFORECOLOR; ButtonFilterRun->Caption = IDS_COMMON_RUN; //=========================================================================== } //--------------------------------------------------------------------------- void __fastcall TGroupingForm::FormCreate(TObject *Sender) { // } //--------------------------------------------------------------------------- void __fastcall TGroupingForm::SpeedButtonTypeClick(TObject *Sender) { InitItem(0); } //--------------------------------------------------------------------------- void __fastcall TGroupingForm::SpeedButtonUserClick(TObject *Sender) { InitItem(1); } //--------------------------------------------------------------------------- void __fastcall TGroupingForm::SpeedButtonFilterClick(TObject *Sender) { InitItem(2); } //--------------------------------------------------------------------------- void __fastcall TGroupingForm::SpeedButtonRearrangeClick(TObject *Sender) { TCursor cursor; cursor = Screen->Cursor; Screen->Cursor = crHourGlass; MainImageForm->UndoSave(UK_ALL, Rect(0, 0, MainImageForm->iMainImage->Bitmap->Width, MainImageForm->iMainImage->Bitmap->Height)); if (!RunRearrange()) Undo->RemoveLast(); #if defined(KNIT) MainImageForm->Arrange->Dyarn.cnt = 0; #endif Screen->Cursor = cursor; ::RepaintColor(); PostMessage(hWnd, TPM_EXITFUNCTION, 0, 0); } //--------------------------------------------------------------------------- void __fastcall TGroupingForm::ButtonTypeRunClick(TObject *Sender) { TCursor cursor; cursor = Screen->Cursor; Screen->Cursor = crHourGlass; RunType(); #if defined(KNIT) MainImageForm->Arrange->Dyarn.cnt = 0; #endif Screen->Cursor = cursor; ::RepaintColor(); PostMessage(hWnd, TPM_EXITFUNCTION, 0, 0); } //--------------------------------------------------------------------------- void __fastcall TGroupingForm::SpeedButtonUserNewClick(TObject *Sender) { ColorSelectorUser->Initial(); } //--------------------------------------------------------------------------- void __fastcall TGroupingForm::SpeedButtonUserAllClick(TObject *Sender) { TCursor cursor; cursor = Screen->Cursor; Screen->Cursor = crHourGlass; if (MainImageForm->WorkArea->Mask) MainImageForm->SearchWorkAreaColor(ColorSelectorUser->ChoiceColor); else MainImageForm->SearchWholeColor(ColorSelectorUser->ChoiceColor); ColorSelectorUser->Invalid(); Screen->Cursor = cursor; } //--------------------------------------------------------------------------- void __fastcall TGroupingForm::SpeedButtonUserOneClick(TObject *Sender) { if (SpeedButtonUserOne->Down) SpeedButtonUserOne->Hint = IDS_COMMON_ONECOLOR; else SpeedButtonUserOne->Hint = IDS_COMMON_BLOCKCOLOR; InitLocate(); } //--------------------------------------------------------------------------- void __fastcall TGroupingForm::SpeedButtonUserDeleteClick(TObject *Sender) { if (SpeedButtonUserDelete->Down) SpeedButtonUserDelete->Hint = IDS_COMMON_DELETECOLOR; else SpeedButtonUserDelete->Hint = IDS_COMMON_INSERTCOLOR; } //--------------------------------------------------------------------------- void __fastcall TGroupingForm::SpeedButtonUserBeforeColorClick(TObject *Sender) { ColorSelectorUser->LoadFromFile(DirectoryItem+"\\Grouping.Ini", "ColorSelector(UserChoose)"); } //--------------------------------------------------------------------------- void __fastcall TGroupingForm::ButtonUserRunClick(TObject *Sender) { if (Item == 1) { TCursor cursor; cursor = Screen->Cursor; Screen->Cursor = crHourGlass; RunUser(); #if defined(KNIT) MainImageForm->Arrange->Dyarn.cnt = 0; #endif Screen->Cursor = cursor; ::RepaintColor(); PostMessage(hWnd, TPM_EXITFUNCTION, 0, 0); } } //--------------------------------------------------------------------------- /* void __fastcall TGroupingForm::ComboBoxFilterSizeChange(TObject *Sender) { FilterSize = ComboBoxFilterSize->ItemIndex; switch (FilterSize) { case 0: UpDown->Max = 3; break; case 1: UpDown->Max = 1; break; case 2: UpDown->Max = 0; break; } View(); } */ //--------------------------------------------------------------------------- void __fastcall TGroupingForm::rgFilterSizeClick(TObject *Sender) { FilterSize = rgFilterSize->ItemIndex; switch (FilterSize) { case 0: UpDown->Max = 3; break; case 1: UpDown->Max = 1; break; case 2: UpDown->Max = 0; break; } View(); } //--------------------------------------------------------------------------- void __fastcall TGroupingForm::UpDownClick(TObject *Sender, TUDBtnType Button) { View(); } //--------------------------------------------------------------------------- void __fastcall TGroupingForm::SpeedButtonFilterNewClick(TObject *Sender) { ColorSelectorFilter->Initial(); } //--------------------------------------------------------------------------- void __fastcall TGroupingForm::SpeedButtonFilterAllClick(TObject *Sender) { TCursor cursor; cursor = Screen->Cursor; Screen->Cursor = crHourGlass; if (MainImageForm->WorkArea->Mask) MainImageForm->SearchWorkAreaColor(ColorSelectorFilter->ChoiceColor); else MainImageForm->SearchWholeColor(ColorSelectorFilter->ChoiceColor); ColorSelectorFilter->Invalid(); Screen->Cursor = cursor; } //--------------------------------------------------------------------------- void __fastcall TGroupingForm::SpeedButtonFilterOneClick(TObject *Sender) { if (SpeedButtonFilterOne->Down) SpeedButtonFilterOne->Hint = IDS_COMMON_ONECOLOR; else SpeedButtonFilterOne->Hint = IDS_COMMON_BLOCKCOLOR; } //--------------------------------------------------------------------------- void __fastcall TGroupingForm::SpeedButtonFilterDeleteClick(TObject *Sender) { if (SpeedButtonFilterDelete->Down) SpeedButtonFilterDelete->Hint = IDS_COMMON_DELETECOLOR; else SpeedButtonFilterDelete->Hint = IDS_COMMON_INSERTCOLOR; } //--------------------------------------------------------------------------- void __fastcall TGroupingForm::SpeedButtonFilterBeforeClick(TObject *Sender) { ColorSelectorFilter->LoadFromFile(DirectoryItem+"\\Grouping.Ini", "ColorSelector(Filter)"); } //--------------------------------------------------------------------------- void __fastcall TGroupingForm::ButtonFilterRunClick(TObject *Sender) { if (Item == 2) { TCursor cursor = Screen->Cursor; Screen->Cursor = crHourGlass; if (MainImageForm->WorkArea->Mask) RunFilter_WA_IRRE(); else RunFilter_WA_NONE(); Screen->Cursor = cursor; } } //--------------------------------------------------------------------------- // Private Method //--------------------------------------------------------------------------- void __fastcall TGroupingForm::InitItem(int i) { Item = i; switch (Item) { case 0: ClientHeight = PanelType->Top+PanelType->Height; Parent->Height = ParentHeight+ClientHeight; PanelType->BringToFront(); break; case 1: ClientHeight = PanelUser->Top+PanelUser->Height; Parent->Height = ParentHeight+ClientHeight; PanelUser->BringToFront(); break; case 2: ClientHeight = PanelFilter->Top+PanelFilter->Height; Parent->Height = ParentHeight+ClientHeight; PanelFilter->BringToFront(); rgFilterSize->ItemIndex = 0; break; } Step = 0; MainImageForm->iMainImage->Repaint(); View(); } //--------------------------------------------------------------------------- void __fastcall TGroupingForm::View() { int UseColor; bool cn[256]; int i, x, y; Byte *scan; RECT r; if (Item==0) { TypeEdit->SetFocus(); if (MainImageForm->WorkArea->Mask) { r = MainImageForm->WorkArea->Range; for (i=0; i<=255; i++) cn[i] = false; if(MainImageForm->iMainImage->Bitmap->StartScanLine()) { for (y = r.top; y < r.bottom; y++) { scan = MainImageForm->iMainImage->Bitmap->GetScanLine(y); for (x = r.left; x < r.right; x++) cn[*(scan+x)] = true; } MainImageForm->iMainImage->Bitmap->StopScanLine(); } else {EXCEPTION_MESSAGE_OK(EC_MEMORY_LACK); return;} UseColor = 0; for (i=2; i<252; i++) { if (cn[i]) UseColor++; } MaxValue = UseColor; } else { MaxValue = MainImageForm->Palette->UseColor-1; } if (MaxValue>0 && MaxValue<=250) TypeEdit->Value = MaxValue; TypeEdit->SelectAll(); atLabel2->Caption = "( 1 -> "+ AnsiString(MaxValue)+" )"; } else if (Item==1) { ColorSelectorUser->SetPalette(MainImageForm->Palette); } else if (Item==2) { ButtonFilterRun->SetFocus(); //lhskys ¿£ÅÍ·Î ½ÇÇàÇϱâÀ§ÇØ focus¸¦ ÁØ´Ù. rgFilterSize->ItemIndex = FilterSize; Image->Canvas->Brush->Color = clWhite; Image->Canvas->Brush->Style = bsSolid; Image->Canvas->Rectangle(0, 0, 26, 26); Image->Canvas->Brush->Color = clBlack; for (i=0; i<=8; i++) { if (Filter[FilterSize][UpDown->Position][i]==1) { x = (i % 3)*9; y = (i / 3)*9; if (i==4) Image->Canvas->Brush->Style = bsSolid; else Image->Canvas->Brush->Style = bsClear; Image->Canvas->Rectangle(x, y, x+7, y+7); } } ColorSelectorFilter->SetPalette(MainImageForm->Palette); } } //--------------------------------------------------------------------------- void __fastcall TGroupingForm::RunType() { int i, x, y, yy, c; TAutoGrouping *at = NULL; Byte *scan; bool undoSw = false; if ((at = new TAutoGrouping(MainImageForm->iMainImage->Bitmap)) == NULL) goto fail; at->ColorNumber = TypeEdit->Value; if (MainImageForm->WorkArea->Mask) { at->Area = MainImageForm->WorkArea->Range; } else { at->Area = Rect(0, 0, MainImageForm->iMainImage->Bitmap->Width, MainImageForm->iMainImage->Bitmap->Height); } if ((undoSw = MainImageForm->UndoSave(UK_ALL, Rect(0, 0, MainImageForm->iMainImage->Bitmap->Width, MainImageForm->iMainImage->Bitmap->Height))) == false) goto fail; if (!MainImageForm->iMainImage->Bitmap->StartScanLine()) goto fail; for (y=0; yHeight; y++) { scan = MainImageForm->iMainImage->Bitmap->GetScanLine(at->Area.top+y); yy = y*at->Width; for (x=0; xWidth; x++) { c = scan[at->Area.left+x]; at->SetRed(yy+x, MainImageForm->Palette->ColorData[c]->RGB.rgbRed >> 2); at->SetGreen(yy+x, MainImageForm->Palette->ColorData[c]->RGB.rgbGreen >> 2); at->SetBlue(yy+x, MainImageForm->Palette->ColorData[c]->RGB.rgbBlue >> 2); } } MainImageForm->iMainImage->Bitmap->StopScanLine(); MainImageForm->iMainImage->Bitmap->FillRect(Rect(0, 0, MainImageForm->iMainImage->Bitmap->Width, MainImageForm->iMainImage->Bitmap->Height), PALETTEINDEX(BackgroundIndex)); at->Processing(); for (i=1; i<=at->ColorNumber+1; i++) { MainImageForm->Palette->ColorData[i+1]->RGB.rgbRed = (at->Palette[i].rgbRed << 2) | (at->Palette[i].rgbRed >> 6); MainImageForm->Palette->ColorData[i+1]->RGB.rgbGreen = (at->Palette[i].rgbGreen << 2) | (at->Palette[i].rgbGreen >> 6); MainImageForm->Palette->ColorData[i+1]->RGB.rgbBlue = (at->Palette[i].rgbBlue << 2) | (at->Palette[i].rgbBlue >> 6); } delete at; at = NULL; /* Use the LEADTOOLS BITMAPHANDLE hBitmap; RGBQUAD rgb[256]; int nRet; HDC dcSrc, dcDst; RECT src, dst; Byte *sl; if (MainImageForm->WorkArea->Mask) { MainImageForm->Undo->Save(UK_ALL, MainImageForm->WorkArea->Range); src = MainImageForm->WorkArea->Range; dst.left = 0; dst.top = 0; dst.right = MainImageForm->WorkArea->Range.right-MainImageForm->WorkArea->Range.left; dst.bottom = MainImageForm->WorkArea->Range.bottom-MainImageForm->WorkArea->Range.top; } else { MainImageForm->Undo->Save(UK_ALL, Rect(0, 0, MainImageForm->iMainImage->Bitmap->Width, MainImageForm->iMainImage->Bitmap->Height)); src.left = 0; src.top = 0; src.right = MainImageForm->iMainImage->Bitmap->Width; src.bottom = MainImageForm->iMainImage->Bitmap->Height; dst = src; } nRet = L_CreateBitmap(&hBitmap, TYPE_CONV, dst.right, dst.bottom, 8, ORDER_BGR, NULL, TOP_LEFT); MainImageForm->Palette->ToRGBQUAD(rgb, 256); nRet = L_PutBitmapColors(&hBitmap, 0, 256, rgb); dcSrc = MainImageForm->iMainImage->Bitmap->CreateDC(); dcDst = L_CreateLeadDC(&hBitmap); BitBlt(dcDst, dst.left, dst.top, dst.right, dst.bottom, dcSrc, src.left, src.top, SRCCOPY); L_DeleteLeadDC(dcDst); MainImageForm->iMainImage->Bitmap->DeleteDC(dcSrc); nRet = L_ColorResBitmap(&hBitmap, &hBitmap, 8, CRF_OPTIMIZEDPALETTE | CRF_ORDEREDDITHERING, NULL, SpinEdit->Value, NULL, NULL); nRet = L_GetBitmapColors(&hBitmap, 0, 256, rgb); MainImageForm->iMainImage->Bitmap->PutColors(0, 256, rgb); dcSrc = L_CreateLeadDC(&hBitmap); dcDst = MainImageForm->iMainImage->Bitmap->CreateDC(); BitBlt(dcDst, src.left, src.top, dst.right, dst.bottom, dcSrc, dst.left, dst.top, SRCCOPY); MainImageForm->iMainImage->Bitmap->DeleteDC(dcDst); L_DeleteLeadDC(dcSrc); for (x=0; xValue; x++) { MainImageForm->Palette->SetNormal(x+2, rgb[x].rgbRed, rgb[x].rgbGreen, rgb[x].rgbBlue); } MainImageForm->Palette->ToRGBQUAD(rgb, 256); MainImageForm->iMainImage->Bitmap->PutColors(0, 256, rgb); MainImageForm->iMainImage->Bitmap->StartScanLine(); for (y=src.top; yiMainImage->Bitmap->GetScanLine(y); for (x=src.left; xiMainImage->Bitmap->PutScanLine(y); } MainImageForm->iMainImage->Bitmap->StopScanLine(); L_FreeBitmap(&hBitmap); */ MainImageForm->iMainImage->Repaint(); MainImageForm->Palette->UseColor = TypeEdit->Value+1; PaletteForm->DIB256Palette->ChoiceIndex = 2; return; fail: if (at) { MainImageForm->iMainImage->Bitmap->StopScanLine(); Undo->RemoveLast(); delete at; } if (undoSw == true) Undo->RemoveLast(); EXCEPTION_MESSAGE_OK(EC_MEMORY_LACK); } //--------------------------------------------------------------------------- void __fastcall TGroupingForm::RunUser() { TTexpiaBitmap *tb; TPWorkArea *wa; Byte pal[256]; int cnt[256]; int i, j, x, y, t; TColorData *cm; Byte *scan, *mp; bool undoSw = false; RGBQUAD rgb[256]; tb = MainImageForm->iMainImage->Bitmap; if ((undoSw = MainImageForm->UndoSave(UK_ALL, Rect(0, 0, tb->Width, tb->Height))) == false) goto fail; if (ColorSelectorUser->Count > 0) { for (i=1; i<=MainImageForm->Palette->UseColor; i++) { for (j=0; jCount; j++) { cm = ColorSelectorUser->GetColorMap(j); cnt[j] = abs(MainImageForm->Palette->ColorData[i]->RGB.rgbRed-cm->RGB.rgbRed)+ abs(MainImageForm->Palette->ColorData[i]->RGB.rgbGreen-cm->RGB.rgbGreen)+ abs(MainImageForm->Palette->ColorData[i]->RGB.rgbBlue-cm->RGB.rgbBlue); } t = 0; for (j=1; jCount; j++) { if (cnt[j]WorkArea; if (!tb->StartScanLine()) goto fail; if (wa->Mask) { if(!wa->Mask->StartScanLine()) goto fail; for (y=wa->Range.top; yRange.bottom; y++) { scan = tb->GetScanLine(y)+wa->Range.left; mp = (Byte *)wa->Mask->GetScanLine(y-wa->Range.top); for (x=wa->Range.left; xRange.right; x++, scan++, mp++) { if (*mp) *scan = ColorSelectorUser->Code[pal[*scan]]; } tb->PutScanLine(y); } wa->Mask->StopScanLine(); } else { for (y=0; yHeight; y++) { scan = tb->GetScanLine(y); for (x=0; xWidth; x++) { *(scan+x) = ColorSelectorUser->Code[pal[*(scan+x)]]; } tb->PutScanLine(y); } } tb->StopScanLine(); } else { wa = MainImageForm->WorkArea; if (!tb->StartScanLine()) goto fail; if (wa->Mask) { if(!wa->Mask->StartScanLine()) goto fail; for (y=wa->Range.top; yRange.bottom; y++) { scan = tb->GetScanLine(y)+wa->Range.left; mp = (Byte *)wa->Mask->GetScanLine(y-wa->Range.top); for (x=wa->Range.left; xRange.right; x++, scan++, mp++) { if (*mp) *scan = 1; } tb->PutScanLine(y); } wa->Mask->StopScanLine(); } else { for (y=0; yHeight; y++) { scan = tb->GetScanLine(y); for (x=0; xWidth; x++) { *(scan+x) = 1; } tb->PutScanLine(y); } } tb->StopScanLine(); } MainImageForm->Palette->ToRGBQUAD(rgb, 256); tb->PutColors(0, 256, rgb); MainImageForm->iMainImage->Repaint(); if (!RunRearrange()) Undo->RemoveLast(); return; fail: if (wa->Mask) wa->Mask->StopScanLine(); tb->StopScanLine(); if (undoSw == true) Undo->RemoveLast(); EXCEPTION_MESSAGE_OK(EC_MEMORY_LACK); } //--------------------------------------------------------------------------- void __fastcall TGroupingForm::RunFilter_WA_NONE() { int ipx[9] = {-1, 0, 1, -1, 0, 1, -1, 0, 1}; int ipy[9] = {0, 0, 0, 1, 1, 1, 2, 2, 2}; Byte max, maxp, c; Byte cm[256]; int Tcol[256]; int x, y, w, h, pss; int i, j; TTexpiaBitmap *Ubmp, *Sbmp; Byte *Usl[3]; Byte *Ssl; bool undoSw = false; int Direction; Sbmp = MainImageForm->iMainImage->Bitmap; if ((undoSw = MainImageForm->UndoSave(UK_ALL, Rect(0, 0, Sbmp->Width, Sbmp->Height))) == false) goto fail; Ubmp = Undo->Last->Bitmap; w = Sbmp->Width-2; h = Sbmp->Height-2; StatusProgress->Maximum = h + 1; for (i=0; i<=255; i++) { Tcol[i] = -1; for (j=0; j<=255; j++) { if (i==ColorSelectorFilter->Code[j]) { Tcol[i] = j; break; } } } StatusProgress->Position = 1; Direction = UpDown->Position; if (!Ubmp->StartScanLineN(3)) goto fail; if (!Sbmp->StartScanLine()) goto fail; Usl[0] = Ubmp->GetScanLineN(0, 0); Usl[1] = Ubmp->GetScanLineN(1, 1); for (y=1; y<=h; y++) { Usl[2] = Ubmp->GetScanLineN(y+1, (y+1)%3); Ssl = Sbmp->GetScanLine(y); for (x=1; x<=w; x++) { c = Usl[1][x]; if (Tcol[c]==-1) { memset(cm, 0, 256); max = 0; for (i=0; i<=8; i++) { if (Filter[FilterSize][Direction][i]==1) { c = Usl[ipy[i]][x+ipx[i]]; cm[c] = cm[c]+1; if (cm[c]>max) { max = cm[c]; maxp = c; } } } if (Tcol[maxp]==-1) { if (max>1) Ssl[x] = maxp; } } } Sbmp->PutScanLine(y); Usl[0] = Usl[1]; Usl[1] = Usl[2]; StatusProgress->Position = y + 1; } Sbmp->StopScanLine(); Ubmp->StopScanLine(); MainImageForm->iMainImage->Repaint(); StatusProgress->End(); return; fail: Sbmp->StopScanLine(); Ubmp->StopScanLine(); if (undoSw == true) Undo->RemoveLast(); EXCEPTION_MESSAGE_OK(EC_MEMORY_LACK); } //--------------------------------------------------------------------------- void __fastcall TGroupingForm::RunFilter_WA_IRRE() { int ipx[9] = {-1, 0, 1, -1, 0, 1, -1, 0, 1}; int ipy[9] = {0, 0, 0, 1, 1, 1, 2, 2, 2}; Byte max, maxp, c; Byte cm[256]; int Tcol[256]; int x, y, sx, sy, w, h; int i, j; TTexpiaBitmap *Ubmp, *Sbmp, *Mbmp; Byte *Usl[3]; Byte *mp, *Ssl, mm; bool undoSw = false; int Direction; #if defined(TEXTILE) if ((undoSw = MainImageForm->UndoSave(UK_ALL, MainImageForm->WorkArea->Range)) == false) goto fail; #elif defined(KNIT) if ((undoSw = MainImageForm->UndoSave(UK_ALL, MainImageForm->WorkArea->Range, true)) == false) goto fail; #endif Ubmp = Undo->Last->Bitmap; Sbmp = MainImageForm->iMainImage->Bitmap; Mbmp = MainImageForm->WorkArea->Mask; sx = MainImageForm->WorkArea->Range.left; sy = MainImageForm->WorkArea->Range.top; w = MainImageForm->WorkArea->Range.right-MainImageForm->WorkArea->Range.left-2; h = MainImageForm->WorkArea->Range.bottom-MainImageForm->WorkArea->Range.top-2; StatusProgress->Maximum = h + 1; for (i=0; i<=255; i++) { Tcol[i] = -1; for (j=0; j<=255; j++) { if (i==ColorSelectorFilter->Code[j]) { Tcol[i] = j; break; } } } StatusProgress->Position = 1; Direction = UpDown->Position; if (!Ubmp->StartScanLineN(3)) goto fail; if (!Sbmp->StartScanLine()) goto fail; Usl[0] = Ubmp->GetScanLineN(0, 0); Usl[1] = Ubmp->GetScanLineN(1, 1); if (!Mbmp->StartScanLine()) goto fail; for (y=1; y<=h; y++) { Usl[2] = Ubmp->GetScanLineN(y+1, (y+1)%3); Ssl = Sbmp->GetScanLine(sy+y); mp = (Byte *)Mbmp->GetScanLine(y); mm = 0x80; for (x=1; x<=w; x++) { if (*mp & mm) { c = Usl[1][x]; if (Tcol[c]==-1) { memset(cm, 0, 256); max = 0; for (i=0; i<=8; i++) { if (Filter[FilterSize][Direction][i]==1) { c = Usl[ipy[i]][x+ipx[i]]; cm[c] = cm[c]+1; if (cm[c]>max) { max = cm[c]; maxp = c; } } } if (Tcol[maxp]==-1) { if (max>1) Ssl[sx+x] = maxp; } } } if (mm == 1) { mp++; mm = 0x80; } else mm >>= 1; } Sbmp->PutScanLine(sy+y); Usl[0] = Usl[1]; Usl[1] = Usl[2]; StatusProgress->Position = y + 1; } Mbmp->StopScanLine(); Sbmp->StopScanLine(); Ubmp->StopScanLine(); MainImageForm->iMainImage->Repaint(); StatusProgress->End(); return; fail: Mbmp->StopScanLine(); Sbmp->StopScanLine(); Ubmp->StopScanLine(); if (undoSw == true) Undo->RemoveLast(); EXCEPTION_MESSAGE_OK(EC_MEMORY_LACK); } //--------------------------------------------------------------------------- int __fastcall find(int current, TList *List) { int k, p, count; sColorArrange *pOrder; k = 0; while (kCount) { pOrder = (sColorArrange *)List->Items[k]; count = pOrder->count; p = k; if (pOrder->spread) { while (true) { k = k+1; pOrder = (sColorArrange *)List->Items[k]; count = count+pOrder->count; if (k+1Count) { pOrder = (sColorArrange *)List->Items[k+1]; if (pOrder->spread==0 || pOrder->spread==1) break; } else break; } } k = k+1; if (current>count) return p; } return List->Count; } //--------------------------------------------------------------------------- bool __fastcall RunRearrange() { int Code[256]; int ColorCount[256]; int i, j, n, x, y, count; Byte *Scan; TList *List = NULL; sColorArrange *pOrder = NULL; TChoiceColor *ChoiceColor = NULL; RGBQUAD rgb[256]; if (MainImageForm->Palette->UseColor>2) { if ((ChoiceColor = new TChoiceColor) == NULL) goto fail; if ((List = new TList) == NULL) goto fail; for (i=0; i<=255; i++) { Code[i] = -1; ColorCount[i] = 0; } if (!MainImageForm->iMainImage->Bitmap->StartScanLine()) goto fail; for (y=0; yiMainImage->Bitmap->Height; y++) { Scan = MainImageForm->iMainImage->Bitmap->GetScanLine(y); for (x=0; xiMainImage->Bitmap->Width; x++) { n = Scan[x]; ColorCount[n]++; } MainImageForm->iMainImage->Bitmap->PutScanLine(y); } MainImageForm->iMainImage->Bitmap->StopScanLine(); i = 2; while (i<252) { if (ColorCount[i]) { j = find(ColorCount[i], List); if ((pOrder = (sColorArrange *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(sColorArrange))) == NULL) goto fail; pOrder->no = i; pOrder->spread = 0; pOrder->count = ColorCount[i]; if (jCount) List->Insert(j, pOrder); else List->Add(pOrder); } i = i+1; } for (i=0; iCount; i++) { pOrder = (sColorArrange *)List->Items[i]; Code[pOrder->no] = ChoiceColor->Count+1; ChoiceColor->Add(MainImageForm->Palette, pOrder->no); } if (!MainImageForm->iMainImage->Bitmap->StartScanLine()) goto fail; for (y=0; yiMainImage->Bitmap->Height; y++) { Scan = MainImageForm->iMainImage->Bitmap->GetScanLine(y); for (x=0; xiMainImage->Bitmap->Width; x++) { n = Scan[x]; if (n>1) Scan[x] = Code[n]+1; } MainImageForm->iMainImage->Bitmap->PutScanLine(y); } MainImageForm->iMainImage->Bitmap->StopScanLine(); for (i=0; iCount; i++) { MainImageForm->Palette->ColorData[i+2] = ChoiceColor->ColorMap[i]; } MainImageForm->Palette->UseColor = List->Count+1; while (List->Count>0) { pOrder = (sColorArrange *)List->First(); HeapFree(GetProcessHeap(), 0, pOrder); List->Remove(pOrder); } delete List; List = NULL; delete ChoiceColor; ChoiceColor = NULL; } MainImageForm->Palette->ToRGBQUAD(rgb, 256); MainImageForm->iMainImage->Bitmap->PutColors(0, 256, rgb); PaletteForm->DIB256Palette->ChoiceIndex = 2; return true; fail: if (List) { while (List->Count>0) { pOrder = (sColorArrange *)List->First(); HeapFree(GetProcessHeap(), 0, pOrder); List->Remove(pOrder); } delete List; } MainImageForm->iMainImage->Bitmap->StopScanLine(); if (ChoiceColor) delete ChoiceColor; EXCEPTION_MESSAGE_OK(EC_MEMORY_LACK); return false; } //--------------------------------------------------------------------------- void __fastcall TGroupingForm::InitLocate() { if (Item==1 || Item==2) { if (Step>0) { if (IsDraw) { MainImageForm->DrawRectangleLocate(Temp); IsDraw = false; Step = 0; } } } } //--------------------------------------------------------------------------- // Public Method //--------------------------------------------------------------------------- void __fastcall TGroupingForm::InitForm() { ParentHeight = Parent->Height+20; FilterSize = 0; SpeedButtonType->Down = true; InitItem(0); MaxValue = 255; ColorSelectorFilter->Initial(); ColorSelectorUser->Initial(); View(); } //--------------------------------------------------------------------------- void __fastcall TGroupingForm::ExitForm() { if (Step > 0) MainImageForm->iMainImage->Repaint(); ColorSelectorFilter->SaveToFile(DirectoryItem+"\\Grouping.Ini", "ColorSelector(Filter)"); ColorSelectorUser->SaveToFile(DirectoryItem+"\\Grouping.Ini", "ColorSelector(UserChoose)"); } //--------------------------------------------------------------------------- void __fastcall TGroupingForm::iMainImageMouseDown(TMouseButton Button, TShiftState Shift, int X, int Y) { RECT r; bool sw, One, Delete; int ix, iy; TColorSelector *ColorSelector; TTexpiaBitmap *tb; Byte *bp; if (Button==mbMiddle || (Shift.Contains(ssShift) && Button==mbLeft)) { switch (Item) { case 1: One = SpeedButtonUserOne->Down; Delete = SpeedButtonUserDelete->Down; ColorSelector = ColorSelectorUser; sw = true; break; case 2: One = SpeedButtonFilterOne->Down; Delete = SpeedButtonFilterDelete->Down; ColorSelector = ColorSelectorFilter; sw = true; break; default: sw = false; break; } if (sw) { tb = MainImageForm->iMainImage->Bitmap; if (One) { if (Delete) { ColorSelector->DeleteColor(tb->GetPixelColor(X, Y)); } else { ColorSelector->AddColor(tb->GetPixelColor(X, Y)); } } 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 (tb->StartScanLine()) { if (Delete) { for (iy=r.top; iyGetScanLine(iy); for (ix=r.left; ixDeleteColor(*(bp+ix)); } } } else { for (iy=r.top; iyGetScanLine(iy); for (ix=r.left; ixAddColor(*(bp+ix)); } } } tb->StopScanLine(); } else { EXCEPTION_MESSAGE_OK(EC_MEMORY_LACK); return;} Step = 0; IsDraw = false; } else { Temp.left = Temp.top = X; Temp.top = Temp.bottom = Y; MainImageForm->DrawRectangleLocate(Temp); IsDraw = true; Step = 11; } } } } } //--------------------------------------------------------------------------- void __fastcall TGroupingForm::iMainImageMouseMove(TShiftState Shift, int X, int Y) { bool sw; bool One; switch (Item) { case 1: One = SpeedButtonUserOne->Down; sw = true; break; case 2: One = SpeedButtonFilterOne->Down; sw = true; break; default: sw = false; break; } if (sw) { if (One==false) { if (Step>0) { if (IsDraw) MainImageForm->DrawRectangleLocate(Temp); Temp.right = X; Temp.bottom = Y; MainImageForm->DrawRectangleLocate(Temp); IsDraw = true; } } } } //--------------------------------------------------------------------------- void __fastcall TGroupingForm::iMainImagePaint() { IsDraw = false; } //--------------------------------------------------------------------------- void __fastcall TGroupingForm::TypeEditChange(TObject *Sender) { if (TypeEdit->Text.Length()>0) { int k = TypeEdit->Value; if (k > MaxValue) TypeEdit->Value = MaxValue; if (k < 1) TypeEdit->Value = 1; } } //--------------------------------------------------------------------------- void __fastcall TGroupingForm::Rearrange() // ÆË¾÷¸Þ´º¸¦ À§ÇØ ÇÔ¼ö µû·Î °ü¸®. playzzang { TCursor cursor; MainMenuForm->showGrouping = false; cursor = Screen->Cursor; Screen->Cursor = crHourGlass; RunRearrange(); #if defined(KNIT) MainImageForm->Arrange->Dyarn.cnt = 0; #endif Screen->Cursor = cursor; ::RepaintColor(); PostMessage(hWnd, TPM_EXITFUNCTION, 0, 0); } //--------------------------------------------------------------------------- void __fastcall TGroupingForm::iMainImageKeyDown(int Key) { if ( Key == 13){ if(SpeedButtonType->Down)ButtonTypeRunClick(this); //lhskys ÀÚµ¿Á¤¸® if(SpeedButtonFilter->Down)ButtonFilterRunClick(this); //lhskys ÀâÆ¼Á¤¸® if(SpeedButtonUser->Down)ButtonUserRunClick(this); //lhskys »ö¼±ÅÂÁ¤¸® } } //---------------------------------------------------------------------------