//--------------------------------------------------------------------------- #include #pragma hdrstop #include "Plan.h" #include "StatusProgress.h" //--------------------------------------------------------------------------- #pragma package(smart_init) //--------------------------------------------------------------------------- // TPileData //--------------------------------------------------------------------------- __fastcall TPileData::TPileData() { for (int i=0; i value // |0|00|00|000|00000|000 ----- 0 ~ 6 random color value //--------------------------------------------------------------------------- __fastcall TPlan::TPlan() { Yarn = new TYarnArrange; Texture = new TTextureArrange; Dsim.machine = DNB; Dsim.edge = 0; Dsim.ec = 1; Dsim.ew = 1; Dsim.spread = 10; Dsim.end = 0; Dsim.cpi = 30.0; Dsim.wpi = 20.0; Dsim.move = 1.0; bmOutput = NULL; mode = false; view = false; ModeBitmap = NULL; bmMode = NULL; } //--------------------------------------------------------------------------- __fastcall TPlan::~TPlan() { if (bmMode) delete bmMode; if (ModeBitmap) delete ModeBitmap; if (bmOutput) delete bmOutput; if (Yarn) delete Yarn; if (Texture) delete Texture; } //--------------------------------------------------------------------------- // Private Methods //--------------------------------------------------------------------------- bool __fastcall TPlan::GetFinish() { if (Yarn && Texture) { return Yarn->bFinish && Texture->bFinish; } return false; } //--------------------------------------------------------------------------- bool __fastcall TPlan::GetPiling() { return bmOutput; } //--------------------------------------------------------------------------- void __fastcall TPlan::set_size(TPileData &pd, int xw, int yc) { int i, j; pd.rx = Yarn->Array[0].count; if (Yarn->ArrayCount>1) { for (i=1; iArrayCount; i++) pd.rx = gcd(pd.rx, (int)Yarn->Array[i].count); j = pd.rx; for (i=0; iArrayCount; i++) { j *= Yarn->Array[i].count / pd.rx; } pd.rx = j; if (pd.rx > xw) pd.rx = xw; } pd.ry = Texture->Array[0].Data[Texture->Array[0].number-1].DY; if (Texture->Count>1) { for (i = 1; i < Texture->Count; i++) { pd.ry = gcd(pd.ry, (int)Texture->Array[i].Data[Texture->Array[i].number-1].DY); } j = pd.ry; for (i = 0; i < Texture->Count; i++) { j *= Texture->Array[i].Data[Texture->Array[i].number-1].DY / pd.ry; } pd.ry = j; if (pd.ry > yc) pd.ry = yc; } } //--------------------------------------------------------------------------- WORD __fastcall TPlan::set_color(TPileData &pd, int p) { Byte c, *yp; short o, q, r, s; int t, ri, it, xx; double di, dt; TYarnData *yd; t = (pd.nc[p]&0x1F)>>3; yd = Yarn->Choice[t].Data; q = 0; xx = pd.Yp.size.x; if (yd->Infor.Detail.TYC == 1) { switch (yd->Infor.Detail.Type) { case 2: if (pd.cx < pd.Ycode.size.x && pd.cy < pd.Ycode.size.y) { r = *pd.Ycode.pointer(pd.cx, pd.cy) & 0xF; if (r==0) { s = pd.cx + pd.Ysx[p][(int) pd.cy % pd.ry]; if (s < 0) s += pd.rx; else if (s >= xx) s -= pd.rx; yp = pd.Yp.pointer(s, p); if (pd.cy < 1.0) { r = rand() % Yarn->Choice[t].Data->Colors; *pd.Yd.pointer(s, p) = r; *yp = rand()%8; } else { r = *pd.Yd.pointer(s, p); if (*yp>=5+rand()%5) { if (r < Yarn->Choice[t].Data->Colors - 1) r++; else r = 0; *pd.Yd.pointer(s, p) = r; *yp = 0; } else *yp += 1; } *pd.Ycode.pointer(pd.cx, pd.cy) |= 8+r; } q = r&7; } break; default: q = rand() % yd->Colors; break; } } else { switch (yd->Infor.Detail.Type) { case 2: if (pd.cx < pd.Ycode.size.x && pd.cy < pd.Ycode.size.y) { o = rand() % yd->Infor.Detail.TYC; r = (*pd.Ycode.pointer(pd.cx, pd.cy)>>(o<<2))&0xF; if (r==0) { s = pd.cx + pd.Ysx[p][(int)pd.cy % pd.ry]; if (s<0) s += pd.rx; else if (s>=xx) s -= pd.rx; yp = pd.Yp.pointer(s, p); if (pd.cy<1.0) { r = rand() % Yarn->Choice[t].Data->Colors; *pd.Yd.pointer(s, p) = r; *yp = rand()%8; } else { r = *pd.Yd.pointer(s, p); if (*yp >= 5+rand()%5) { if (r < Yarn->Choice[t].Data->Colors - 1) r++; else r = 0; *pd.Yd.pointer(s, p) = r; *yp = 0; } else *yp += 1; } *pd.Ycode.pointer(pd.cx, pd.cy) |= (8+r)<<(o<<2); } q = (o<<3)+(r&7); } break; default: q = rand() % yd->Colors; break; } } return ((pd.nc[p] & 0x1F) << 8) | (q << 3) | (rand() % 7); } //--------------------------------------------------------------------------- Byte __fastcall TPlan::choice_tricot(TPileData &pd, Byte i) { short mxx; Byte bd = *pd.Ymap[i].pointer(pd.mx, pd.my)&0xE0; if ((bd&0xC0)==0x40) { double bx = pd.cx-Dsim.move; if (bx<0) mxx = (int)(bx + pd.rx)%pd.rx; else mxx = (int) bx % pd.rx; return bd | (*pd.Ymap[i].pointer(mxx, pd.my) & 0x1F); } else if ((bd&0xC0)==0x80) { mxx = (int)(pd.cx+Dsim.move) % pd.rx; return *pd.Ymap[i].pointer(mxx, pd.my); } else if ((bd&0xC0)==0xC0) { return *pd.Ymap[i].pointer(pd.mx, pd.my); } return 0; } //--------------------------------------------------------------------------- WORD __fastcall TPlan::choice_color_tricot(TPileData &pd) { Byte cnt, yc; short i, j, k, thick[MAX_TEXTURE]; pd.mx = (int)pd.cx % pd.rx; pd.my = (int)pd.cy % pd.ry; if (Texture->Count>1) { for (i=0, j=0; iCount; i++) { pd.nc[i] = choice_tricot(pd, i); if (pd.nc[i]) { thick[i] = Yarn->Choice[(pd.nc[i]&0x1F)>>3].Data->Thick; j += thick[i]; } } if (j) { k = rand()%j; for (i = 0, j = 0; i < Texture->Count; i++) { if (pd.nc[i]) { j += thick[i]; if (j>k) { if (pd.cx < pd.max[i]) { if ((pd.nc[i] & 0x20)==0) return set_color(pd, i); } else { return set_color(pd, i); } } } } } } else { pd.nc[0] = choice_tricot(pd, 0); if (pd.cx < pd.max[0]) { if (pd.nc[0] && (pd.nc[0]&0x20)==0) return set_color(pd, 0); } else { if (pd.nc[0]) return set_color(pd, 0); } } return 0x8000; } //--------------------------------------------------------------------------- WORD __fastcall TPlan::choice_color_dnb(TPileData &pd) { Byte cnt, yc; short i, j, k, thick[MAX_TEXTURE]; pd.mx = (int)pd.cx % pd.rx; pd.my = (int)pd.cy % pd.ry; if (Texture->Count>1) { for (i = 0, j = 0; i < Texture->Count; i++) { pd.nc[i] = *pd.Ymap[i].pointer(pd.mx, pd.my); if (pd.nc[i]) { thick[i] = Yarn->Choice[(pd.nc[i]&0x1F)>>3].Data->Thick; j += thick[i]; } } if (j) { k = rand()%j; for (i=0, j=0; iCount; i++) { if (pd.nc[i]) { j += thick[i]; if (j>k) return set_color(pd, i); } } } } else { pd.nc[0] = *pd.Ymap[0].pointer(pd.mx, pd.my); if (pd.nc[0]) return set_color(pd, 0); } return 0x8000; } //--------------------------------------------------------------------------- WORD __fastcall TPlan::DataToColor(WORD sp) { TGradeColor **ydc; WORD c = sp & 0x1FFF; ydc = Yarn->Choice[c>>11].Color[(c>>8)&7]; return RGBToColor15(ydc[(c >> 3) & 0x1F]->GetColor((c & 7) - 3)->RGB); } //--------------------------------------------------------------------------- bool __fastcall TPlan::TricotSimulation(int dpi, WORD bgColor) { Byte *ip, *ipp, tc, td, tu; short x, y, lx, ly, dx, ux, xw, wid, hei, yyy; int i, j, k, l, n, d, p, yc; double w, c, hr, yn[4]; TPileData pd; TYarnData *yd; AnsiString as; WORD *DP, *SP; StatusProgress->Maximum = 400; w = (double) dpi / Dsim.wpi; xw = bmOutput->size.x / w; c = (double) dpi / Dsim.cpi; yc = bmOutput->size.y / c; if (!pd.Yp.create(xw, Yarn->ArrayCount)) goto fail; if (pd.Yp.lock() == NULL) goto fail; if (!pd.Yd.create(xw, Yarn->ArrayCount)) goto fail; if (pd.Yd.lock() == NULL) goto fail; if (!pd.Ycode.create(xw, yc)) goto fail; if (pd.Ycode.lock() == NULL) goto fail; set_size(pd, xw, yc); pd.max[MAX_TEXTURE] = 0; for (i = 0; i < Texture->Count; i++) { pd.max[i] = 0; for (j = 0; j < Texture->Array[i].number; j++) { if (Texture->Array[i].Data[j].DX-1 > pd.max[i]) pd.max[i] = Texture->Array[i].Data[j].DX - 1; } if (pd.max[i] > pd.max[MAX_TEXTURE]) pd.max[MAX_TEXTURE] = pd.max[i]; } if (pd.rx < pd.max[MAX_TEXTURE]) pd.rx = int(pd.max[MAX_TEXTURE]/pd.rx+1)*pd.rx; for (p = 0; p < Texture->Count; p++) { if (pd.Ymap[p].create(pd.rx, pd.ry) == false) goto fail; pd.Ymap[p].lock(); if ((pd.Ysx[p]=(short *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, pd.ry*sizeof(short)))==NULL) goto fail; l = Texture->Array[p].sx; x = y = 0; i = 0; while (y < pd.ry) { if (i == 0) { lx = Texture->Array[p].Data[i].DX-Texture->Array[p].sx; ly = Texture->Array[p].Data[i].DY; } else { lx = Texture->Array[p].Data[i].DX - Texture->Array[p].Data[i-1].DX; ly = Texture->Array[p].Data[i].DY - Texture->Array[p].Data[i-1].DY; } if (Texture->Array[p].Data[i].Pass==PT_EXIST) { if (i==0) { dx = Texture->Array[p].Data[Texture->Array[p].number-1].DX -Texture->Array[p].Data[Texture->Array[p].number-2].DX; } else if (i==1) { dx = Texture->Array[p].Data[i].DX - Texture->Array[p].sx; } else { dx = Texture->Array[p].Data[i].DX - Texture->Array[p].Data[i-2].DX; } if (i==Texture->Array[p].number-1) { ux = Texture->Array[p].sx - Texture->Array[p].Data[0].DX; } else { ux = Texture->Array[p].Data[i].DX - Texture->Array[p].Data[i+1].DX; } if (ly > 1) { td = tu = 0; if (abs(dx)>Texture->Array[p].cut) { if (dx<0) td = 0x80; else if (dx>0) td = 0x40; } if (abs(ux)>Texture->Array[p].cut) { if (ux<0) tu = 0x80; else if (ux>0) tu = 0x40; } for (j = 0; j < pd.rx; j++) { n = Yarn->Array[p].count-1-(j+l)%Yarn->Array[p].count; if (Yarn->Array[p].code[n]) { d = (x+j) % pd.rx; if (d < 0) d += pd.rx; if (td) { if (Dsim.end == false) { if (d < pd.max[p]-Texture->Array[p].Data[i].DX+1) td |= 0x20; else td &= 0xC0; } *pd.Ymap[p].pointer(d, y) = td | (Yarn->Array[p].code[n]-1); pd.Ysx[p][y] = Texture->Array[p].Data[i].DX-Texture->Array[p].sx; } if (tu && y+ly-1Array[p].Data[i].DX-1) tu |= 0x20; else tu &= 0xC0; } *pd.Ymap[p].pointer(d, y+ly-1) = tu | (Yarn->Array[p].code[n]-1); pd.Ysx[p][y+ly-1] = Texture->Array[p].Data[i].DX - Texture->Array[p].sx; } } } } else { if (abs(dx)>Texture->Array[p].cut || abs(ux)>Texture->Array[p].cut) { tc = 0; if (dx<0) { if (ux>0) tc = 0xC0; else tc = 0x80; } else if (dx>0) { if (ux<0) tc = 0xC0; else tc = 0x40; } if (tc) { for (j=0; jArray[p].count-1-(j+l)%Yarn->Array[p].count; if (Yarn->Array[p].code[n]) { d = (x+j) % pd.rx; if (d<0) d += pd.rx; if (Dsim.end == false) { if (dArray[p].Data[i].DX+1) tc |= 0x20; else tc &= 0xC0; } *pd.Ymap[p].pointer(d, y) = tc | (Yarn->Array[p].code[n]-1); pd.Ysx[p][y] = Texture->Array[p].Data[i].DX-Texture->Array[p].sx; } } } } } } x -= lx; y += ly; i++; if (i>=Texture->Array[p].number) i = 0; } StatusProgress->Position = 50 * p / Texture->Count; } if (Dsim.edge==0) { for (y = 0; y < bmOutput->size.y; y++) { DP = bmOutput->pointer(0, y); if (y < Dsim.ec * pd.ry * c) { for (x = 0; x < bmOutput->size.x; x++, DP++) { if (x < Dsim.ew * pd.rx * w) { pd.cx = x/w; pd.cy = y/c; } else { hr = rand()%Dsim.spread/10.0; pd.cx = x/w+hr; pd.cy = y/c+hr; } *DP = choice_color_tricot(pd); } } else { for (x=0; x < bmOutput->size.x; x++, DP++) { hr = rand()%Dsim.spread/10.0; pd.cx = x/w+hr; pd.cy = y/c+hr; *DP = choice_color_tricot(pd); } } StatusProgress->Position = 50 + 200 * y / bmOutput->size.y; } } else { for (y = 0; y < bmOutput->size.y; y++) { DP = bmOutput->pointer(0, y); for (x=0; x < bmOutput->size.x; x++, DP++) { hr = rand()%Dsim.spread/10.0; pd.cx = x/w+hr; pd.cy = y/c+hr; *DP = choice_color_tricot(pd); } StatusProgress->Position = 50 + 200 * y / bmOutput->size.y; } } // Mode Simulation ----------------------------------------------- if (mode) { if (Texture->Count>1) { if (pd.rx < DWIDTH) wid = DWIDTH; else wid = pd.rx; yyy = DHEIGHT / Texture->Count; if (pd.ry < yyy) hei = yyy; else hei = pd.ry; if (ModeBitmap == NULL) ModeBitmap = new TTexpiaBitmap; ModeBitmap->Create(wid, hei * Texture->Count, 16); if (bmMode == NULL) bmMode = new TPMemoryArea(wid, hei*Texture->Count); ModeBitmap->StartScanLine(); bmMode->lock(); for (p = 0; p < Texture->Count; p++) { for (y = 0; y < hei; y++) { DP = (WORD *) ModeBitmap->GetScanLine(hei * p + (hei - 1 - y)); SP = (WORD *) bmMode->pointer(0, hei * p + (hei - 1 - y)); for (x = 0; x < wid; x++) { hr = rand() % Dsim.spread / 10.0; pd.cx = x/w + hr; pd.cy = y/c + hr; pd.mx = (int)pd.cx % pd.rx; pd.my = (int)pd.cy % pd.ry; pd.nc[p] = choice_tricot(pd, p); if (pd.cx < pd.max[p]) { if (pd.nc[p] && (pd.nc[p] & 0x20) == 0) { SP[x] = set_color(pd, p); DP[x] = DataToColor(SP[x]); } else { SP[x] = 0x8000; DP[x] = bgColor; } } else { if (pd.nc[p]) { SP[x] = set_color(pd, p); DP[x] = DataToColor(SP[x]); } else { SP[x] = 0x8000; DP[x] = bgColor; } } } ModeBitmap->PutScanLine(hei * p + (hei - 1 - y)); } StatusProgress->Position = 250 + 100 * p / Texture->Count; } bmMode->unlock(); ModeBitmap->StopScanLine(); } } else { if (bmMode) delete bmMode; bmMode = NULL; if (ModeBitmap) delete ModeBitmap; ModeBitmap = NULL; } //------------------------------------------------------------------- return true; fail: return false; } //--------------------------------------------------------------------------- bool __fastcall TPlan::DNBSimulation(int dpi, WORD bgColor) { Byte *ip, *ipp, tc, td, tu; short x, y, lx, ly, dx, ux, xw, wid, hei; int i, j, k, l, n, d, p, yc; double w, c, hr, yn[4]; TPileData pd; WORD *DP, *SP, color; TGradeColor **ydc; TYarnData *yd; AnsiString as; StatusProgress->Maximum = 400; w = (double) dpi / Dsim.wpi; xw = bmOutput->size.x / w; c = (double) dpi / Dsim.cpi; yc = bmOutput->size.y / c; if (!pd.Yp.create(xw, Yarn->ArrayCount)) goto fail; if (pd.Yp.lock() == NULL) goto fail; if (!pd.Yd.create(xw, Yarn->ArrayCount)) goto fail; if (pd.Yd.lock() == NULL) goto fail; if (!pd.Ycode.create(xw, yc)) goto fail; if (pd.Ycode.lock() == NULL) goto fail; set_size(pd, xw, yc); // 1 repeat¿µ¿ª¾È¿¡ °¢ Á¶Á÷º°·Î °¡´Â ±æÀ» Ç¥½ÃÇÑ´Ù. for (p = 0; p < Texture->Count; p++) { if (pd.Ymap[p].create(pd.rx, pd.ry) == false) goto fail; pd.Ymap[p].lock(); if ((pd.Ysx[p]=(short *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, pd.ry*sizeof(short)))==NULL) goto fail; l = Texture->Array[p].sx; x = y = 0; i = 0; while (y < pd.ry) { if (i==0) { lx = Texture->Array[p].Data[i].DX - Texture->Array[p].sx; ly = Texture->Array[p].Data[i].DY; } else { lx = Texture->Array[p].Data[i].DX - Texture->Array[p].Data[i-1].DX; ly = Texture->Array[p].Data[i].DY - Texture->Array[p].Data[i-1].DY; } if (Texture->Array[p].Data[i].Pass == PT_EXIST) { for (j = 0; j < pd.rx; j++) { n = Yarn->Array[p].count - 1 - (j+l) % Yarn->Array[p].count; if (Yarn->Array[p].code[n]) { d = (x+j) % pd.rx; if (d < 0) d += pd.rx; for (k = 0; k < ly; k++) { if (y+k < pd.ry) { *pd.Ymap[p].pointer(d, y+k) = 0xC0 | (Yarn->Array[p].code[n]-1); pd.Ysx[p][y+k] = Texture->Array[p].Data[i].DX - Texture->Array[p].sx; } } } } } x -= lx; y += ly; i++; if (i>=Texture->Array[p].number) i = 0; } StatusProgress->Position = 50 * p / Texture->Count; } // if (Dsim.edge==0) { for (y = 0; y < bmOutput->size.y; y++) { DP = bmOutput->pointer(0, y); if (y < Dsim.ec * pd.ry * c) { for (x = 0; x < bmOutput->size.x; x++, DP++) { if (x < Dsim.ew * pd.rx * w) { pd.cx = x/w; pd.cy = y/c; } else { hr = rand()%Dsim.spread/10.0; pd.cx = x/w+hr; pd.cy = y/c+hr; } *DP = choice_color_dnb(pd); } } else { for (x=0; x < bmOutput->size.x; x++, DP++) { hr = rand()%Dsim.spread/10.0; pd.cx = x/w+hr; pd.cy = y/c+hr; *DP = choice_color_dnb(pd); } } StatusProgress->Position = 50 + 200 * y / bmOutput->size.y; } } else { for (y = 0; y < bmOutput->size.y; y++) { DP = bmOutput->pointer(0, y); for (x=0; x < bmOutput->size.x; x++, DP++) { hr = rand()%Dsim.spread/10.0; pd.cx = x/w+hr; pd.cy = y/c+hr; *DP = choice_color_dnb(pd); } } StatusProgress->Position = 50 + 200 * y / bmOutput->size.y; } // Mode Simulation ----------------------------------------------- if (mode) { if (bmOutput->size.x < DWIDTH) wid = bmOutput->size.x; else wid = DWIDTH; if (bmOutput->size.y < DHEIGHT) hei = bmOutput->size.y; else hei = DHEIGHT; if (ModeBitmap == NULL) ModeBitmap = new TTexpiaBitmap; ModeBitmap->Create(wid, hei, 16); ModeBitmap->StartScanLine(); for (y = 0; y < hei; y++) { DP = (WORD *) ModeBitmap->GetScanLine(hei - 1 - y); SP = bmOutput->pointer(0, y); for (x = 0; x < wid; x++) { if ((SP[x] & 0x8000) == 0) { DP[wid - 1 - x] = DataToColor(SP[x]); } else { DP[wid - 1 - x] = bgColor; } } ModeBitmap->PutScanLine(hei - 1 - y); StatusProgress->Position = 250 + 100 * y / hei; } ModeBitmap->StopScanLine(); } else { if (ModeBitmap) delete ModeBitmap; ModeBitmap = NULL; } //------------------------------------------------------------------- return true; fail: return false; } //--------------------------------------------------------------------------- // Public Methods //--------------------------------------------------------------------------- bool __fastcall TPlan::Simulation(int w, int h, int dpi, WORD bgcolor) { WORD *bo = NULL; if (bmOutput==NULL) { if ((bmOutput = new TPMemoryArea(w, h)) == NULL) goto fail; } if ((bo = bmOutput->lock()) == NULL) goto fail; if (Dsim.machine==TRICOT) { if (!TricotSimulation(dpi, bgcolor)) goto fail; } else { if (!DNBSimulation(dpi, bgcolor)) goto fail; } bmOutput->unlock(); return true; fail: if (bo) bmOutput->unlock(); return false; } //--------------------------------------------------------------------------- void __fastcall TPlan::DrawOutput(TTexpiaBitmap *Bitmap, WORD bgColor) { WORD *sp, *dp, c; TGradeColor **ydc; WORD *bo = NULL, okk; if (bmOutput) { bo = bmOutput->lock(); if (bo == NULL) goto fail; if (Bitmap->StartScanLine() == false) goto fail; for (int y = 0; y < Bitmap->Height; y++) { sp = bmOutput->pointer(0, y); dp = (WORD *)Bitmap->GetScanLine(Bitmap->Height-1-y); for (int x=0; xWidth; x++, sp++, dp++) { if ((*sp & 0x8000) == 0) { *dp = DataToColor(*sp); } else { *dp = bgColor; } } Bitmap->PutScanLine(Bitmap->Height-1-y); StatusProgress->Position = 350 + y * 50 / Bitmap->Height; } Bitmap->StopScanLine(); bmOutput->unlock(); StatusProgress->End(); } else Bitmap->FillRect(Rect(0, 0, Bitmap->Width, Bitmap->Height), bgColor); return; fail: Bitmap->StopScanLine(); if (bo) bmOutput->unlock(); EXCEPTION_MESSAGE_OK(EC_MEMORY_LACK); } //--------------------------------------------------------------------------- WORD __fastcall TPlan::GetYarn(int X, int Y, bool m) { WORD value; WORD *bo = NULL, *bm = NULL; if (m) { if (Dsim.machine == TRICOT) { if (bmMode) { bm = bmMode->lock(); if (bm == NULL) goto fail; value = *bmMode->pointer(X, Y); bmMode->unlock(); } else return 0; } else { if (bmOutput) { bo = bmOutput->lock(); if (bo == NULL) goto fail; value = *bmOutput->pointer(ModeBitmap->Width - 1 - X , bmOutput->size.y - 1 - Y); bmOutput->unlock(); } else return 0; } } else { if (bmOutput) { bo = bmOutput->lock(); if (bo == NULL) goto fail; value = *bmOutput->pointer(X, Y); bmOutput->unlock(); } else return 0; } return value; fail: if (bm) bmMode->unlock(); if (bo) bmOutput->unlock(); EXCEPTION_MESSAGE_OK(EC_MEMORY_LACK); return 0; } //--------------------------------------------------------------------------- void __fastcall TPlan::ChangeColor(TTexpiaBitmap *Bitmap, WORD *yc, int cnt) { WORD *sp, *dp, c; COLORREF rgb; TGradeColor **ydc; WORD *bo = NULL, *bm = NULL; int x, y, i; if (bmOutput) { bo = bmOutput->lock(); if (bo == NULL) goto fail; if (Bitmap->StartScanLine() == false) goto fail; for (y = 0; y < bmOutput->size.y; y++) { sp = bmOutput->pointer(0, bmOutput->size.y-1-y); dp = (WORD *)Bitmap->GetScanLine(y); for (x = 0; x < bmOutput->size.x; x++, sp++, dp++) { for (i = 0; i < cnt; i++) { if (*sp && (*sp & 0x1FF8) == yc[i] && ~(*sp & 0x8000)) { *dp = DataToColor(*sp); // c = *sp & 0x1FFF; // ydc = Yarn->Choice[c>>11].Color[(c>>8)&7]; // *dp = RGBToColor15(ydc[(c>>3) & 0x1F]->GetColor(0)->RGB); break; } } } Bitmap->PutScanLine(y); } Bitmap->StopScanLine(); bmOutput->unlock(); if (ModeBitmap && view) { if (ModeBitmap->StartScanLine() == false) goto fail; if (Dsim.machine==TRICOT) { bm = bmMode->lock(); if (bm == NULL) goto fail; for (y = 0; y < bmMode->size.y; y++) { sp = bmMode->pointer(0, y); dp = (WORD *)ModeBitmap->GetScanLine(y); for (x = 0; x < bmMode->size.x; x++, sp++, dp++) { for (i = 0; i < cnt; i++) { if (*sp && (*sp & 0x1FF8)==yc[i] && ~(*sp & 0x8000)) { *dp = DataToColor(*sp); // c = *sp & 0x1FFF; // ydc = Yarn->Choice[c>>11].Color[(c>>8)&7]; // *dp = RGBToColor15(ydc[(c>>3)&0x1F]->GetColor(0)->RGB); break; } } } ModeBitmap->PutScanLine(y); } bmMode->unlock(); } else { bo = bmOutput->lock(); if (bo == NULL) goto fail; for (y = 0; y < ModeBitmap->Height; y++) { dp = (WORD *) ModeBitmap->GetScanLine(ModeBitmap->Height - 1 - y); sp = bmOutput->pointer(0, y); for (x = 0; x < ModeBitmap->Width; x++) { dp[ModeBitmap->Width - 1 - x] = DataToColor(sp[x]); } ModeBitmap->PutScanLine(ModeBitmap->Height - 1 - y); } bmOutput->unlock(); } ModeBitmap->StopScanLine(); } } return; fail: Bitmap->StopScanLine(); ModeBitmap->StopScanLine(); if (bo) bmOutput->unlock(); if (bm) bmOutput->unlock(); EXCEPTION_MESSAGE_OK(EC_MEMORY_LACK); } //--------------------------------------------------------------------------- void __fastcall TPlan::ChangeBackGroundColor(TTexpiaBitmap *Bitmap, WORD bgc) { WORD *sp, *dp; int x, y; WORD *bo = NULL, *bm = NULL; if (bmOutput) { bo = bmOutput->lock(); if (bo == NULL) goto fail; if (Bitmap->StartScanLine() == false) goto fail; for (y = 0; y < bmOutput->size.y; y++) { sp = bmOutput->pointer(0, bmOutput->size.y-1-y); dp = (WORD *) Bitmap->GetScanLine(y); for (x = 0; x < bmOutput->size.x; x++, sp++, dp++) { if (*sp & 0x8000) *dp = bgc; } Bitmap->PutScanLine(y); } Bitmap->StopScanLine(); bmOutput->unlock(); if (mode && view) { if (ModeBitmap->StartScanLine() == false) goto fail; if (Dsim.machine == TRICOT) { bm = bmMode->lock(); if (bm == NULL) goto fail; for (y = 0; y < bmMode->size.y; y++) { sp = bmMode->pointer(0, y); dp = (WORD *) ModeBitmap->GetScanLine(y); for (x = 0; x < bmMode->size.x; x++, sp++, dp++) { if (*sp & 0x8000) *dp = bgc; } ModeBitmap->PutScanLine(y); } bmMode->unlock(); } else { bo = bmOutput->lock(); if (bo == NULL) goto fail; for (y = 0; y < ModeBitmap->Height; y++) { dp = (WORD *) ModeBitmap->GetScanLine(ModeBitmap->Height - 1 - y); sp = bmOutput->pointer(0, y); for (x = 0; x < ModeBitmap->Width; x++) { if (sp[x] & 0x8000) dp[ModeBitmap->Width - 1 - x] = bgc; } ModeBitmap->PutScanLine(ModeBitmap->Height - 1 - y); } bmOutput->unlock(); } ModeBitmap->StopScanLine(); } } else { if (Bitmap->StartScanLine() == false) goto fail; for (y = 0; y < Bitmap->Height; y++) { dp = (WORD *) Bitmap->GetScanLine(y); for (x = 0; x < Bitmap->Width; x++, dp++) { *dp = bgc; } Bitmap->PutScanLine(y); } Bitmap->StopScanLine(); } return; fail: Bitmap->StopScanLine(); ModeBitmap->StopScanLine(); if (bo) bmOutput->unlock(); if (bm) bmMode->unlock(); EXCEPTION_MESSAGE_OK(EC_MEMORY_LACK); } //--------------------------------------------------------------------------- TPException __fastcall TPlan::LoadFromFile(HANDLE fh, TEXPIAFILEHEADER &tpfh) { DWORD dwRead; WORD opt, *bo = NULL, *bm = NULL; short w, h; TPException ec = EC_NONE; if (tpfh.Version.Texpia=='P') { if (tpfh.Version.Method=='T' && tpfh.Version.Number>=200) { // Windows Version if (!ReadFile(fh, &Dsim, sizeof(TPileCondition), &dwRead, NULL)) goto fail; if ((ec = Yarn->LoadFromFile(fh, tpfh)) != EC_NONE) goto fail; if ((ec = Texture->LoadFromFile(fh, tpfh.Version)) != EC_NONE) goto fail; if ((bmOutput = new TPMemoryArea(tpfh.CanvasInfor.Width, tpfh.CanvasInfor.Height)) == NULL) { ec = EC_MEMORY_LACK; goto fail; } if ((bo = bmOutput->lock()) == NULL) { ec = EC_MEMORY_LACK; goto fail; } if (!bmOutput->LoadFromFile(fh, tpfh.Compress)) goto fail; bmOutput->unlock(); if (!ReadFile(fh, &mode, sizeof(bool), &dwRead, NULL)) goto fail; if (mode) { if ((ModeBitmap = new TTexpiaBitmap) == NULL) { ec = EC_MEMORY_LACK; goto fail; } if (!ReadFile(fh, &w, sizeof(short), &dwRead, NULL)) goto fail; if (!ReadFile(fh, &h, sizeof(short), &dwRead, NULL)) goto fail; if (!ModeBitmap->Create(w, h, 16)) { ec = EC_MEMORY_LACK; goto fail; } if (!ModeBitmap->LoadFromTexpiaFile(fh, tpfh.Compress)) goto fail; if (Dsim.machine == TRICOT) { if ((bmMode = new TPMemoryArea(w, h)) == NULL) { ec = EC_MEMORY_LACK; goto fail; } if ((bm = bmMode->lock()) == NULL) { ec = EC_MEMORY_LACK; goto fail; } if (!bmMode->LoadFromFile(fh, tpfh.Compress)) goto fail; bmMode->unlock(); } } } else { // DOS Version if ((ec = Yarn->LoadFromFile(fh, tpfh)) != EC_NONE) goto fail; if ((ec = Texture->LoadFromFile(fh, tpfh.Version)) != EC_NONE) goto fail; if (tpfh.Version.Method!='T' && tpfh.Version.Number<303) { if (!ReadFile(fh, &Dsim.machine, 1, &dwRead, NULL)) goto fail; if (!ReadFile(fh, &Dsim.edge, 1, &dwRead, NULL)) goto fail; if (!ReadFile(fh, &Dsim.ec, 1, &dwRead, NULL)) goto fail; if (!ReadFile(fh, &Dsim.ew, 1, &dwRead, NULL)) goto fail; if (!ReadFile(fh, &opt, 2, &dwRead, NULL)) goto fail; Dsim.spread = opt; if (!ReadFile(fh, &Dsim.cpi, 8, &dwRead, NULL)) goto fail; if (!ReadFile(fh, &Dsim.wpi, 8, &dwRead, NULL)) goto fail; if (!ReadFile(fh, &Dsim.move, 8, &dwRead, NULL)) goto fail; if (!ReadFile(fh, &Texture->Array[0].cut, 2, &dwRead, NULL)) goto fail; for (int i=1; iCount; i++) Texture->Array[i].cut = Texture->Array[0].cut; } else { if (!ReadFile(fh, &Dsim, sizeof(TPileCondition), &dwRead, NULL)) goto fail; } } Yarn->bFinish = true; Texture->bFinish = true; } return EC_NONE; fail: if (bo) bmOutput->unlock(); if (ec == EC_NONE) ec = EC_FILE_NOT_READ; return ec; } //--------------------------------------------------------------------------- TPException __fastcall TPlan::SaveToFile(HANDLE fh, TCompressMethod cm) { DWORD dwWrite; int n; short w, h; WORD *bo = NULL, *bm = NULL; TPException ec = EC_NONE; if (!WriteFile(fh, &Dsim, sizeof(TPileCondition), &dwWrite, NULL)) goto fail; if ((ec = Yarn->SaveToFile(fh)) != EC_NONE) goto fail; if ((ec = Texture->SaveToFile(fh)) != EC_NONE) goto fail; if ((bo = bmOutput->lock()) == NULL) { ec = EC_MEMORY_LACK; goto fail; } if (!bmOutput->SaveToFile(fh, cm)) goto fail; bmOutput->unlock(); if (mode && ModeBitmap) { mode = true; w = ModeBitmap->Width; h = ModeBitmap->Height; if (!WriteFile(fh, &mode, sizeof(bool), &dwWrite, NULL)) goto fail; if (!WriteFile(fh, &w, sizeof(short), &dwWrite, NULL)) goto fail; if (!WriteFile(fh, &h, sizeof(short), &dwWrite, NULL)) goto fail; if (!ModeBitmap->SaveToTexpiaFile(fh, cm)) goto fail; if (bmMode && Dsim.machine == TRICOT) { if ((bm = bmMode->lock()) == NULL) { ec = EC_MEMORY_LACK; goto fail; } if (!bmMode->SaveToFile(fh, cm)) goto fail; bmMode->unlock(); } } else { mode = false; if (!WriteFile(fh, &mode, sizeof(bool), &dwWrite, NULL)) goto fail; } return EC_NONE; fail: if (bo) bmOutput->unlock(); if (bm) bmMode->unlock(); if (ec == EC_NONE) ec = EC_FILE_NOT_WRITE; return ec; } // -----------------------------------------------------------------------