00001
00002
00003
00004
00005
00006
00007
00009
00010
00011 #include <wx/wxprec.h>
00012 #include "flexilog.h"
00013
00014 #ifdef __BORLANDC__
00015 #pragma hdrstop
00016 #endif
00017
00018
00019
00020 #include "imagrgb.h"
00021
00022 #ifndef WX_PRECOMP
00023 #include "wx/intl.h"
00024 #include "wx/log.h"
00025 #endif
00026
00027 #include "wx/txtstrm.h"
00028
00029
00030
00031
00032
00033
00034
00035
00036 void wxRGBHandler::ConvertShort(unsigned short *array, long length)
00037 {
00038 unsigned b1, b2;
00039 unsigned char *ptr;
00040
00041 ptr = (unsigned char *)array;
00042 while (length--) {
00043 b1 = *ptr++;
00044 b2 = *ptr++;
00045 *array++ = (b1 << 8) | (b2);
00046 }
00047 }
00048
00049 void wxRGBHandler::ConvertLong(unsigned *array, long length)
00050 {
00051 unsigned b1, b2, b3, b4;
00052 unsigned char *ptr;
00053
00054 ptr = (unsigned char *)array;
00055 while (length--) {
00056 b1 = *ptr++;
00057 b2 = *ptr++;
00058 b3 = *ptr++;
00059 b4 = *ptr++;
00060 *array++ = (b1 << 24) | (b2 << 16) | (b3 << 8) | (b4);
00061 }
00062 }
00063
00064 ImageRec *wxRGBHandler::ImageOpen(wxBufferedInputStream &buf_stream)
00065 {
00066 union {
00067 int testWord;
00068 char testByte[4];
00069 } endianTest;
00070 ImageRec *img;
00071 int swapFlag;
00072 int x;
00073
00074 endianTest.testWord = 1;
00075 if (endianTest.testByte[0] == 1) {
00076 swapFlag = 1;
00077 } else {
00078 swapFlag = 0;
00079 }
00080
00081
00082 img = (ImageRec *)malloc(sizeof(ImageRec));
00083 if (img == NULL) {
00084 LOGERROR(_("RGB: Out of memory."));
00085 return NULL;
00086 }
00087 buf_stream.Read(img, 12);
00088 if (swapFlag) {
00089 ConvertShort(&img->imagic, 6);
00090 }
00091 img->tmp = (unsigned char *)malloc(img->xsize*256);
00092 img->tmpR = (unsigned char *)malloc(img->xsize*256);
00093 img->tmpG = (unsigned char *)malloc(img->xsize*256);
00094 img->tmpB = (unsigned char *)malloc(img->xsize*256);
00095 if (img->tmp == NULL || img->tmpR == NULL || img->tmpG == NULL || img->tmpB == NULL) {
00096 LOGERROR(_("RGB: Out of memory."));
00097 return NULL;
00098 }
00099
00100 if ((img->type & 0xFF00) == 0x0100) {
00101 x = img->ysize * img->zsize * sizeof(unsigned);
00102 img->rowStart = (unsigned *)malloc(x);
00103 img->rowSize = (int *)malloc(x);
00104 if (img->rowStart == NULL || img->rowSize == NULL) {
00105 LOGERROR(_("RGB: Out of memory."));
00106 return NULL;
00107 }
00108 img->rleEnd = 512 + (2 * x);
00109 buf_stream.SeekI(512);
00110 buf_stream.Read(img->rowStart, x);
00111 buf_stream.Read(img->rowSize, x);
00112 if (swapFlag) {
00113 ConvertLong(img->rowStart, x/(int)sizeof(unsigned));
00114 ConvertLong((unsigned *)img->rowSize, x/(int)sizeof(int));
00115 }
00116 } else {
00117 img->rowStart = NULL;
00118 img->rowSize = NULL;
00119 }
00120 return img;
00121 }
00122
00123 void wxRGBHandler::ImageClose(ImageRec *img)
00124 {
00125 free(img->tmp);
00126 free(img->tmpR);
00127 free(img->tmpG);
00128 free(img->tmpB);
00129 free(img->rowSize);
00130 free(img->rowStart);
00131 free(img);
00132 }
00133
00134 void wxRGBHandler::ImageGetRow(wxBufferedInputStream &buf_stream, ImageRec *img, unsigned char *buf, int y, int z)
00135 {
00136 unsigned char *iPtr, *oPtr, pixel;
00137 int count;
00138
00139 if ((img->type & 0xFF00) == 0x0100) {
00140 buf_stream.SeekI((long)img->rowStart[y+z*img->ysize]);
00141 buf_stream.Read(img->tmp, (unsigned int)img->rowSize[y+z*img->ysize]);
00142
00143 iPtr = img->tmp;
00144 oPtr = buf;
00145 for (;;) {
00146 pixel = *iPtr++;
00147 count = (int)(pixel & 0x7F);
00148 if (!count) {
00149 return;
00150 }
00151 if (pixel & 0x80) {
00152 while (count--) {
00153 *oPtr++ = *iPtr++;
00154 }
00155 } else {
00156 pixel = *iPtr++;
00157 while (count--) {
00158 *oPtr++ = pixel;
00159 }
00160 }
00161 }
00162 } else {
00163 buf_stream.SeekI(512+(y*img->xsize)+(z*img->xsize*img->ysize));
00164 buf_stream.Read(buf, img->xsize);
00165 }
00166 }
00167
00168 #if wxUSE_STREAMS
00169 bool wxRGBHandler::LoadFile( wxImage *image, wxInputStream& stream, bool WXUNUSED(verbose), int WXUNUSED(index) )
00170 {
00171 image->Destroy();
00172 wxBufferedInputStream buf_stream(stream);
00173
00174 unsigned char *rbuf, *gbuf, *bbuf, *abuf;
00175 ImageRec *img;
00176 int y;
00177
00178 img = ImageOpen(buf_stream);
00179
00180 if(!img)
00181 return false;
00182 image->Create( img->xsize, img->ysize );
00183 if (((img->zsize>=4)||(img->zsize==2)) && (!image->HasAlpha()))
00184 image->InitAlpha();
00185 rbuf = (unsigned char *)malloc(img->xsize*sizeof(unsigned char));
00186 gbuf = (unsigned char *)malloc(img->xsize*sizeof(unsigned char));
00187 bbuf = (unsigned char *)malloc(img->xsize*sizeof(unsigned char));
00188 abuf = (unsigned char *)malloc(img->xsize*sizeof(unsigned char));
00189 if(!rbuf || !gbuf || !bbuf)
00190 return false;
00191 for (y=0; y<img->ysize; y++) {
00192 if (img->zsize>=4) {
00193 ImageGetRow(buf_stream, img,rbuf,y,0);
00194 ImageGetRow(buf_stream, img,gbuf,y,1);
00195 ImageGetRow(buf_stream, img,bbuf,y,2);
00196 ImageGetRow(buf_stream, img,abuf,y,3);
00197 for (int x=0; x<img->xsize; x++)
00198 {
00199 image->SetRGB(x, img->ysize-y-1, rbuf[x], gbuf[x], bbuf[x]);
00200 image->SetAlpha(x, img->ysize-y-1, abuf[x]);
00201 }
00202 } else if(img->zsize==3) {
00203 ImageGetRow(buf_stream, img,rbuf,y,0);
00204 ImageGetRow(buf_stream, img,gbuf,y,1);
00205 ImageGetRow(buf_stream, img,bbuf,y,2);
00206 for (int x=0; x<img->xsize; x++)
00207 {
00208 image->SetRGB(x, img->ysize-y-1, rbuf[x], gbuf[x], bbuf[x]);
00209 }
00210 } else if(img->zsize==2) {
00211 ImageGetRow(buf_stream, img,rbuf,y,0);
00212 ImageGetRow(buf_stream, img,abuf,y,1);
00213 for (int x=0; x<img->xsize; x++)
00214 {
00215 image->SetRGB(x, img->ysize-y-1, rbuf[x], rbuf[x], rbuf[x]);
00216 image->SetAlpha(x, img->ysize-y-1, abuf[x]);
00217 }
00218 } else {
00219 ImageGetRow(buf_stream, img,rbuf,y,0);
00220 for (int x=0; x<img->xsize; x++)
00221 {
00222 image->SetRGB(x, img->ysize-y-1, rbuf[x], rbuf[x], rbuf[x]);
00223 }
00224 }
00225 }
00226 ImageClose(img);
00227 free(rbuf);
00228 free(gbuf);
00229 free(bbuf);
00230 free(abuf);
00231
00232
00233 return true;
00234 }
00235
00236 bool wxRGBHandler::SaveFile( wxImage * WXUNUSED(image), wxOutputStream & WXUNUSED(stream), bool WXUNUSED(verbose) )
00237 {
00238 return false;
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248 }
00249
00250 bool wxRGBHandler::DoCanRead( wxInputStream& stream )
00251 {
00252 if ( (stream.GetC() == 1) && (stream.GetC() == 218))
00253 return true;
00254 return false;
00255 }
00256
00257 #endif // wxUSE_STREAMS
00258
00259