//--------------------------------------------------------------------------- #include #include #pragma hdrstop #include "YarnArrange.h" #include "MainImage.h" //--------------------------------------------------------------------------- #pragma package(smart_init) //--------------------------------------------------------------------------- // TYarnChoice //--------------------------------------------------------------------------- TPException __fastcall TYarnChoice::LoadFromFile(HANDLE fh, Word dpi, int YarnVer, bool rfn) { DWORD dwRead; int i, j, k, index; BYTE c; TColorData *cd = NULL; TPException ec = EC_NONE; if ((Data = new TYarnData) == NULL) { ec = EC_MEMORY_LACK; goto fail; } if ((ec = Data->LoadFromFile(fh, YarnVer, rfn)) != EC_NONE) goto fail; if (Data->Dyed) { if (!ReadFile(fh, &DyeColor, sizeof(COLORREF), &dwRead, NULL)) goto fail; } else { if (!ReadFile(fh, &c, sizeof(BYTE), &dwRead, NULL)) goto fail; if ((cd = new TColorData) == NULL) { ec = EC_MEMORY_LACK; goto fail; } for (i=0, k=1; i<8; i++, k<<=1) { if (c&k) { if ((Color[i] = new TGradeColor *[Data->Colors])==NULL) { ec = EC_MEMORY_LACK; goto fail; } for (j=0; jColors; j++) { if (!ReadFile(fh, &index, sizeof(int), &dwRead, NULL)) goto fail; cd->LoadFromFile((int)fh, 1); if ((Color[i][j] = new TGradeColor) == NULL) { ec = EC_MEMORY_LACK; goto fail; } Color[i][j]->SetColor(cd, index); } } } delete cd; } ypi = (double)dpi/Data->ExactThick; return EC_NONE; fail: if (cd) delete cd; if (Data) { for (i=0; i<8; i++) { if (Color[i]) { for (j=0; jColors; j++) { if (Color[i][j]) delete Color[i][j]; } delete[] Color[i]; Color[i] = NULL; } } delete Data; Data = NULL; } if (ec == EC_NONE) ec = EC_FILE_NOT_READ; return ec; } //--------------------------------------------------------------------------- TPException __fastcall TYarnChoice::SaveToFile(HANDLE fh) { DWORD dwWrite; int i, j; BYTE c; TPException ec = EC_NONE; if ((ec = Data->SaveToFile(fh)) != EC_NONE) goto fail; if (Data->Dyed) { if (!WriteFile(fh, &DyeColor, sizeof(COLORREF), &dwWrite, NULL)) goto fail; } else { c = 0; for (i=0, j=1; i<8; i++, j<<=1) { if (Color[i]) c |= j; } if (!WriteFile(fh, &c, sizeof(BYTE), &dwWrite, NULL)) goto fail; for (i=0; i<8; i++) { if (Color[i]) { for (j=0; jColors; j++) { if (!WriteFile(fh, &Color[i][j]->Index, sizeof(int), &dwWrite, NULL)) goto fail; Color[i][j]->GetColor(0)->SaveToFile((int)fh); } } } } return EC_NONE; fail: if (ec == EC_NONE) ec = EC_FILE_NOT_WRITE; return ec; } //--------------------------------------------------------------------------- // TYarnArrange //--------------------------------------------------------------------------- __fastcall TYarnArrange::TYarnArrange() { int i; for (i=0; i<2; i++) { Array[i] = new TList; //Array[0] ==> Warp Array[1] ==> Weft PData[i] = new TList; } for (i = 0; i < 8; i++) { Choice[i].Data = NULL; for (int j = 0; j < 8; j++) { Choice[i].Color[j] = NULL; } } ChoiceCount = 0; bSuckerDown = false; bFinish[0] = false; bFinish[1] = false; } //--------------------------------------------------------------------------- __fastcall TYarnArrange::~TYarnArrange() { int i, j; TYarnArray *ya, *pd; TColorData *cd; for (i = 0; i < 8; i++) { for (j = 0; j < 8; j++) { if (Choice[i].Color[j]) { for (int k=0; kColors; k++) { delete Choice[i].Color[j][k]; } delete[] Choice[i].Color[j]; Choice[i].Color[j] = NULL; } } if (Choice[i].Data) { delete Choice[i].Data; Choice[i].Data = NULL; } } /* while(Array[WEFT]->Count) { ya = (TYarnArray *) Array[WEFT]->Last(); Array[WEFT]->Remove(ya); delete ya; } delete Array[WEFT]; Array[WEFT] = NULL; while(Array[WARP]->Count) { ya = (TYarnArray *) Array[WARP]->Last(); Array[WARP]->Remove(ya); delete ya; } delete Array[WARP]; Array[WARP] = NULL; */ for (i=1; i>=0; i--) { while(Array[i]->Count) { ya = (TYarnArray *)Array[i]->Last(); Array[i]->Remove(ya); delete ya; } delete Array[i]; Array[i] = NULL; while(PData[i]->Count) { pd = (TYarnArray *)PData[i]->Last(); PData[i]->Remove(pd); delete pd; } delete PData[i]; PData[i] = NULL; } } //--------------------------------------------------------------------------- void __fastcall TYarnArrange::AddChoiceData(AnsiString fn, int i, int dpi) { if ((Choice[i].Data = new TYarnData) == NULL) goto fail; if (Choice[i].Data->LoadFromFile(fn) != EC_NONE) goto fail; Choice[i].ypi = (double)dpi/Choice[i].Data->ExactThick; if (!Choice[i].Data->Dyed) AddColor(i, 0); ChoiceCount++; return; fail: if (Choice[i].Data) { delete Choice[i].Data; Choice[i].Data = NULL; } } //--------------------------------------------------------------------------- void __fastcall TYarnArrange::ChangeChoiceData(AnsiString fn, int i, int dpi) { if ((Choice[i].Data = new TYarnData) == NULL) goto fail; if (Choice[i].Data->LoadFromFile(fn) != EC_NONE) goto fail; Choice[i].ypi = (double) dpi / Choice[i].Data->ExactThick; ChoiceCount++; return; fail: if (Choice[i].Data) { delete Choice[i].Data; Choice[i].Data = NULL; } } //--------------------------------------------------------------------------- void __fastcall TYarnArrange::AddColor(int i, int j) { Choice[i].Color[j] = new TGradeColor*[Choice[i].Data->Colors]; for (int k=0; k < Choice[i].Data->Colors; k++) Choice[i].Color[j][k] = new TGradeColor; } //--------------------------------------------------------------------------- void __fastcall TYarnArrange::RemoveColor(int i, int j) { if (Choice[i].Color[j]) { for (int k=0; k < Choice[i].Data->Colors; k++) { delete Choice[i].Color[j][k]; } delete[] Choice[i].Color[j]; Choice[i].Color[j] = NULL; } } //--------------------------------------------------------------------------- void __fastcall TYarnArrange::ChangeRemoveColor(int i, int j, int pc) //added by qe ½Çchange¸¦ À§ÇØ { if (Choice[i].Color[j]) { for (int k=0; k < pc; k++) { delete Choice[i].Color[j][k]; } delete[] Choice[i].Color[j]; Choice[i].Color[j] = NULL; } } //--------------------------------------------------------------------------- void __fastcall TYarnArrange::RemoveChoiceData(int i) { TColorData *cd; for (int j = 0; j < 8; j++) RemoveColor(i, j); if(Choice[i].Data) { delete Choice[i].Data; Choice[i].Data = NULL; ChoiceCount--; } } //--------------------------------------------------------------------------- void __fastcall TYarnArrange::ClearAllChoiceData() { for (int i = 0; i < 8; i++) { RemoveChoiceData(i); } ChoiceCount = 0; } //--------------------------------------------------------------------------- void __fastcall TYarnArrange::AddArray(int c, int n) { TYarnArray *yarnarray = new TYarnArray; yarnarray->code = c; yarnarray->method = 0; yarnarray->length = 0; yarnarray->yarns = 0; yarnarray->density = Choice[(c-1) / 8].ypi; yarnarray->repeat = 1; yarnarray->sucker = false; Array[n]->Add(yarnarray); } //--------------------------------------------------------------------------- void __fastcall TYarnArrange::InsertArray(int i, int n) { TYarnArray *yarnarray = new TYarnArray; yarnarray->code = 0; yarnarray->method = 0; yarnarray->length = 0; yarnarray->yarns = 0; yarnarray->density = 0; yarnarray->repeat = 1; yarnarray->sucker = false; Array[n]->Insert(i, yarnarray); } //--------------------------------------------------------------------------- void __fastcall TYarnArrange::RemoveArray(int i, int n) { TYarnArray *yarnarray; yarnarray = (TYarnArray *) Array[n]->Items[i]; Array[n]->Remove(yarnarray); delete yarnarray; } //--------------------------------------------------------------------------- void __fastcall TYarnArrange::ClearArray(int n) { TYarnArray *yarnarray; while(Array[n]->Count) { yarnarray = (TYarnArray *) Array[n]->Last(); Array[n]->Remove(yarnarray); delete yarnarray; } } //--------------------------------------------------------------------------- void __fastcall TYarnArrange::CopyFromWarpToWeft() { TYarnArray *warp, *weft; ClearArray(WEFT); for (int i = 0; i < Array[WARP]->Count; i++) { warp = (TYarnArray *) Array[WARP]->Items[i]; weft = new TYarnArray; weft->code = warp->code; weft->method = warp->method; weft->length = warp->length; weft->yarns = warp->yarns; weft->density = warp->density; weft->repeat = warp->repeat; Array[WEFT]->Add(weft); } } // ----------------------------------------------------------------------- TPException __fastcall TYarnArrange::LoadFromFile(HANDLE fh, TEXPIAFILEHEADER &tpfh) { DWORD dwRead; BYTE c, *sp, *dp; TYarnArray *ya, *pd; BYTE bc; BYTE color[8][16], *yp; short count; int i, j, k, l, m, n, t, dn, s, size; int YarnVerNo, cnt; double r; BYTE temp[28]; BYTE DdyeTemp[376]; Ddye dye[8]; Ydata *yd; Yinfor *yi; TPException ec = EC_NONE; if (tpfh.Version.Texpia=='P') { if (tpfh.Version.Method=='T' && tpfh.Version.Number>=200) { // Windows Version if(tpfh.Version.Number<240) {YarnVerNo = 100; cnt = 4;} else {YarnVerNo = 110; cnt = 8;} //Compiler Up¹®Á¦·Î file±¸Á¶ ¹Ù²î¾úÀ½.. if (!ReadFile(fh, &ChoiceCount, sizeof(int), &dwRead, NULL)) goto fail; if (!ReadFile(fh, &c, sizeof(BYTE), &dwRead, NULL)) goto fail; for (i=0, j=1; ilength, 2, &dwRead, NULL)) goto fail; if (!ReadFile(fh, &yi->width, 2, &dwRead, NULL)) goto fail; if (!ReadFile(fh, &yi->thick, 2, &dwRead, NULL)) goto fail; if (!ReadFile(fh, &bc, 1, &dwRead, NULL)) goto fail; yi->state.sz = bc; if (!ReadFile(fh, &bc, 1, &dwRead, NULL)) goto fail; yi->state.nty = bc; if (!ReadFile(fh, &bc, 1, &dwRead, NULL)) goto fail; yi->colors = bc; if (SetFilePointer(fh, 3, NULL, FILE_CURRENT) == 0xFFFFFFFF) goto fail; if (!ReadFile(fh, &yi->tpi, 8, &dwRead, NULL)) goto fail; if (SetFilePointer(fh, 4, NULL, FILE_CURRENT) == 0xFFFFFFFF) goto fail; if (!ReadFile(fh, &bc, 1, &dwRead, NULL)) goto fail; yi->state.system = bc; if (!ReadFile(fh, &bc, 1, &dwRead, NULL)) goto fail; yi->state.unit = bc; if (SetFilePointer(fh, 2, NULL, FILE_CURRENT) == 0xFFFFFFFF) goto fail; if (!ReadFile(fh, &dye[i].ypi, 8, &dwRead, NULL)) goto fail; yi->ratio = 2.0/3.0; yi->count = Dot2Count(160, TYarnUnit(yi->state.system, yi->state.unit), yi->thick, yi->ratio); if (dye[i].type) { ChoiceCount++; yi->state.hair = (yi->thick==yi->width) ? 0 : 1; s = yi->length*yi->width; if ((dye[i].map = new BYTE[s]) == NULL) { ec = EC_MEMORY_LACK; goto fail; } if (!ReadFile(fh, dye[i].map, s, &dwRead, NULL)) goto fail; if (yi->state.nty==yi->colors) { // Normal yi->type = 0; for (k=0; kstate.nty; k++) { yd = &dye[i].data[k]; yd->cnum = k; yd->colors = 1; for (j=0; j<8; j++) { dye[i].color[j][k<<3] = color[j][k]; } } yp = dye[i].map; for (j=0; jstate.nty==1) { // Rainbow yi->type = 2; yd = &dye[i].data[0]; yd->cnum = 0; yd->colors = yi->colors; for (j=0; j<8; j++) { for (k=0; kcolors; k++) { dye[i].color[j][k] = color[j][k]; } } yp = dye[i].map; for (j=0; j>1)+(*yp&0x0F)+1; else *yp = 0; } } else { // Melange yi->type = 0; size = (yi->colors+yi->state.nty-1)/ yi->state.nty; for (k=m=0; kstate.nty; k++) { n = yi->colors-k*size; if (n>size) n = size; yi->type |= 1<<(k<<1); yd = &dye[i].data[k]; yd->cnum = m; yd->colors = n; for (j=0; j<8; j++) { for (l=0; l>4; *yp = ((k/size)<<6)+((k%size)<<3)+(*yp&0x0F)+1; } else *yp = 0; } } for (j=0; jstate.nty; j++) { yd = &dye[i].data[j]; yd->color = NULL; yd->methods = 0; yd->method = NULL; } } } } else if ((tpfh.Version.Method=='V' || tpfh.Version.Method=='C') && tpfh.Version.Number<320) { ChoiceCount = 0; for (i=0; i<4; i++) { if (!ReadFile(fh, dye[i].fname, 14, &dwRead, NULL)) goto fail; if (!ReadFile(fh, &dye[i].dycp, 1, &dwRead, NULL)) goto fail; if (!ReadFile(fh, &dye[i].type, 1, &dwRead, NULL)) goto fail; if (!ReadFile(fh, dye[i].color[0], 256, &dwRead, NULL)) goto fail; if (!ReadFile(fh, &dye[i].ypi, 8, &dwRead, NULL)) goto fail; if (!ReadFile(fh, &dye[i].infor, sizeof(Yinfor), &dwRead, NULL)) goto fail; for (j=0; j<4; j++) { if (!ReadFile(fh, &dye[i].data[j].count, 2, &dwRead, NULL)) goto fail; if (!ReadFile(fh, &dye[i].data[j].cnum, 1, &dwRead, NULL)) goto fail; if (!ReadFile(fh, &dye[i].data[j].colors, 1, &dwRead, NULL)) goto fail; if (SetFilePointer(fh, 4, NULL, FILE_CURRENT) == 0xFFFFFFFF) goto fail; } if (SetFilePointer(fh, 4, NULL, FILE_CURRENT) == 0xFFFFFFFF) goto fail; if (dye[i].type) { ChoiceCount++; for (j=0; jcolor = new TTexColor[yd->colors]) == NULL) { ec = EC_MEMORY_LACK; goto fail; } if (!ReadFile(fh, yd->color, yd->colors*sizeof(TTexColor), &dwRead, NULL)) goto fail; yd->methods = 0; yd->method = NULL; } s = dye[i].infor.length*dye[i].infor.width; if ((dye[i].map = new BYTE[s]) == NULL) { ec = EC_MEMORY_LACK; goto fail; } if (!ReadFile(fh, dye[i].map, s, &dwRead, NULL)) goto fail; } } } else { ChoiceCount = 0; for (i=0; i<4; i++) { if (!ReadFile(fh, DdyeTemp, 376, &dwRead, NULL)) goto fail; memcpy(dye[i].fname, DdyeTemp, 14); dye[i].dycp = DdyeTemp[14]; dye[i].type = DdyeTemp[15]; memcpy(dye[i].color, DdyeTemp+16, 256); dye[i].ypi = *(double *)(DdyeTemp+272); memcpy(&dye[i].infor, DdyeTemp+280, 28); for(t = 0; t<4; t++) memcpy(&dye[i].data[t], DdyeTemp+308+16*t, 16); if (dye[i].type) { ChoiceCount++; for (j=0; jcolors) { if ((yd->color = new TTexColor[yd->colors]) == NULL) { ec = EC_MEMORY_LACK; goto fail; } if (!ReadFile(fh, yd->color, yd->colors*sizeof(TTexColor), &dwRead, NULL)) goto fail; } else yd->color = NULL; if (yd->methods) { if ((yd->method = new Ymethod[yd->methods]) == NULL) { ec = EC_MEMORY_LACK; goto fail; } if (!ReadFile(fh, yd->method, yd->methods*sizeof(Ymethod), &dwRead, NULL)) goto fail; } else yd->method = NULL; } s = dye[i].infor.length*dye[i].infor.width; if ((dye[i].map = new BYTE[s]) == NULL) { ec = EC_MEMORY_LACK; goto fail; } if (!ReadFile(fh, dye[i].map, s, &dwRead, NULL)) goto fail; } } } for (i=0; i<4; i++) { if (dye[i].type) { Choice[i].Data = new TYarnData; TYarnData *data = Choice[i].Data; data->FileName = dye[i].fname; data->DPI = 160; data->Length = dye[i].infor.length; data->Width = dye[i].infor.width; data->Thick = dye[i].infor.thick; data->Ratio = dye[i].infor.ratio; data->Dyed = false; TYarnInfor *infor = &data->Infor; infor->Detail.Count = dye[i].infor.count; data->Unit.System = dye[i].infor.state.unit; data->Unit.Method = dye[i].infor.state.system; infor->Detail.TYC = dye[i].infor.state.nty; infor->Detail.SZ = dye[i].infor.state.sz; infor->Detail.Hair = dye[i].infor.state.hair; infor->Detail.TPI = dye[i].infor.tpi; data->ExactThick = Count2Dot(data->DPI, data->Unit, infor->Detail.Count, data->Ratio); if (infor->Detail.TYC>1) { infor->Detail.Type = 0; infor->Colors = 0; infor->Color = NULL; infor->Infor = new TYarnInfor[infor->Detail.TYC]; for (k=0; kDetail.TYC; k++) { infor->Infor[k].Detail.Count = dye[i].data[k].count; infor->Infor[k].Detail.Type = (dye[i].infor.type>>(2*k))&3; infor->Infor[k].Detail.TYC = 1; infor->Infor[k].Detail.SZ = infor->Detail.SZ; infor->Infor[k].Detail.Hair = infor->Detail.Hair; infor->Infor[k].Detail.TPI = infor->Detail.TPI; infor->Infor[k].Colors = dye[i].data[k].colors; infor->Infor[k].Color = new TColorData *[dye[i].data[k].colors]; for (j=0; jInfor[k].Colors; j++) { infor->Infor[k].Color[j] = new TColorData; } if (dye[i].data[k].colors>0 && dye[i].data[k].color) { for (j=0; jInfor[k].Color[j]->SetTexColor(dye[i].data[k].color[j]); } } if (infor->Infor[k].Detail.Type>0) { infor->Infor[k].Methods = dye[i].data[k].methods; infor->Infor[k].Method = new TYarnMethod[dye[i].data[k].methods]; if (dye[i].data[k].methods>0) { for (j=0; jInfor[k].Method[j], &dye[i].data[k].method[j], 4); } } } } } else { infor->Detail.Type = dye[i].infor.type; infor->Colors = dye[i].infor.colors; infor->Color = new TColorData *[dye[i].infor.colors]; for (j=0; jColors; j++) { infor->Color[j] = new TColorData; } if (dye[i].data[0].colors>0 && dye[i].data[0].color) { for (j=0; jColor[j]->SetTexColor(dye[i].data[0].color[j]); } } if (infor->Detail.Type>0) { infor->Methods = dye[i].data[0].methods; infor->Method = new TYarnMethod[dye[i].data[0].methods]; if (dye[i].data[0].methods>0) { for (j=0; jMethod[j], &dye[i].data[0].method[j], 4); } } } } data->Rearrange(); sp = dye[i].map; data->Map = new BYTE[data->Length*data->Width]; memset(data->Map, 0, data->Length*data->Width); dp = (BYTE *)data->Map; if (infor->Detail.TYC>1) { for (j=0; jLength*data->Width; j++, sp++, dp++) { if (*sp) { *dp = ((infor->Infor[(*sp>>6)&3].FirstColor+((*sp>>3)&7))<<3)+(*sp&7); } else *dp = 0; } } else { for (j=0; jLength*data->Width; j++, sp++, dp++) { if (*sp) { *dp = ((infor->FirstColor+((*sp>>3)&7))<<3)+(*sp&7); } else *dp = 0; } } for (j=0; j<8; j++) { if (dye[i].type&(1<Colors]; for (l=k=0; lSetColor(MainImageForm->Palette->ColorData[n], n); } } } } Choice[i].ypi = (double)tpfh.CanvasInfor.DotsPerInch/data->ExactThick; } if (dye[i].type) { for (j=0; jcolor) delete yd->color; if (yd->method) delete yd->method; } if (dye[i].map) delete dye[i].map; } } } for (i=0; i<2; i++) { // Array[i] = new TList; if (!ReadFile(fh, &n, sizeof(int), &dwRead, NULL)) goto fail; for (j=0; j=240) { if (!ReadFile(fh, ya, sizeof(TYarnArray) , &dwRead, NULL)) goto fail; } else { if (!ReadFile(fh, temp, sizeof(TYarnArray) -12, &dwRead, NULL)) goto fail; ya->unused1 = temp[0]; ya->code = temp[1]; ya->method = temp[2]; ya->unused2 = temp[3]; ya->length = *(double *)(temp+4); ya->yarns = *(int *)(temp+12); ya->density = *(double *)(temp+16); ya->repeat = *(int *)(temp+24); ya->sucker = false; } Array[i]->Add(ya); } bFinish[i] = true; } if (tpfh.Version.Method=='T' && tpfh.Version.Number>=230) { // Windows Version for (i=0; i<2; i++) { // read production data // PData[i] = new TList; // i think some error is. if (!ReadFile(fh, &dn, sizeof(int), &dwRead, NULL)) goto fail; for (j=0; junused1 = temp[0]; pd->code = temp[1]; pd->method = temp[2]; pd->unused2 = temp[3]; pd->length = *(double *)(temp+4); pd->yarns = *(int *)(temp+12); pd->density = *(double *)(temp+16); pd->repeat = *(int *)(temp+24); } else { if (!ReadFile(fh, pd, sizeof(TYarnArray), &dwRead, NULL)) goto fail; } PData[i]->Add(pd); } } } else { for (i=0; i<2; i++) { // read production data // PData[i] = new TList; // i think some error is. for (j=0; jCount; j++) { ya = (TYarnArray *)Array[i]->Items[j]; pd = new TYarnArray; pd->code = ya->code; pd->method = ya->method; pd->length = ya->length; pd->yarns = ya->yarns; pd->density = ya->density; pd->repeat = ya->repeat; pd->sucker = false; PData[i]->Add(pd); } } } } return EC_NONE; fail: if (ec == EC_NONE) ec = EC_FILE_NOT_READ; return ec; } //--------------------------------------------------------------------------- TPException __fastcall TYarnArrange::SaveToFile(HANDLE fh) { DWORD dwWrite; int i, j, n; BYTE c; TYarnArray *ya; TPException ec = EC_NONE; if (!WriteFile(fh, &ChoiceCount, sizeof(int), &dwWrite, NULL)) goto fail; c = 0; for (i=0, j=1; i<8; i++, j<<=1) { if (Choice[i].Data) c |= j; } if (!WriteFile(fh, &c, sizeof(BYTE), &dwWrite, NULL)) goto fail; for (i=0; i<8; i++) { if (Choice[i].Data) { if ((ec = Choice[i].SaveToFile(fh)) != EC_NONE) goto fail; } } for (i=0; i<2; i++) { n = Array[i]->Count; if (!WriteFile(fh, &n, sizeof(int), &dwWrite, NULL)) goto fail; for (j=0; jItems[j]; if (!WriteFile(fh, ya, sizeof(TYarnArray), &dwWrite, NULL)) goto fail; } } for (i=0; i<2; i++) { //save production data n = PData[i]->Count; if (!WriteFile(fh, &n, sizeof(int), &dwWrite, NULL)) goto fail; for (j=0; jItems[j]; if (!WriteFile(fh, ya, sizeof(TYarnArray), &dwWrite, NULL)) goto fail; } } return EC_NONE; fail: if (ec == EC_NONE) ec = EC_FILE_NOT_WRITE; return ec; } //---------------------------------------------------------------------------