• R/O
  • SSH

wp2latex: Commit

WP2LaTeX sources.


Commit MetaInfo

Revision397865a071123276f2f064fcd97ac526492f53a4 (tree)
Zeit2022-09-30 05:40:58
AutorFojtik
CommiterFojtik

Log Message

TGA reader and writer fix.

Ändern Zusammenfassung

Diff

diff -r 4d266d6a304d -r 397865a07112 trunk/sources.cc/images/ras_img.cc
--- a/trunk/sources.cc/images/ras_img.cc Wed Sep 28 15:44:12 2022 +0200
+++ b/trunk/sources.cc/images/ras_img.cc Thu Sep 29 22:40:58 2022 +0200
@@ -920,7 +920,7 @@
920920 #else
921921 Header.bfOffBits = 14 + 40;
922922 #endif
923- if(StoredPlanes==1 || StoredPlanes==4 || StoredPlanes==8)
923+ if(StoredPlanes==1 || StoredPlanes==4 || StoredPlanes==8) // Palette is arbitrary for planes <=8
924924 Header.bfOffBits+=(1 << (StoredPlanes + 2));
925925 Header.bfSize = Header.bfOffBits + CurrImg->Raster->Size2D*((ldblk+3) & 0xFFFC);
926926
@@ -2216,7 +2216,7 @@
22162216
22172217 NewChunk:
22182218 memset(Axes,0,sizeof(Axes));
2219-// memset(&attr,0,sizeof(attr));
2219+ // memset(&attr,0,sizeof(attr));
22202220 Naxis = 0;
22212221 memset(HDU,0,sizeof(HDU));
22222222
@@ -5832,12 +5832,14 @@
58325832 //--------------------------TGA----------------------------
58335833 #ifdef SupportTGA
58345834
5835+//http://www.paulbourke.net/dataformats/tga/
5836+
58355837 struct TGAHeader
58365838 {
58375839 BYTE IdentifLen;
58385840 BYTE ColorMapType;
5839- BYTE ImageCodeType;
5840-
5841+ BYTE ImageCodeType; ///<Compression and color types (0-No Image Data Included. 1-Uncompressed, Color mapped image, 2-Uncompressed, True Color Image,
5842+ ///< 3-Uncompressed, black and white images, 9-Run-length encoded, Color mapped image, 10 - Runlength encoded RGB images, 11-Run-Length encoded, Black and white image)
58415843 WORD CMapOrigin;
58425844 WORD CMapLength;
58435845 BYTE CMapEntrySize;
@@ -5882,46 +5884,52 @@
58825884
58835885 if((Header.ImageCodeType<1 || Header.ImageCodeType>3) ||
58845886 (Header.ColorMapType>=2))
5885- { //LoadPictureTGA:=ErrAnother;
5886- goto ENDPROC;
5887- }
5888-
5889- Raster=CreateRaster2D(Header.Width,Header.Height,Header.ImagePixSize);
5887+ { //LoadPictureTGA:=ErrAnother;
5888+ goto ENDPROC;
5889+ }
5890+
5891+ // ImagePixSize==16 has representation ARRRRRGG GGGBBBBB.
5892+ if(Header.ImagePixSize==32)
5893+ Raster = CreateRaster2DRGBA(Header.Width,Header.Height,8);
5894+ else
5895+ Raster = CreateRaster2D(Header.Width,Header.Height,Header.ImagePixSize);
58905896 if(Raster==NULL) goto ENDPROC;
58915897
58925898 fseek(f,18+Header.IdentifLen,SEEK_SET);
58935899
5894-// if AlineProc<>nil then AlineProc^.InitPassing(p.y,'Loading TGA');
5900+ // if AlineProc<>nil then AlineProc^.InitPassing(p.y,'Loading TGA');
58955901 if(Header.ColorMapType==1)
5896- {
5902+ {
58975903 Palette=BuildPalette(1 << Header.ImagePixSize,8);
58985904 if(Header.CMapEntrySize == 24)
58995905 { // Pozor Muze se zhroutit pri spatne tabulce
59005906 fread((char *)Palette->Data1D + 3*Header.CMapOrigin,
59015907 3*Header.CMapLength,1,f);
59025908 RGB_BGR((char *)Palette->Data1D + 3*Header.CMapOrigin,Header.CMapLength);
5903- };
5909+ }
59045910 fseek(f,18+Header.IdentifLen + (Header.CMapEntrySize / 8)*Header.CMapLength,SEEK_SET);
5905- };
5906-
5907-// if(Header.ImageCodeType==ImagePalette) ColorMode=ImagePalette;
5908- ldblk=(Raster->GetPlanes()*Raster->GetSize1D()+7)/8;
5909- if(Header.ImageDescByte==32)
5911+ }
5912+
5913+ // if(Header.ImageCodeType==ImagePalette) ColorMode=ImagePalette;
5914+ ldblk = (Raster->GetPlanes()*Raster->GetSize1D()+7)/8;
5915+ if((Header.ImageDescByte&32) == 32) // Flop image when descriptor contains 32.
59105916 for(i=0; i<Raster->Size2D; i++)
59115917 {
59125918 if(fread(Raster->GetRow(i),ldblk,1,f)!=1) {goto ENDPROC;}
5913- if(Header.ImageCodeType==2) RGB_BGR((char *)Raster->GetRow(i),Raster->GetSize1D());
5914-// if AlineProc<>nil then AlineProc^.NextLine;
5919+ if(Header.ImageCodeType==2 && Raster->GetPlanes()==24)
5920+ RGB_BGR((char *)Raster->GetRow(i),Raster->GetSize1D());
5921+ // if AlineProc<>nil then AlineProc^.NextLine;
59155922 }
59165923 else
59175924 {
59185925 i = Raster->Size2D;
59195926 while(i > 0)
59205927 {
5921- i--;
5922- if(fread(Raster->GetRow(i),ldblk,1,f)!=1) {goto ENDPROC;}
5923- if(Header.ImageCodeType==2) RGB_BGR((char *)Raster->GetRow(i),Raster->GetSize1D());
5924-// if AlineProc<>nil then AlineProc^.NextLine;
5928+ i--;
5929+ if(fread(Raster->GetRow(i),ldblk,1,f)!=1) {goto ENDPROC;}
5930+ if(Header.ImageCodeType==2 && Raster->GetPlanes()==24)
5931+ RGB_BGR((char *)Raster->GetRow(i),Raster->GetSize1D());
5932+ // if AlineProc<>nil then AlineProc^.NextLine;
59255933 }
59265934 }
59275935
@@ -5950,19 +5958,17 @@
59505958 }
59515959
59525960
5953-int SavePictureTGA(const char *Name,const Image &Img)
5961+int SavePictureTGA(const char *Name, const Image &Img)
59545962 {
59555963 FILE *f;
59565964 TGAHeader Header;
5957-char ImageID[256] = ""; //???
5958-//char Author[41] = "";
5959-//char SoftwareID[41] = "";
59605965 WORD x, y;
59615966
59625967 if(Img.Raster==NULL) return(-10);
59635968 if((f=fopen(Name,"wb"))==NULL) return(-1);
59645969
5965- Header.IdentifLen=strlen(ImageID); //??? Start with the lenght of the Image ID string. ??
5970+ Header.IdentifLen = 0; //strlen(ImageID); //??? Start with the lenght of the Image ID string. ??
5971+ Header.ImageDescByte = 32;
59665972 if(Img.Raster->GetPlanes()==8 && Img.Palette!=NULL)
59675973 {
59685974 Header.ColorMapType = 1; // Then the Color Map Type.
@@ -5975,15 +5981,20 @@
59755981 else
59765982 {
59775983 Header.ColorMapType = 0; //There's no color map here (0).
5978- if(Img.Raster->GetPlanes()==1)
5979- {
5980- Header.ImageCodeType = 3; //And then the Image Type. Uncompressed Monochromatic.
5981- Header.ImagePixSize = 1;
5982- }
5983- else
5984- {
5985- Header.ImageCodeType = 2; //Uncompressed True Color (2).
5986- Header.ImagePixSize = 24;
5984+ switch(Img.Raster->GetPlanes())
5985+ {
5986+ case 1: Header.ImageCodeType = 3; //And then the Image Type. Uncompressed Monochromatic.
5987+ Header.ImagePixSize = 1;
5988+ break;
5989+
5990+ case 16: Header.ImageCodeType = 2; // Uncompressed ARRRRRGG GGGBBBBB.
5991+ Header.ImagePixSize = 16;
5992+ Header.ImageDescByte |= 1;
5993+ break;
5994+
5995+ default: Header.ImageCodeType = 2; //Uncompressed True Color (2).
5996+ Header.ImagePixSize = 24;
5997+ break;
59875998 }
59885999 Header.CMapLength = 0;
59896000 Header.CMapEntrySize = 0;
@@ -5996,19 +6007,25 @@
59966007 * the screen. */
59976008 Header.Width = Img.Raster->GetSize1D();
59986009 Header.Height = Img.Raster->Size2D;
5999- Header.ImageDescByte = 32;
60006010
60016011 SaveTgaHeader(f,Header);
60026012
6003- /* Now we write the Image ID string. */
6004- if (fwrite(ImageID,strlen(ImageID),1,f) != 1)
6005- {
6006- fclose(f);
6007- return -1;
6013+ if(Header.IdentifLen > 0)
6014+ { /* Now we write the Image ID string. */
6015+ //char ImageID[256] = ""; //???
6016+ //char Author[41] = "";
6017+ //char SoftwareID[41] = "";
6018+ //if (fwrite(ImageID,strlen(ImageID),1,f) != 1)
6019+ //{
6020+ // fclose(f);
6021+ // return -1;
6022+ //}
6023+ for(int i=0; i<Header.IdentifLen; i++)
6024+ fputc(0,f);
60086025 }
60096026
6010- /* This is where the color map should go if this was not True Color. */
6011- if(Header.ImageCodeType==1)
6027+ /* This is where the color map should go if this was not True Color. */
6028+ if(Header.ImageCodeType==1)
60126029 {
60136030 for(x=0; x<Header.CMapLength ;x++)
60146031 {
@@ -6031,6 +6048,10 @@
60316048 }
60326049 break;
60336050
6051+ case 16:
6052+ fwrite(Img.Raster->GetRow(y),2*Header.Width,1,f);
6053+ break;
6054+
60346055 case 1:
60356056 fwrite(Img.Raster->GetRow(y),(Header.Width+7)/8,1,f);
60366057 break;
Show on old repository browser