//--------------------------------------------------------------------------- #include #include #pragma hdrstop #include "Yarn.h" #include "MainImage.h" #include "TPUtils.hpp" //--------------------------------------------------------------------------- #pragma package(smart_init) double YarnRatio; #if defined(WEAVE) double YarnBrightness[7] = { -0.03, -0.02, -0.01, 0.0, 0.01, 0.02, 0.03 }; #elif defined(KNIT) double YarnBrightness[7] = { -0.12, -0.08, -0.04, 0.0, 0.04, 0.08, 0.12 }; #elif defined(PILE) double YarnBrightness[7] = { -0.12, -0.08, -0.04, 0.0, -0.02, -0.06, -0.10 }; #endif //--------------------------------------------------------------------------- __fastcall TYarnInfor::TYarnInfor() { memset(&Detail, 0, sizeof(TYarnDetail)); Detail.TYC = 1; Colors = 0; Color = NULL; Methods = 0; Method = NULL; Infor = NULL; } //--------------------------------------------------------------------------- __fastcall TYarnInfor::~TYarnInfor() { ClearData(); } //--------------------------------------------------------------------------- void __fastcall TYarnInfor::ClearData() { if (Color) { for (int i = 0; i < Colors; i++) { if (Color[i]) { delete Color[i]; Color[i] = NULL; } } delete[] Color; Color = NULL; Colors = 0; } if (Method) { delete[] Method; Method = NULL; Methods = 0; } if (Infor) { delete[] Infor; Infor = NULL; } } //--------------------------------------------------------------------------- void __fastcall TYarnInfor::Copy(TYarnInfor *yi) { int i; ClearData(); Detail.Type = yi->Detail.Type; Detail.TYC = yi->Detail.TYC; Detail.SZ = yi->Detail.SZ; Detail.Hair = yi->Detail.Hair; Detail.Count = yi->Detail.Count; Detail.TPI = yi->Detail.TPI; if (yi->Color) { Colors = yi->Colors; if (Colors > 0) { if ((Color = new TColorData *[Colors]) == NULL) { #ifdef TPDEBUG SHOWDEBUG; #endif } for (i = 0; i < Colors; i++) { if ((Color[i] = new TColorData) == NULL) { #ifdef TPDEBUG SHOWDEBUG; #endif } Color[i]->Copy(yi->Color[i]); } } } if (yi->Method) { Methods = yi->Methods; if (Methods > 0) { if ((Method = new TYarnMethod[Methods]) == NULL) { #ifdef TPDEBUG SHOWDEBUG; #endif } for (i = 0; i < Methods; i++) { Method[i].Code = i; Method[i].Number = yi->Method[i].Number; } } } Infor = NULL; } //--------------------------------------------------------------------------- bool __fastcall TYarnInfor::LoadFromFile(HANDLE fh, int YarnVerNo) { DWORD dwRead; int i; if(YarnVerNo < 110){ Byte temp[12]; if (!ReadFile(fh, temp, 12, &dwRead, NULL)) return false; Detail.Type = temp[0] & 0x07; Detail.TYC = (temp[0]>>3) & 0x07; Detail.SZ = (temp[0]>>6) & 0x01; Detail.Hair = (temp[0]>>7) & 0x01; Detail.Count = *(short *)(temp+2); Detail.TPI = *(double *)(temp+4); }else{ if (!ReadFile(fh, &Detail , sizeof(TYarnDetail), &dwRead, NULL)) return false; } if (!ReadFile(fh, &Colors, sizeof(short), &dwRead, NULL)) return false; if (Colors>0) { if ((Color = new TColorData *[Colors]) == NULL) { #ifdef TPDEBUG SHOWDEBUG; #endif } for (i=0; iLoadFromFile((int)fh, 1); } } if (!ReadFile(fh, &Methods, sizeof(short), &dwRead, NULL)) return false; if (Methods>0) { if ((Method = new TYarnMethod[Methods]) == NULL) { #ifdef TPDEBUG SHOWDEBUG; #endif } if (!ReadFile(fh, Method, sizeof(TYarnMethod)*Methods, &dwRead, NULL)) return false; } if (Detail.TYC>1) { if ((Infor = new TYarnInfor[Detail.TYC]) == NULL) { #ifdef TPDEBUG SHOWDEBUG; #endif } for (i=0; i0) { for (i=0; iSaveToFile((int)fh); } if (!WriteFile(fh, &Methods, sizeof(short), &dwWrite, NULL)) goto fail; if (Methods>0) { if (!WriteFile(fh, Method, sizeof(TYarnMethod)*Methods, &dwWrite, NULL)) goto fail; } if (Detail.TYC>1) { for (i=0; iCopy(cd); RGBQUAD2HLS(Color[3]->RGB, h, l, s); a = l+YarnBrightness[0]; if (a<0.0) a = 0.0; else if (a>1.0) a = 1.0; Color[0]->RGB = HLS2RGBQUAD(h, a, s); a = l+YarnBrightness[1]; if (a<0.0) a = 0.0; else if (a>1.0) a = 1.0; Color[1]->RGB = HLS2RGBQUAD(h, a, s); a = l+YarnBrightness[2]; if (a<0.0) a = 0.0; else if (a>1.0) a = 1.0; Color[2]->RGB = HLS2RGBQUAD(h, a, s); a = l+YarnBrightness[4]; if (a<0.0) a = 0.0; else if (a>1.0) a = 1.0; Color[4]->RGB = HLS2RGBQUAD(h, a, s); a = l+YarnBrightness[5]; if (a<0.0) a = 0.0; else if (a>1.0) a = 1.0; Color[5]->RGB = HLS2RGBQUAD(h, a, s); a = l+YarnBrightness[6]; if (a<0.0) a = 0.0; else if (a>1.0) a = 1.0; Color[6]->RGB = HLS2RGBQUAD(h, a, s); Index = i; } //--------------------------------------------------------------------------- // YarnData //--------------------------------------------------------------------------- __fastcall TYarnData::TYarnData() { memset(&Infor, 0, sizeof(TYarnInfor)); Map = NULL; Color = NULL; } //--------------------------------------------------------------------------- __fastcall TYarnData::~TYarnData() { Remove(&Infor); if (Map) { delete[] Map; Map = NULL; } if (Color) { for (int i = 0; i < Colors; i++) { delete Color[i]; Color[i] = NULL; } delete[] Color; Color = NULL; } } //--------------------------------------------------------------------------- // Private Methods //--------------------------------------------------------------------------- int __fastcall TYarnData::RecurrenceColorCount(TYarnInfor *pInfor, int n) { if (pInfor->Detail.TYC>1) { for (int i=0; iDetail.TYC; i++) { n = RecurrenceColorCount(pInfor->Infor+i, n); } } else { n += pInfor->Colors; } return n; } //--------------------------------------------------------------------------- int __fastcall TYarnData::RecurrenceColorTable(TYarnInfor *pInfor, int n) { int i; if (pInfor->Detail.TYC>1) { for (i=0; iDetail.TYC; i++) { n = RecurrenceColorTable(pInfor->Infor+i, n); } } else { pInfor->FirstColor = n; for (i=0; iColors; i++, n++) { Color[n]->SetColor(pInfor->Color[i]); } } return n; } //--------------------------------------------------------------------------- void __fastcall TYarnData::Remove(TYarnInfor *pInfor) { int i; if (pInfor->Detail.TYC>1) { if (pInfor->Infor) { for (i=0; iDetail.TYC; i++) { Remove(pInfor->Infor+i); } delete[] pInfor->Infor; pInfor->Infor = NULL; } } if (pInfor->Color) { for (i=0; iColors; i++) { delete pInfor->Color[i]; pInfor->Color[i] = NULL; } delete[] pInfor->Color; pInfor->Color = NULL; } if (pInfor->Method) { delete[] pInfor->Method; pInfor->Method = NULL; } } //--------------------------------------------------------------------------- // Public Methods //--------------------------------------------------------------------------- void __fastcall TYarnData::Rearrange() { int i; if (Color) { if (Colors > 0) { for (i = 0; i < Colors; i++) { delete Color[i]; Color[i] = NULL; } } delete[] Color; Color = NULL; } Colors = RecurrenceColorCount(&Infor, 0); if (Colors > 0) { if ((Color = new TGradeColor*[Colors]) == NULL) { #ifdef TPDEBUG SHOWDEBUG; #endif } for (i = 0; i < Colors; i++) { if ((Color[i] = new TGradeColor) == NULL) { #ifdef TPDEBUG SHOWDEBUG; #endif } } RecurrenceColorTable(&Infor, 0); } } //--------------------------------------------------------------------------- TPException __fastcall TYarnData::LoadFromFile(HANDLE fh, int YarnVerNo, bool bfn) { DWORD dwRead; int s, len; char *ch = NULL; TPException ec = EC_NONE; if (bfn) { if (!ReadFile(fh, &len, sizeof(int), &dwRead, NULL)) goto fail; if ((ch = (char *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len)) == NULL) { ec = EC_MEMORY_LACK; goto fail; } if (!ReadFile(fh, ch, len, &dwRead, NULL)) goto fail; FileName = AnsiString(ch, len); HeapFree(GetProcessHeap(), 0, ch); ch = NULL; } if (!ReadFile(fh, &Unit, sizeof(TYarnUnit), &dwRead, NULL)) goto fail; if (!ReadFile(fh, &DPI, sizeof(short), &dwRead, NULL)) goto fail; if (!ReadFile(fh, &Length, sizeof(short), &dwRead, NULL)) goto fail; if (!ReadFile(fh, &Width, sizeof(short), &dwRead, NULL)) goto fail; if (!ReadFile(fh, &Thick, sizeof(short), &dwRead, NULL)) goto fail; if (!ReadFile(fh, &Ratio, sizeof(double), &dwRead, NULL)) goto fail; if (!ReadFile(fh, &Dyed, sizeof(bool), &dwRead, NULL)) goto fail; if (!Infor.LoadFromFile(fh, YarnVerNo)) goto fail; if(YarnVerNo < 110){ if (Dyed) ExactThick = Thick; else ExactThick = Count2Dot(DPI, Unit, Infor.Detail.Count, Ratio); } else{ if( !ReadFile(fh, &ExactThick, sizeof(double), &dwRead, NULL)) goto fail; if (ExactThick == 0) ExactThick = Thick; //qe } s = (Dyed ? 2 : 1)*Length*Width; if ((Map = new Byte[s]) == NULL) { ec = EC_MEMORY_LACK; goto fail; } if (!ReadFile(fh, Map, s, &dwRead, NULL)) goto fail; Rearrange(); return EC_NONE; fail: if (Map) { delete[] Map; Map = NULL; } if (ch) HeapFree(GetProcessHeap(), 0, ch); if (ec == EC_NONE) ec = EC_FILE_NOT_READ; return ec; } //--------------------------------------------------------------------------- TPException __fastcall TYarnData::LoadFromFile(AnsiString fn) { HANDLE hFile = INVALID_HANDLE_VALUE; DWORD dwRead; TTexVersion v; Byte bc, *map = NULL, *sp, *dp; char *yp, cnum[4]; short count, colors, number, tot; int i, k, l, m, size, n, cp = 2; double r; TTexColor tc; TPException ec = EC_NONE; /* µÎ¹øÀÌ»ó ½ÇÇàÇÒ¶§ Memory Leak */ Remove(&Infor); if (Map) { delete[] Map; Map = NULL; } if (Color) { for (int i = 0; i < Colors; i++) { delete Color[i]; Color[i] = NULL; } delete[] Color; Color = NULL; } /* µÎ¹øÀÌ»ó ½ÇÇàÇÒ¶§ Memory Leak */ FileName = fn; if ((hFile = CreateFile(FileName.c_str(), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) { ec = EC_FILE_NOT_OPEN; goto fail; } if (!ReadFile(hFile, &v, sizeof(TTexVersion), &dwRead, NULL)) goto fail; if (v.Texpia=='P') { if (v.Method=='V') { DPI = 160; if (!ReadFile(hFile, &Length, sizeof(short), &dwRead, NULL)) goto fail; if (!ReadFile(hFile, &Width, sizeof(short), &dwRead, NULL)) goto fail; if (!ReadFile(hFile, &Thick, sizeof(short), &dwRead, NULL)) goto fail; if (!ReadFile(hFile, &Infor.Detail.Count, sizeof(short), &dwRead, NULL)) goto fail; if (!ReadFile(hFile, &Infor.Detail.TPI, sizeof(double), &dwRead, NULL)) goto fail; if (!ReadFile(hFile, &Ratio, sizeof(double), &dwRead, NULL)) goto fail; Dyed = false; if (!ReadFile(hFile, &bc, sizeof(Byte), &dwRead, NULL)) goto fail; Infor.Detail.TYC = bc&7; Infor.Detail.SZ = (bc>>3)&1; Infor.Detail.Hair = (bc>>4)&1; Unit.System = (bc>>5)&3; Unit.Method = (bc>>7)&1; if (!ReadFile(hFile, &bc, sizeof(Byte), &dwRead, NULL)) goto fail; if (Infor.Detail.TYC>1) { Infor.Detail.Type = 0; if ((Infor.Infor = new TYarnInfor[Infor.Detail.TYC]) == NULL) { ec = EC_MEMORY_LACK; goto fail; } memset(Infor.Infor, 0, sizeof(TYarnInfor)*Infor.Detail.TYC); for (i=0; i>= 2; } } else { Infor.Detail.Type = bc&3; } ExactThick = Count2Dot(DPI, Unit, Infor.Detail.Count, Ratio); if (!ReadFile(hFile, &colors, 2, &dwRead, NULL)) goto fail; size = Length*Width; if ((Map = new Byte[size]) == NULL) { ec = EC_MEMORY_LACK; goto fail; } memset(Map, 0, size); if ((map = new Byte[size]) == NULL) { ec = EC_MEMORY_LACK; goto fail; } if (!ReadFile(hFile, map, size, &dwRead, NULL)) goto fail; if (Infor.Detail.TYC>1) { for (k=0; kSetTexColor(tc); } } if (Infor.Infor[k].Detail.Type) { if (!ReadFile(hFile, &Infor.Infor[k].Methods, 2, &dwRead, NULL)) goto fail; if (Infor.Infor[k].Methods) { if ((Infor.Infor[k].Method = new TYarnMethod[Infor.Infor[k].Methods]) == NULL) { ec = EC_MEMORY_LACK; goto fail; } if (!ReadFile(hFile, Infor.Infor[k].Method, sizeof(TYarnMethod)*Infor.Infor[k].Methods, &dwRead, NULL)) goto fail; } } } for (k=0; kCopy(MainImageForm->Palette->ColorData[cp++]); } } } } else { if (SetFilePointer(hFile, 3, NULL, 1) == 0xFFFFFFFF) goto fail; if (!ReadFile(hFile, &Infor.Colors, 1, &dwRead, NULL)) goto fail; if (Infor.Colors) { if ((Infor.Color = new TColorData*[Infor.Colors]) == NULL) { ec = EC_MEMORY_LACK; goto fail; } for (i=0; iSetTexColor(tc); } } if (Infor.Detail.Type) { if (!ReadFile(hFile, &Infor.Methods, 2, &dwRead, NULL)) goto fail; if (Infor.Methods) { if ((Infor.Method = new TYarnMethod[Infor.Methods]) == NULL) { ec = EC_MEMORY_LACK; goto fail; } if (!ReadFile(hFile, Infor.Method, sizeof(TYarnMethod)*Infor.Methods, &dwRead, NULL)) goto fail; } } if (Infor.Color == NULL) { if (colors > 0) { Infor.Colors = colors; if ((Infor.Color = new TColorData*[Infor.Colors]) == NULL) { ec = EC_MEMORY_LACK; goto fail; } for (i=0; iCopy(MainImageForm->Palette->ColorData[cp++]); } } } } Rearrange(); sp = map; dp = (BYTE *)Map; if (Infor.Detail.TYC>1) { for (i=0; i>6)&3].FirstColor+((*sp>>3)&7))<<3)+(*sp&7); } } } else { for (i=0; i>3)&7))<<3)+(*sp&7); } } } delete[] map; } else if (v.Method=='T') { if ((ec = LoadFromFile(hFile, v.Number)) != EC_NONE) goto fail; } else goto fail; } else { DPI = 160; if (SetFilePointer(hFile, 0, NULL, 0) == 0xFFFFFFFF) goto fail; if (!ReadFile(hFile, &Length, 2, &dwRead, NULL)) goto fail; if (!ReadFile(hFile, &Width, 2, &dwRead, NULL)) goto fail; if (!ReadFile(hFile, &Thick, 2, &dwRead, NULL)) goto fail; Infor.Detail.Hair = (Thick==Width) ? 0 : 1; if (!ReadFile(hFile, &bc, 1, &dwRead, NULL)) goto fail; Infor.Detail.SZ = bc; if (!ReadFile(hFile, &bc, 1, &dwRead, NULL)) goto fail; Infor.Detail.TYC = bc; if (!ReadFile(hFile, &bc, 1, &dwRead, NULL)) goto fail; colors = bc; if (SetFilePointer(hFile, 3, NULL, 1) == 0xFFFFFFFF) goto fail; if (!ReadFile(hFile, &Infor.Detail.TPI, 8, &dwRead, NULL)) goto fail; Ratio = 2.0/3.0; Dyed = false; size = Length*Width; if ((Map = new Byte[size]) == NULL) { ec = EC_MEMORY_LACK; goto fail; } memset(Map, 0, size); if ((map = new Byte[size]) == NULL) { ec = EC_MEMORY_LACK; goto fail; } if (!ReadFile(hFile, map, size, &dwRead, NULL)) goto fail; if (!ReadFile(hFile, &bc, 1, &dwRead, NULL)) goto fail; Unit.Method = bc; if (!ReadFile(hFile, &bc, 1, &dwRead, NULL)) goto fail; Unit.System = bc; if (!ReadFile(hFile, &k, 4, &dwRead, NULL)) goto fail; Infor.Detail.Count = k; if (Infor.Detail.TYC>1) { if ((Infor.Infor = new TYarnInfor[Infor.Detail.TYC]) == NULL) { ec = EC_MEMORY_LACK; goto fail; } memset(Infor.Infor, 0, sizeof(TYarnInfor)*Infor.Detail.TYC); for (i=0; i1) { for (i=0; iCopy(MainImageForm->Palette->ColorData[cp++]); } } else { Infor.Colors = 1; if ((Infor.Color = new TColorData*[1]) == NULL) { ec = EC_MEMORY_LACK; goto fail; } if ((Infor.Color[0] = new TColorData) == NULL) { ec = EC_MEMORY_LACK; goto fail; } Infor.Color[0]->Copy(MainImageForm->Palette->ColorData[cp++]); } } else if (Infor.Detail.TYC==1) { // Rainbow Infor.Detail.Type = 2; Infor.Colors = colors; if ((Infor.Color = new TColorData*[Infor.Colors]) == NULL) { ec = EC_MEMORY_LACK; goto fail; } for (i=0; iCopy(MainImageForm->Palette->ColorData[cp++]); } //////////////////// 99/06/01 addition lsg Infor.Methods = colors; if ((Infor.Method = new TYarnMethod[Infor.Methods]) == NULL) { ec = EC_MEMORY_LACK; goto fail; } number = Length / colors; for (i = 0; i < Infor.Methods; i++) { Infor.Method[i].Code = i; Infor.Method[i].Number = number; } /////////////////// } else { // Melange Infor.Detail.Type = 1; size = colors / Infor.Detail.TYC; for (k = 0; k < Infor.Detail.TYC; k++) { Infor.Infor[k].Detail.Type = 1; if (k == Infor.Detail.TYC-1) size = colors - k * size; Infor.Infor[k].Colors = size; if ((Infor.Infor[k].Color = new TColorData*[Infor.Infor[k].Colors]) == NULL) { ec = EC_MEMORY_LACK; goto fail; } for (i=0; iCopy(MainImageForm->Palette->ColorData[cp++]); } //////////////////// 99/06/01 addition lsg Infor.Infor[k].Methods = Infor.Infor[k].Colors; if ((Infor.Infor[k].Method = new TYarnMethod[Infor.Infor[k].Colors]) == NULL) { ec = EC_MEMORY_LACK; goto fail; } number = 100 / Infor.Infor[k].Methods; tot = 0; for (i = 0; i < Infor.Infor[k].Methods; i++) { Infor.Infor[k].Method[i].Code = i; Infor.Infor[k].Method[i].Number = number; tot += number; } tot = 100 - tot; Infor.Infor[k].Method[i - 1].Number += tot; // interpolation /////////////////// } } Rearrange(); sp = map; dp = (BYTE *)Map; if (Infor.Detail.TYC>1) { n = (colors+1)/Infor.Detail.TYC+1; for (k=0; k>1; my = (sy>0) ? sy : 0; sy = (sy<0) ? -sy : 0; if (Dyed) { for (int y=sy ; y < ViewSize.cy-sy ; y++, my++) { dp = (WORD *) Map+ my *Length; for (int x=0; x<(LengthPixels[StartPoint.x+x][StartPoint.y+y] = clWhite; } else { value = *dp; b = ((value & 0x1F) << 3) | ((value & 0x1F) >> 2); value >>= 5; g = ((value & 0x1F) << 3) | ((value & 0x1F) >> 2); value >>= 5; r = ((value & 0x1F) << 3) | ((value & 0x1F) >> 2); Canvas->Pixels[StartPoint.x+x][StartPoint.y+y] = RGB(r, g, b); } } } } else { if (cp==NULL) cp = Color; for (int y=sy ; yPixels[StartPoint.x+x][StartPoint.y+y] = *wp ? TColor(RGBToTColor(cp[*wp>>3]->GetColor((*wp&7)-4)->RGB)) : clWhite; } } } #else Byte *wp, r, g, b; WORD *dp, value; if (Dyed) { for (int y=0; y < Width; y++) { dp = (WORD *) Map+y*Length; for (int x=0; x<(LengthPixels[StartPoint.x+x][StartPoint.y+y] = clWhite; } else { value = *dp; b = ((value & 0x1F) << 3) | ((value & 0x1F) >> 2); value >>= 5; g = ((value & 0x1F) << 3) | ((value & 0x1F) >> 2); value >>= 5; r = ((value & 0x1F) << 3) | ((value & 0x1F) >> 2); Canvas->Pixels[StartPoint.x+x][StartPoint.y+y] = RGB(r, g, b); } } } } else { if (cp==NULL) cp = Color; for (int y=0; yPixels[StartPoint.x+x][StartPoint.y+y] = *wp ? TColor(RGBToTColor(cp[*wp>>3]->GetColor((*wp&7)-4)->RGB)) : clWhite; } } } #endif } //--------------------------------------------------------------------------- // Extern Functions //--------------------------------------------------------------------------- static struct { struct { char *name; double F; // Yarn diameter d = sqrt(N)/F } d[2]; struct { char *name; double F; // Yarn diameter d = 1/(F*sqrt(N)) } i[4]; } Yunit = { { { "Tex", 678.6 }, //ÀϹݻç 1 g/1000 m { "Denier", 2036.0 }, //°ß,Àΰß,È­¼¶»ç 1 g/9000 m }, { { "Cotton", 28.0 }, //¸é»ç 840 yd/1 lb { "Worsted", 22.8 }, //¼Ò¸ð»ç 560 yd/1 lb { "Y.S.W.", 15.4 }, //¹æ¸ð»ç 256 yd/1 lb { "Linen", 16.7 } //¸¶»ç 300 yd/1 lb } }; //--------------------------------------------------------------------------- double __fastcall Count2Dot(Word dpi, TYarnUnit yu, short count, double ratio) { if (yu.Method==YU_DIRECT) { return (dpi*sqrt((double)count))/(Yunit.d[yu.System].F*ratio); } else { return dpi/(sqrt((double)count)*Yunit.i[yu.System].F*ratio); } } //--------------------------------------------------------------------------- double __fastcall Dot2Count(Word dpi, TYarnUnit yu, short dot, double ratio) { if (yu.Method==YU_DIRECT) { return pow((double)(Yunit.d[yu.System].F*ratio*dot)/dpi, 2.0); } else { return pow((double)dpi/(Yunit.i[yu.System].F*ratio*dot), 2.0); } } //--------------------------------------------------------------------------- short __fastcall ConvertYarnCount(Byte method, double r1, double r2, short count) { return (method==YU_DIRECT ? pow(r2/r1, 2.0) : pow(r1/r2, 2.0))*count; } //---------------------------------------------------------------------------