]> Creatis software - clitk.git/blob - utilities/CxImage/ximaska.cpp
cosmetic
[clitk.git] / utilities / CxImage / ximaska.cpp
1 /*
2  * File:        ximaska.cpp
3  * Purpose:     Platform Independent SKA Image Class Loader and Writer
4  * 25/Sep/2007 Davide Pizzolato - www.xdp.it
5  * CxImage version 6.0.0 02/Feb/2008
6  */
7
8 #include "ximaska.h"
9
10 #if CXIMAGE_SUPPORT_SKA
11
12 ////////////////////////////////////////////////////////////////////////////////
13 #if CXIMAGE_SUPPORT_DECODE
14 ////////////////////////////////////////////////////////////////////////////////
15 bool CxImageSKA::Decode(CxFile *hFile)
16 {
17         if (hFile==NULL)
18                 return false;
19
20         // read the  header
21         SKAHEADER ska_header;
22         hFile->Read(&ska_header,sizeof(SKAHEADER),1);
23
24     ska_header.Width = ntohs(ska_header.Width);
25     ska_header.Height = ntohs(ska_header.Height);
26     ska_header.dwUnknown = ntohl(ska_header.dwUnknown);
27
28         // check header
29         if (ska_header.dwUnknown != 0x01000000 ||
30                 ska_header.Width > 0x7FFF || ska_header.Height > 0x7FFF ||
31                 ska_header.BppExp != 3)
32                 return false;
33
34         if (info.nEscape == -1){
35                 head.biWidth = ska_header.Width ;
36                 head.biHeight= ska_header.Height;
37                 info.dwType = CXIMAGE_FORMAT_SKA;
38                 return true;
39         }
40
41         int bpp = 1<<ska_header.BppExp;
42
43         Create(ska_header.Width,ska_header.Height,bpp,CXIMAGE_FORMAT_SKA);
44         if (!IsValid())
45                 return false;
46
47         // read the palette
48         int nColors = 1<<bpp;
49         rgb_color* ppal = (rgb_color*)malloc(nColors*sizeof(rgb_color));
50         if (!ppal) return false;
51         hFile->Read(ppal,nColors*sizeof(rgb_color),1);
52         SetPalette(ppal,nColors);
53         free(ppal);
54
55         //read the image
56         hFile->Read(GetBits(),ska_header.Width*ska_header.Height,1);
57
58         //reorder rows
59         if (GetEffWidth() != ska_header.Width){
60                 BYTE *src,*dst;
61                 src = GetBits() + ska_header.Width*(ska_header.Height-1);
62                 dst = GetBits(ska_header.Height-1);
63                 for(int y=0;y<ska_header.Height;y++){
64                         memcpy(dst,src,ska_header.Width);
65                         src -= ska_header.Width;
66                         dst -= GetEffWidth();
67                 }
68         }
69
70         Flip();
71
72         return true;
73 }
74 ////////////////////////////////////////////////////////////////////////////////
75 #endif //CXIMAGE_SUPPORT_DECODE
76 ////////////////////////////////////////////////////////////////////////////////
77 #if CXIMAGE_SUPPORT_ENCODE
78 ////////////////////////////////////////////////////////////////////////////////
79 bool CxImageSKA::Encode(CxFile * hFile)
80 {
81         if (EncodeSafeCheck(hFile)) return false;
82
83         if(head.biBitCount > 8) {
84                 strcpy(info.szLastError,"SKA Images must be 8 bit or less");
85                 return false;
86         }
87
88         SKAHEADER ska_header;
89
90         ska_header.Width = (unsigned short)GetWidth();
91         ska_header.Height = (unsigned short)GetHeight();
92         ska_header.BppExp = 3;
93         ska_header.dwUnknown = 0x01000000;
94
95     ska_header.Width = ntohs(ska_header.Width);
96     ska_header.Height = ntohs(ska_header.Height);
97     ska_header.dwUnknown = ntohl(ska_header.dwUnknown);
98
99         hFile->Write(&ska_header,sizeof(SKAHEADER),1);
100
101     ska_header.Width = ntohs(ska_header.Width);
102     ska_header.Height = ntohs(ska_header.Height);
103     ska_header.dwUnknown = ntohl(ska_header.dwUnknown);
104
105         if (head.biBitCount<8) IncreaseBpp(8);
106
107         rgb_color pal[256];
108         for(int idx=0; idx<256; idx++){
109                 GetPaletteColor(idx,&(pal[idx].r),&(pal[idx].g),&(pal[idx].b));
110         }
111
112         hFile->Write(pal,256*sizeof(rgb_color),1);
113
114         BYTE* src = GetBits(ska_header.Height-1);
115         for(int y=0;y<ska_header.Height;y++){
116                 hFile->Write(src,ska_header.Width,1);
117                 src -= GetEffWidth();
118         }
119
120         return true;
121 }
122 ////////////////////////////////////////////////////////////////////////////////
123 #endif // CXIMAGE_SUPPORT_ENCODE
124 ////////////////////////////////////////////////////////////////////////////////
125 #endif // CXIMAGE_SUPPORT_SKA
126