//--------------------------------------------------------------------------- #include #include #pragma hdrstop #include "File.h" #include "lzw.hpp" //#include "MainImage.h" //#include "Palette.h" //--------------------------------------------------------------------------- #pragma package(smart_init) //--------------------------------------------------------------------------- // TFileInfor //--------------------------------------------------------------------------- __fastcall TFileInfor::TFileInfor() { Bitmap = NULL; tBitmap = NULL; } //--------------------------------------------------------------------------- __fastcall TFileInfor::~TFileInfor() { if (tBitmap) delete tBitmap; if (Bitmap) delete Bitmap; } //--------------------------------------------------------------------------- // TDirectoryInfor //--------------------------------------------------------------------------- __fastcall TDirectoryInfor::TDirectoryInfor() { pDirectoryList = NULL; pFileList = NULL; } //--------------------------------------------------------------------------- __fastcall TDirectoryInfor::~TDirectoryInfor() { Clear(); } //--------------------------------------------------------------------------- // Private Function //--------------------------------------------------------------------------- void __fastcall TDirectoryInfor::FileListDestroy(TList *&pList) { TFileInfor *fi; if (pList) { while (pList->Count>0) { fi = (TFileInfor *)pList->First(); pList->Remove(fi); delete fi; } delete pList; pList = NULL; } } //--------------------------------------------------------------------------- void __fastcall TDirectoryInfor::DirectoryListDestroy(TList *&pList) { TDirectoryInfor *sdi; if (pList) { while (pList->Count>0) { sdi = (TDirectoryInfor *)pList->First(); pList->Remove(sdi); ListDestroy(sdi); delete sdi; } delete pList; pList = NULL; } } //--------------------------------------------------------------------------- void __fastcall TDirectoryInfor::ListDestroy(TDirectoryInfor *sdi) { FileListDestroy(sdi->pFileList); DirectoryListDestroy(sdi->pDirectoryList); } //--------------------------------------------------------------------------- // Public Function //--------------------------------------------------------------------------- void __fastcall TDirectoryInfor::Clear() { FileListDestroy(pFileList); DirectoryListDestroy(pDirectoryList); } //--------------------------------------------------------------------------- // TSearchFile //--------------------------------------------------------------------------- __fastcall TSearchFile::TSearchFile() : TObject() { FFileName = "*.*"; FMake = false; FSubDirectory = false; } //--------------------------------------------------------------------------- __fastcall TSearchFile::~TSearchFile() { } //--------------------------------------------------------------------------- // Private Function //--------------------------------------------------------------------------- void __fastcall TSearchFile::Add(TObject *Sender, int depth, TDirectoryInfor *di) { int rtn; TSearchRec sr; TDirectoryInfor *sdi; TFileInfor *fi; rtn = FindFirst(FileName, faReadOnly | faArchive, sr); while (rtn==0) { if (FMake) { fi = new TFileInfor; if (fi) { fi->Name = sr.Name; if (di->Path.Length()>0) { fi->Path = di->Path+"\\"+di->Name; } else { fi->Path = di->Name; } fi->Depth = depth; if (di->pFileList==NULL) di->pFileList = new TList; if (di->pFileList) { di->pFileList->Add(fi); if (FOnFile) FOnFile(Sender, &sr, depth, fi); } else { delete fi; } } } else { if (FOnFile) FOnFile(Sender, &sr, depth, NULL); } rtn = FindNext(sr); } FindClose(sr);// by jeegeo // ²À²À²À ¿©±â¼­ ´Ý¾ÆÁÖ¶ó-_-; ¾È±×·¯¸é À©2000À̻󿡼­ µð·ºÅ丮³ª È­ÀÏÀÇ »èÁ¦°¡ ¾ÈµÊ if (FSubDirectory) { rtn = FindFirst("*.*", faReadOnly | faArchive | faDirectory, sr); while (rtn==0) { if ((sr.Attr&faDirectory) && *sr.Name.c_str()!='.') { if (FMake) { sdi = new TDirectoryInfor; if (sdi) { sdi->Name = sr.Name; if (di->Path.Length()>0) { sdi->Path = di->Path+"\\"+di->Name; } else { sdi->Path = di->Name; } sdi->Depth = depth; if (di->pDirectoryList==NULL) di->pDirectoryList = new TList; if (di->pDirectoryList) { di->pDirectoryList->Add(sdi); if (FOnDirectory) FOnDirectory(Sender, &sr, depth, sdi); } else { delete sdi; } } } else { if (FOnDirectory) FOnDirectory(Sender, &sr, depth, NULL); } chdir(sr.Name.c_str()); Add(Sender, depth+1, sdi); chdir(".."); } rtn = FindNext(sr); } } FindClose(sr); } //--------------------------------------------------------------------------- TDirectoryInfor *__fastcall TSearchFile::SearchDirectory(AnsiString dn, TDirectoryInfor *di) { TDirectoryInfor *sdi; if (di->Name!=dn) { if (di->pDirectoryList) { for (int i=0; ipDirectoryList->Count; i++) { sdi = SearchDirectory(dn, (TDirectoryInfor *)di->pDirectoryList->Items[i]); if (sdi) return sdi; } } return NULL; } else { return di; } } //--------------------------------------------------------------------------- TFileInfor *__fastcall TSearchFile::SearchFile(AnsiString fn, TDirectoryInfor *di) { int i; TFileInfor *sfi; for (i=0; ipFileList->Count; i++) { sfi = (TFileInfor *)di->pFileList->Items[i]; if (fn==sfi->Name) return sfi; } if (di->pDirectoryList) { for (i=0; ipDirectoryList->Count; i++) { sfi = SearchFile(fn, (TDirectoryInfor *)di->pDirectoryList->Items[i]); if (sfi) return sfi; } } return NULL; } //--------------------------------------------------------------------------- void __fastcall TSearchFile::SetFileName(AnsiString fn) { if (fn!=FFileName) FFileName = fn; } //--------------------------------------------------------------------------- void __fastcall TSearchFile::SetInitDirectory(AnsiString id) { if (id!=FInitDirectory) FInitDirectory = id; } //--------------------------------------------------------------------------- void __fastcall TSearchFile::SetMake(bool b) { if (b!=FMake) FMake = b; } //--------------------------------------------------------------------------- void __fastcall TSearchFile::SetSubDirectory(bool b) { if (b!=FSubDirectory) FSubDirectory = b; } //--------------------------------------------------------------------------- // Public Function //--------------------------------------------------------------------------- void __fastcall TSearchFile::Create(TObject *Sender) { FData.Clear(); chdir(FInitDirectory.c_str()); Add(Sender, 0, &FData); } //--------------------------------------------------------------------------- TDirectoryInfor *__fastcall TSearchFile::RootDirectory() { return &FData; } //--------------------------------------------------------------------------- TDirectoryInfor *__fastcall TSearchFile::SearchDirectory(AnsiString dn) { return SearchDirectory(dn, &FData); } //--------------------------------------------------------------------------- TFileInfor *__fastcall TSearchFile::SearchFile(AnsiString fn) { return SearchFile(fn, &FData); } //--------------------------------------------------------------------------- // Extern Function //--------------------------------------------------------------------------- AnsiString __fastcall FullPathName(AnsiString dn, AnsiString fn) { char *str = dn.c_str(); if (str[dn.Length()-1] == '\\') return dn + fn; return dn + "\\" + fn; } //--------------------------------------------------------------------------- bool __fastcall LoadFromTexpiaTag(HANDLE fh, TFileInfor *fi, TTexVersion *ver) { TEXPIAFILEHEADER tpfh; int y, j, r, g, b, ec; TTexVersion Tex; RGBQUAD rgb[256]; Byte buf[320]; TPalette *palette = NULL; WORD BitCount; DWORD dwRead; if ((palette = new TPalette)==NULL) { ec = EC_MEMORY_LACK; goto fail; } if (!ReadFile(fh, &tpfh, sizeof(TEXPIAFILEHEADER), &dwRead, NULL)) { ec = EC_FILE_NOT_READ; goto fail; } if (ver) *ver = tpfh.Version; if (tpfh.Version.Texpia=='P' && tpfh.Version.Method=='T' && tpfh.Version.Number>=210) { if (!ReadFile(fh, &palette->UseColor, sizeof(Word), &dwRead, NULL)) { ec = EC_FILE_NOT_READ; goto fail; } palette->LoadFromFileHandle((int)fh, 1); if (!ReadFile(fh, &BitCount, sizeof(WORD), &dwRead, NULL)) { ec = EC_FILE_NOT_READ; goto fail; } if (BitCount==8) { palette->ToRGBQUAD(rgb, 256); fi->Bitmap->PixelFormat = pf8bit; fi->Bitmap->Width = 80; fi->Bitmap->Height = 100; if (fi->Bitmap->Empty) { ec = EC_MEMORY_LACK; goto fail; } SetDIBColorTable(fi->Bitmap->Canvas->Handle, 0, 256, rgb); if (tpfh.Version.Number<222) { for (y=0; y<100; y++) { if (!ReadFile(fh, buf, 80, &dwRead, NULL)) { ec = EC_FILE_NOT_READ; goto fail; } memcpy(fi->Bitmap->ScanLine[99-y], buf, 80); } } else { for (y=0; y<100; y++) { if (!ReadFile(fh, buf, 80, &dwRead, NULL)) { ec = EC_FILE_NOT_READ; goto fail; } memcpy(fi->Bitmap->ScanLine[y], buf, 80); } } } else { fi->Bitmap->PixelFormat = pf24bit; fi->Bitmap->Width = 80; fi->Bitmap->Height = 100; if (fi->Bitmap->Empty) { ec = EC_MEMORY_LACK; goto fail; } if (tpfh.Version.Number<222) { for (y=0; y<100; y++) { if (!ReadFile(fh, buf, 240, &dwRead, NULL)) { ec = EC_FILE_NOT_READ; goto fail; } memcpy(fi->Bitmap->ScanLine[99-y], buf, 240); } } else { for (y=0; y<100; y++) { if (!ReadFile(fh, buf, 240, &dwRead, NULL)) { ec = EC_FILE_NOT_READ; goto fail; } memcpy(fi->Bitmap->ScanLine[y], buf, 240); } } } } else { if (SetFilePointer(fh, 0, NULL, FILE_BEGIN)==0xFFFFFFFF) { ec = EC_FILE_NOT_READ; goto fail; } if (!ReadFile(fh, &Tex, sizeof(TTexVersion), &dwRead, NULL)) { ec = EC_FILE_NOT_READ; goto fail; } fi->Bitmap->PixelFormat = pf8bit; fi->Bitmap->Width = 80; fi->Bitmap->Height = 100; if (fi->Bitmap->Empty) { ec = EC_MEMORY_LACK; goto fail; } if (Tex.Method=='T' && Tex.Number>=200) { for (y=0; y<100; y++) { if (!ReadFile(fh, buf, 80, &dwRead, NULL)) { ec = EC_FILE_NOT_READ; goto fail; } memcpy(fi->Bitmap->ScanLine[99-y], buf, 80); } if (SetFilePointer(fh, 2, NULL, FILE_CURRENT)==0xFFFFFFFF) { ec = EC_FILE_NOT_READ; goto fail; } palette->LoadFromFileHandle((int)fh, 0); palette->ToRGBQUAD(rgb, 256); } else { for (y=0; y<100; y++) { if (!ReadFile(fh, buf, 80, &dwRead, NULL)) { ec = EC_FILE_NOT_READ; goto fail; } memcpy(fi->Bitmap->ScanLine[y], buf, 80); } rgb[0].rgbRed = 255; rgb[0].rgbGreen = 255; rgb[0].rgbBlue = 255; j = 1; for (r=0; r<=5; r++) { for (g=0; g<=5; g++) { for (b=0; b<=5; b++) { rgb[j].rgbRed = r*51; rgb[j].rgbGreen = g*51; rgb[j].rgbBlue = b*51; j = j+1; } } } for (r=j; r<=255; r++) { rgb[r].rgbRed = 255; rgb[r].rgbGreen = 255; rgb[r].rgbBlue = 255; } } SetDIBColorTable(fi->Bitmap->Canvas->Handle, 0, 256, rgb); } delete palette; return true; fail: if (palette) delete palette; //EXCEPTION_MESSAGE_OK(ec); return false; } //--------------------------------------------------------------------------- bool __fastcall LoadFromTexpiaStyleTag(HANDLE fh, TFileInfor *fi, TTexVersion *ver) { TEXPIAFILEHEADER tpfh; int y, j, r, g, b, ec, i, tt; TTexVersion Tex; RGBQUAD rgb[256]; Byte buf[320], *get; TPalette *palette = NULL; WORD BitCount; DWORD dwRead; TCanvas *canvas; if ((palette = new TPalette)==NULL) { ec = EC_MEMORY_LACK; goto fail; } if (!ReadFile(fh, &tpfh, sizeof(TEXPIAFILEHEADER), &dwRead, NULL)) { ec = EC_FILE_NOT_READ; goto fail; } if (ver) *ver = tpfh.Version; if (tpfh.Version.Texpia=='P' && tpfh.Version.Method=='T' && tpfh.Version.Number>=210) { if (!ReadFile(fh, &palette->UseColor, sizeof(Word), &dwRead, NULL)) { ec = EC_FILE_NOT_READ; goto fail; } palette->LoadFromFileHandle((int)fh, 1); if (!ReadFile(fh, &BitCount, sizeof(WORD), &dwRead, NULL)) { ec = EC_FILE_NOT_READ; goto fail; } if (BitCount==8) { palette->ToRGBQUAD(rgb, 256); if (!fi->tBitmap->Create(80, 100, 8, rgb)) { ec = EC_MEMORY_LACK; goto fail; } canvas = fi->tBitmap->CreateCanvas(); if (!canvas) { ec = EC_MEMORY_LACK; goto fail; } SetDIBColorTable(canvas->Handle, 0, 256, rgb); fi->tBitmap->DeleteCanvas(canvas); if (!(fi->tBitmap->StartScanLine())) { ec = EC_MEMORY_LACK; goto fail; } if (tpfh.Version.Number<222) { for (y=0; y<100; y++) { if (!ReadFile(fh, buf, 80, &dwRead, NULL)) { ec = EC_FILE_NOT_READ; goto fail; } get = fi->tBitmap->GetScanLine(99 - y); for ( i = 0 ; i < 80 ; i++) { get[i] = buf[i]; } fi->tBitmap->PutScanLine(99 - y); } } else { for (y=0; y<100; y++) { if (!ReadFile(fh, buf, 80, &dwRead, NULL)) { ec = EC_FILE_NOT_READ; goto fail; } get = fi->tBitmap->GetScanLine(y); for ( i = 0 ; i < 80 ; i++) { get[i] = buf[i]; } fi->tBitmap->PutScanLine(y); } } fi->tBitmap->StopScanLine(); } else { if (!fi->tBitmap->Create(80, 100, 24)) { ec = EC_MEMORY_LACK; goto fail; } fi->tBitmap->StartScanLine(); if (tpfh.Version.Number<222) { for (y=0; y<100; y++) { if (!ReadFile(fh, buf, 240, &dwRead, NULL)) { ec = EC_FILE_NOT_READ; goto fail; } get = fi->tBitmap->GetScanLine(99 - y); for (i=0, tt=0 ; i<80 ; i++, tt+=3) { get[tt] = buf[tt]; get[tt+1] = buf[tt+1]; get[tt+2] = buf[tt+2]; } fi->tBitmap->PutScanLine(99 - y); } } else { for (y=0; y<100; y++) { if (!ReadFile(fh, buf, 240, &dwRead, NULL)) { ec = EC_FILE_NOT_READ; goto fail; } get = fi->tBitmap->GetScanLine(y); for (i=0, tt=0 ; i<80 ; i++, tt+=3) { get[tt] = buf[tt]; get[tt+1] = buf[tt+1]; get[tt+2] = buf[tt+2]; } fi->tBitmap->PutScanLine(y); } } fi->tBitmap->StopScanLine(); } } else { if (SetFilePointer(fh, 0, NULL, FILE_BEGIN)==0xFFFFFFFF) { ec = EC_FILE_NOT_READ; goto fail; } if (!ReadFile(fh, &Tex, sizeof(TTexVersion), &dwRead, NULL)) { ec = EC_FILE_NOT_READ; goto fail; } //============================================= if (Tex.Method=='T' && Tex.Number>=200) { // TBitmap->TexpiaBitmapÀ¸·Î ¹Ù²Û°Í¿¡ ´ëÇÑ ¼öÁ¤ÀÌ ¾ÈµÇ¾ú´Ù. // ¾î¶² °æ¿ì¿¡ µé¾î¿À´ÂÁö È®ÀÎÇÒ ¼ö ¾ø´Ù. if (!fi->tBitmap->Create(80, 100, 8, rgb)) { ec = EC_MEMORY_LACK; goto fail; } fi->tBitmap->StartScanLine(); for (y=0; y<100; y++) { if (!ReadFile(fh, buf, 80, &dwRead, NULL)) { ec = EC_FILE_NOT_READ; goto fail; } get = fi->tBitmap->GetScanLine(99 - y); for ( i = 0 ; i < 80 ; i++) { get[i] = buf[i]; } fi->tBitmap->PutScanLine(99 - y); } if (SetFilePointer(fh, 2, NULL, FILE_CURRENT)==0xFFFFFFFF) { ec = EC_FILE_NOT_READ; goto fail; } palette->LoadFromFileHandle((int)fh, 0); palette->ToRGBQUAD(rgb, 256); fi->tBitmap->StopScanLine(); canvas = fi->tBitmap->CreateCanvas(); SetDIBColorTable(canvas->Handle, 0, 256, rgb); fi->tBitmap->DeleteCanvas(canvas); ShowMessage("Please tell me... by kjs"); } else { rgb[0].rgbRed = 0; rgb[0].rgbGreen = 0; rgb[0].rgbBlue = 0; rgb[1].rgbRed = 255; rgb[1].rgbGreen = 255; rgb[1].rgbBlue = 255; for (r=2; r<=255; r++) { rgb[r].rgbRed = 0; rgb[r].rgbGreen = 0; rgb[r].rgbBlue = 0; } if (!fi->tBitmap->Create(80, 100, 8, rgb)) { ec = EC_MEMORY_LACK; goto fail; } fi->tBitmap->StartScanLine(); for (y=0; y<100; y++) { if (!ReadFile(fh, buf, 80, &dwRead, NULL)) { ec = EC_FILE_NOT_READ; goto fail; } get = fi->tBitmap->GetScanLine(y); for ( i = 0 ; i < 80 ; i++) { get[i] = buf[i]; } fi->tBitmap->PutScanLine(y); } fi->tBitmap->StopScanLine(); } //============================================================= /* if (!fi->tBitmap->Create(80, 100, 8, rgb)) { ec = EC_MEMORY_LACK; goto fail; } if (Tex.Method=='T' && Tex.Number>=200) { fi->tBitmap->StartScanLine(); for (y=0; y<100; y++) { if (!ReadFile(fh, buf, 80, &dwRead, NULL)) { ec = EC_FILE_NOT_READ; goto fail; } get = fi->tBitmap->GetScanLine(99 - y); for ( i = 0 ; i < 80 ; i++) { get[i] = buf[i]; } fi->tBitmap->PutScanLine(99 - y); } if (SetFilePointer(fh, 2, NULL, FILE_CURRENT)==0xFFFFFFFF) { ec = EC_FILE_NOT_READ; goto fail; } palette->LoadFromFileHandle((int)fh, 0); palette->ToRGBQUAD(rgb, 256); fi->tBitmap->StopScanLine(); } else { fi->tBitmap->StartScanLine(); for (y=0; y<100; y++) { if (!ReadFile(fh, buf, 80, &dwRead, NULL)) { ec = EC_FILE_NOT_READ; goto fail; } get = fi->tBitmap->GetScanLine(y); for ( i = 0 ; i < 80 ; i++) { get[i] = buf[i]; } fi->tBitmap->PutScanLine(y); } fi->tBitmap->StopScanLine(); rgb[0].rgbRed = 255; rgb[0].rgbGreen = 255; rgb[0].rgbBlue = 255; j = 1; for (r=0; r<=5; r++) { for (g=0; g<=5; g++) { for (b=0; b<=5; b++) { rgb[j].rgbRed = r*51; rgb[j].rgbGreen = g*51; rgb[j].rgbBlue = b*51; j = j+1; } } } for (r=j; r<=255; r++) { rgb[r].rgbRed = 255; rgb[r].rgbGreen = 255; rgb[r].rgbBlue = 255; } } canvas = fi->tBitmap->CreateCanvas(); SetDIBColorTable(canvas->Handle, 0, 256, rgb); fi->tBitmap->DeleteCanvas(canvas); */ } delete palette; return true; fail: if (palette) delete palette; //EXCEPTION_MESSAGE_OK(ec); return false; } //--------------------------------------------------------------------------- void __fastcall MakeTexpiaTag(TTexpiaBitmap *tag, TTexpiaBitmap *tb, TRect src, int method) { // TCanvas *cv = NULL; double rx, ry; int w, h, x, y, hh, ww, mw, mh, yy, xx, rtn; HDC dcSrc = NULL, dcDst = NULL; Byte *srcp, *srcpp, *dstp; TPException ec = EC_NONE; RGBQUAD rgb[256]; Byte *DP, *SP; switch (method) { case 0 : if (tb->BitsPerPixel==8) { if (!(tag->Create(80, 100, 8, rgb))) {ec = EC_MEMORY_LACK; goto fail;} tb->GetColors(0, 256, rgb); tag->PutColors(0, 256, rgb); } else { if (!(tag->Create(80, 100, 24))) {ec = EC_MEMORY_LACK; goto fail;} } // if ((cv = tag->CreateCanvas())==NULL) { ec = EC_RESOURCE_LACK; goto fail; } // cv->Brush->Color = clWhite; // cv->FillRect(Rect(0, 0, 80, 100)); tag->FillRect(Rect(0, 0, 80, 100), clWhite); rx = 80.0/(src.Right-src.Left); ry = 100.0/(src.Bottom-src.Top); if (rx>ry) { w = (src.Right-src.Left)*ry; h = (src.Bottom-src.Top)*ry; } else { w = (src.Right-src.Left)*rx; h = (src.Bottom-src.Top)*rx; } /* if ((dcSrc = tb->CreateDC())==NULL) { ec = EC_RESOURCE_LACK; goto fail; } SetStretchBltMode(cv->Handle, COLORONCOLOR); rtn = StretchBlt(cv->Handle, 0, 0, w, h, dcSrc, src.Left, src.Top, src.Right-src.Left, src.Bottom-src.Top, SRCCOPY); tb->DeleteDC(dcSrc); dcSrc = NULL; tag->DeleteCanvas(cv); cv = NULL; */ if ((dcDst = tag->CreateDC())==NULL) { ec = EC_RESOURCE_LACK; goto fail; } if ((dcSrc = tb->CreateDC())==NULL) { ec = EC_RESOURCE_LACK; goto fail; } SetStretchBltMode(dcDst, COLORONCOLOR); rtn = StretchBlt(dcDst, 0, 0, w, h, dcSrc, src.Left, src.Top, src.Right-src.Left, src.Bottom-src.Top, SRCCOPY); tb->DeleteDC(dcSrc); dcSrc = NULL; tag->DeleteDC(dcDst); dcDst = NULL; if (!rtn) { tb->StartScanLine(); tag->StartScanLine(); if (tb->BitsPerPixel==8) { for (y=0; yGetScanLine(y*ry); dstp = tag->GetScanLine(y); for (x=0; xPutScanLine(y); } } else { for (y=0; yGetScanLine(y/ry); dstp = tag->GetScanLine(y); for (x=0; xPutScanLine(y); } } tb->StopScanLine(); tag->StopScanLine(); } break; case 1 : /// Style Tag........ tb->GetColors(0, 256, rgb); if (!(tag->Create(80, 100, 8, rgb))) {ec = EC_MEMORY_LACK; goto fail;} /* if ((cv = tag->CreateCanvas()) == NULL) {ec = EC_MEMORY_LACK; goto fail;} cv->Brush->Color = clWhite; cv->FillRect(Rect(0, 0, 80, 100)); tag->DeleteCanvas(cv); cv = NULL; */ tag->FillRect(Rect(0, 0, 80, 100), clWhite); mw = src.Right-src.Left; mh = src.Bottom-src.Top; rx = 80.0 / mw; ry = 100.0 / mh; if (rx>ry) { w = mw*ry; h = mh*ry; } else { w = mw*rx; h = mh*rx; } hh = Ceil((double) mh / h); ww = Ceil((double) mw / w); if (tb->StartScanLine() == false) {ec = EC_MEMORY_LACK; goto fail;} if (tag->StartScanLine() == false) {ec = EC_MEMORY_LACK; goto fail;} for (y = src.Top; y < src.Bottom; y++) { SP = (Byte *) tb->GetScanLine(y); yy = (y - src.Top) / hh; DP = (Byte *) tag->GetScanLine(yy); for (x = src.Left; x < src.Right; x++) { if (SP[x] > 1) { xx = (x - src.Left) / ww; DP[xx] = SP[x]; } } tag->PutScanLine(yy); } tag->StopScanLine(); tb->StopScanLine(); break; } return; fail : if (dcSrc) tb->DeleteDC(dcSrc); // if (cv) tag->DeleteCanvas(cv); if (dcDst) tag->DeleteDC(dcDst); //EXCEPTION_MESSAGE_OK(ec); } //--------------------------------------------------------------------------- TPException __fastcall LoadFromTexpiaFile(HANDLE fh, TPalette *pPalette, TEXPIAFILEHEADER &tpfh, TTexpiaBitmap *pBitmap, bool bTopLeft) { TTexVersion Tex; TTexColor TexPal[225]; int x, y, iw, ih, scan, offset; short ww, wh; HGLOBAL hMem = NULL; char *src = NULL, *sp; long attr, packsize; RGBQUAD rgb[256]; bool sw; Byte *bp; WORD BitCount; int w; TPMemory *memory = NULL; TPException ec = EC_NONE; DWORD dwRead; if (!ReadFile(fh, &tpfh, sizeof(TEXPIAFILEHEADER), &dwRead, NULL)) goto fail; if (tpfh.Version.Texpia=='P' && tpfh.Version.Method=='T' && tpfh.Version.Number>=210) { if (!ReadFile(fh, &pPalette->UseColor, sizeof(Word), &dwRead, NULL)) goto fail; pPalette->LoadFromFileHandle((int)fh, 1); if (!ReadFile(fh, &BitCount, sizeof(WORD), &dwRead, NULL)) goto fail; if (SetFilePointer(fh, BitCount==8 ? 8000 : 24000, NULL, FILE_CURRENT)== 0xFFFFFFFF) goto fail; if (pBitmap) { if (!pBitmap->Create(tpfh.CanvasInfor.Width, tpfh.CanvasInfor.Height, tpfh.BitsPerPixel)) { ec = EC_MEMORY_LACK; goto fail; } if (tpfh.BitsPerPixel==8) { pPalette->ToRGBQUAD(rgb, 256); pBitmap->PutColors(0, 256, rgb); if (tpfh.Version.Number<220) { scan = ((pBitmap->Width+3)/4)*4; if ((hMem = GlobalAlloc(GHND, scan*pBitmap->Height))==NULL) { ec = EC_MEMORY_LACK; goto fail; } if ((src = (Byte *)GlobalLock(hMem))==NULL) { ec = EC_MEMORY_LACK; goto fail; } if (!ReadFile(fh, src, scan*pBitmap->Height, &dwRead, NULL)) goto fail; if (!pBitmap->StartScanLine()) { ec = EC_MEMORY_LACK; goto fail; } sp = src; for (y=0; yHeight; y++) { bp = pBitmap->GetScanLine(y); memcpy(bp, sp, pBitmap->Width); pBitmap->PutScanLine(y); sp += scan; } pBitmap->StopScanLine(); GlobalUnlock(hMem); src = NULL; GlobalFree(hMem); hMem = NULL; } else { if (tpfh.Version.Number<222) { w = pBitmap->Width; switch (pBitmap->BitsPerPixel) { case 16: w *= 2; break; case 24: w *= 3; break; } if ((memory = new TPMemory(w*pBitmap->Height))==NULL) { ec = EC_MEMORY_LACK; goto fail; } if (memory->lock()==NULL) { ec = EC_MEMORY_LACK; goto fail; } if (!memory->LoadFromFile(fh, tpfh.Compress)) goto fail; pBitmap->LoadFromMemory(memory->pointer(), w); delete memory; memory = NULL; } else { if (!pBitmap->LoadFromTexpiaFile(fh, tpfh.Compress)) goto fail; } } } else { if (tpfh.Version.Number<220) { scan = ((3*pBitmap->Width+3)/4)*4; if ((hMem = GlobalAlloc(GHND, scan*pBitmap->Height))==NULL) { ec = EC_MEMORY_LACK; goto fail; } if ((src = (Byte *)GlobalLock(hMem))==NULL) { ec = EC_MEMORY_LACK; goto fail; } if (!ReadFile(fh, src, scan*pBitmap->Height, &dwRead, NULL)) goto fail; if (!pBitmap->StartScanLine()) { ec = EC_MEMORY_LACK; goto fail; } sp = src; for (y=0; yHeight; y++) { bp = pBitmap->GetScanLine(y); memcpy(bp, sp, 3*pBitmap->Width); pBitmap->PutScanLine(y); sp += scan; } pBitmap->StopScanLine(); GlobalUnlock(hMem); src = NULL; GlobalFree(hMem); hMem = NULL; } else { if (tpfh.Version.Number<222) { w = pBitmap->Width; switch (pBitmap->BitsPerPixel) { case 16: w *= 2; break; case 24: w *= 3; break; } if ((memory = new TPMemory(w*pBitmap->Height))==NULL) { ec = EC_MEMORY_LACK; goto fail; } if (memory->lock()==NULL) { ec = EC_MEMORY_LACK; goto fail; } if (!memory->LoadFromFile(fh, tpfh.Compress)) goto fail; pBitmap->LoadFromMemory(memory->pointer(), w); delete memory; memory = NULL; } else { if (!pBitmap->LoadFromTexpiaFile(fh, tpfh.Compress)) goto fail; } } } } } else { if (SetFilePointer(fh, 0, NULL, FILE_BEGIN)== 0xFFFFFFFF) goto fail; if (!ReadFile(fh, &Tex, sizeof(TTexVersion), &dwRead, NULL)) goto fail; if (SetFilePointer(fh, 8000, NULL, FILE_CURRENT)== 0xFFFFFFFF) goto fail; if (!ReadFile(fh, &pPalette->UseColor, sizeof(Word), &dwRead, NULL)) goto fail; if (pBitmap) { if (Tex.Method=='T') { if (Tex.Number<200) { if (!ReadFile(fh, TexPal, sizeof(TexPal), &dwRead, NULL)) goto fail; pPalette->SetTexPalette(TexPal, 225); sw = true; if (!ReadFile(fh, &attr, sizeof(long), &dwRead, NULL)) goto fail; tpfh.Compress = attr&0xF ? cmLZW : cmNone; if (!ReadFile(fh, &tpfh.CanvasInfor.DotsPerInch, sizeof(short), &dwRead, NULL)) goto fail; if (!ReadFile(fh, &ww, sizeof(short), &dwRead, NULL)) goto fail; if (!ReadFile(fh, &wh, sizeof(short), &dwRead, NULL)) goto fail; tpfh.CanvasInfor.SetSize(cstFree, ww, wh); } else if (Tex.Number<201) { pPalette->LoadFromFileHandle((int)fh, 0); if (!ReadFile(fh, &attr, sizeof(long), &dwRead, NULL)) goto fail; tpfh.Compress = attr&0xF ? cmLZW : cmNone; if (!ReadFile(fh, &tpfh.CanvasInfor.DotsPerInch, sizeof(short), &dwRead, NULL)) goto fail; sw = false; if (!ReadFile(fh, &iw, sizeof(int), &dwRead, NULL)) goto fail; if (!ReadFile(fh, &ih, sizeof(int), &dwRead, NULL)) goto fail; tpfh.CanvasInfor.SetSize(cstFree, iw, ih); } else { pPalette->LoadFromFileHandle((int)fh, 0); sw = false; if (!ReadFile(fh, &attr, sizeof(long), &dwRead, NULL)) goto fail; tpfh.Compress = attr&0xF ? cmLZW : cmNone; if (!ReadFile(fh, &tpfh.CanvasInfor, sizeof(TPCanvasInfor), &dwRead, NULL)) goto fail; } } else { if (!ReadFile(fh, TexPal, sizeof(TexPal), &dwRead, NULL)) goto fail; pPalette->SetTexPalette(TexPal, 225); sw = true; if (Tex.Method == 'C') { tpfh.Compress = cmLZW; attr = 1; } else { tpfh.Compress = cmNone; attr = 0; } tpfh.CanvasInfor.DotsPerInch = 160; if (!ReadFile(fh, &ww, sizeof(short), &dwRead, NULL)) goto fail; if (!ReadFile(fh, &wh, sizeof(short), &dwRead, NULL)) goto fail; tpfh.CanvasInfor.SetSize(cstFree, ww, wh); } pPalette->ToRGBQUAD(rgb, 256); if (pBitmap->Create(tpfh.CanvasInfor.Width, tpfh.CanvasInfor.Height, 8, rgb)==false) { ec = EC_MEMORY_LACK; goto fail; } if (attr&0x10) { scan = ((pBitmap->Width+3)/4)*4; if ((hMem = GlobalAlloc(GHND, scan*pBitmap->Height))==NULL) { ec = EC_MEMORY_LACK; goto fail; } if ((src = (Byte *)GlobalLock(hMem))==NULL) { ec = EC_MEMORY_LACK; goto fail; } if (!ReadFile(fh, src, scan*pBitmap->Height, &dwRead, NULL)) goto fail; } else { scan = pBitmap->Width; if ((hMem = GlobalAlloc(GHND, scan*pBitmap->Height))==NULL) { ec = EC_MEMORY_LACK; goto fail; } if ((src = (Byte *)GlobalLock(hMem))==NULL) { ec = EC_MEMORY_LACK; goto fail; } if (tpfh.Compress==cmLZW) { if ((offset = SetFilePointer(fh, 0, NULL, FILE_CURRENT))==0xFFFFFFFF) goto fail; if (Tex.Method=='T' && Tex.Number>100) { if (!ReadFile(fh, &packsize, sizeof(long), &dwRead, NULL)) goto fail; if (LZWdecoder_1((int)fh, src, packsize)!=scan*pBitmap->Height) goto fail; offset += 4+packsize; } else { offset += LZWdecoder((int)fh, src); } if (SetFilePointer(fh, offset, NULL, FILE_BEGIN)==0xFFFFFFFF) goto fail; } else { if (!ReadFile(fh, src, scan*pBitmap->Height, &dwRead, NULL)) goto fail; } } if (sw) { sp = src; for (y=0; yHeight; y++) { for (x=0; xWidth; x++) *(sp+x) += 1; sp += pBitmap->Width; } pPalette->UseColor++; } if (!pBitmap->StartScanLine()) { ec = EC_MEMORY_LACK; goto fail; } if (bTopLeft) { sp = src; for (y=0; yHeight; y++) { memcpy(pBitmap->GetScanLine(y), sp, pBitmap->Width); pBitmap->PutScanLine(y); sp += scan; } } else { sp = src+(pBitmap->Height-1)*scan; for (y=0; yHeight; y++) { memcpy(pBitmap->GetScanLine(y), sp, pBitmap->Width); pBitmap->PutScanLine(y); sp -= scan; } } pBitmap->StopScanLine(); GlobalUnlock(hMem); src = NULL; GlobalFree(hMem); hMem = NULL; } else { if (Tex.Method=='V') { if (SetFilePointer(fh, (int)sizeof(TTexColor), NULL, FILE_CURRENT)==0xFFFFFFFF) goto fail; if (!ReadFile(fh, TexPal, sizeof(TTexColor)*pPalette->UseColor, &dwRead, NULL)) goto fail; pPalette->SetTexPalette(TexPal, 225); } else { pPalette->LoadFromFileHandle((int)fh, 0); if (SetFilePointer(fh, 6, NULL, FILE_CURRENT)==0xFFFFFFFF) goto fail; } } } return EC_NONE; fail: if (hMem) { if (src) { pBitmap->StopScanLine(); GlobalUnlock(hMem); } GlobalFree(hMem); } if (memory) delete memory; if (ec == EC_NONE) ec = EC_FILE_NOT_READ; return ec; } //--------------------------------------------------------------------------- bool __fastcall SaveToTexpiaFile(HANDLE fh, TPalette *pPalette, TEXPIAFILEHEADER &tpfh, TTexpiaBitmap *tag, TTexpiaBitmap *pBitmap, RECT *rect) { WORD bpp; DWORD dwWrite; if (!WriteFile(fh, &tpfh, sizeof(TEXPIAFILEHEADER), &dwWrite, NULL)) return false; if (!WriteFile(fh, &pPalette->UseColor, sizeof(Word), &dwWrite, NULL)) return false; pPalette->SaveToFileHandle((int)fh); bpp = tag->BitsPerPixel; if (!WriteFile(fh, &bpp, sizeof(WORD), &dwWrite, NULL)) return false; if (!tag->SaveToTexpiaFile(fh, cmNone)) return false; if (pBitmap) { if (rect) { if (!pBitmap->SaveToTexpiaFile(fh, *rect, tpfh.Compress)) return false; } else { if (!pBitmap->SaveToTexpiaFile(fh, tpfh.Compress)) return false; } } return true; } //--------------------------------------------------------------------------- #ifdef TEXTILE #ifndef ACADEMY TGraphicFileFormat __fastcall FindExtension(AnsiString ext) { TGraphicFileFormat ExtIndex = gffNON; if (ext == ".tex") ExtIndex = gffTEX; else if (ext == ".tfc") ExtIndex = gffTFC; else if (ext == ".t3d") ExtIndex = gffT3D; #ifdef NOTN3D #else else if (ext == ".n3d") ExtIndex = gffN3D; #endif else if (ext == ".wea") ExtIndex = gffWEA; else if (ext == ".wav") ExtIndex = gffWAV; else if (ext == ".knt") ExtIndex = gffKNT; else if (ext == ".bmp") ExtIndex = gffBMP; else if (ext == ".jpg") ExtIndex = gffJPG; else if (ext == ".pct") ExtIndex = gffPCT; else if (ext == ".pcx") ExtIndex = gffPCX; else if (ext == ".png") ExtIndex = gffPNG; else if (ext == ".psd") ExtIndex = gffPSD; else if (ext == ".ras") ExtIndex = gffRAS; else if (ext == ".tga") ExtIndex = gffTGA; else if (ext == ".tif") ExtIndex = gffTIF; else if (ext == ".eps") ExtIndex = gffEPS; else ExtIndex = gffNON; return ExtIndex; } #endif #endif //---------------------------------------------------------------------------