gifFile: add warning prototype for missing func
[rofl0r-obeditor.git] / src / images__gifFile.cpp
blobb816de79c45826d238892361555c66b63a1b40e9
1 /*
2 * gifFile.cpp
4 * Created on: 12 nov. 2008
5 * Author: pat
6 */
7 #include "wx/wxprec.h"
8 #ifndef WX_PRECOMP
9 #include "wx/wx.h"
10 #endif
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <fcntl.h>
16 #include <gif_lib.h>
17 #include <wx/image.h>
18 #include <wx/palette.h>
20 #include "images__gifFile.h"
21 #include "images__pngFile.h"
22 #include "images__MyPalette.h"
24 using namespace std;
26 //---------------------------------------------------------------
27 imgFile*
28 imgFile::LoadImage( const wxString& img_path )
30 wxString ext = img_path.Right(3).Upper();
31 imgFile* res = NULL;
32 if( ext == wxT("GIF") )
33 res = new wxIndexedGIF( img_path );
34 else if( ext == wxT("PNG") )
35 res = new wxIndexedPNG( img_path );
37 if( res == NULL )
38 return res;
39 if( res->IsOk() == false )
41 delete res;
42 return NULL;
44 return res;
49 //*************************************************
51 wxIndexedGIF::wxIndexedGIF()
53 Init();
57 //*************************************************
59 void wxIndexedGIF::Init()
61 height = width = 0;
62 SWidth = SHeight = SColorResolution = SBackGroundColor = 0;
63 RasterBits = NULL;
64 SavedImage = NULL;
65 str_fn =wxString();
66 type = pIMG_GIF;
70 //*************************************************
72 wxIndexedGIF::~wxIndexedGIF()
74 Reset();
78 //*************************************************
80 void wxIndexedGIF::Reset()
82 height = width = 0;
83 if( RasterBits != NULL )
84 delete RasterBits;
85 RasterBits = NULL ;
87 if( SavedImage != NULL )
89 delete[] SavedImage->ImageDesc.ColorMap->Colors;
90 delete SavedImage->ImageDesc.ColorMap;
91 delete SavedImage;
92 SavedImage = NULL;
94 SWidth = SHeight = SColorResolution = SBackGroundColor = 0;
95 str_fn = wxString();
99 //*************************************************
101 bool wxIndexedGIF::IsOk()
103 return ! ( height == 0 || width == 0 || SavedImage == NULL );
107 //*************************************************
109 wxIndexedGIF::wxIndexedGIF( const wxString& str_GIFFile )
111 Init();
112 LoadFrom( str_GIFFile );
116 //*************************************************
118 bool wxIndexedGIF::LoadFrom( const wxString& str_GIFFile )
120 Reset();
121 return ReadGIF( str_GIFFile ) == 0;
125 //*************************************************
127 int wxIndexedGIF::GetIndex( const int x, const int y)
129 if( !IsOk() || x >= width || y >= height )
130 return -1;
132 return RasterBits[y*width+x];
136 //*************************************************
138 MyPalette* wxIndexedGIF::GetPalette()
140 MyPalette* res = NULL;
141 if( !IsOk() )
142 return res;
144 // Setting the palette
145 if( SavedImage != NULL )
147 int nb_colour = SavedImage->ImageDesc.ColorMap->ColorCount;
148 unsigned char r[nb_colour], g[nb_colour], b[nb_colour];
149 for( int i = 0; i < nb_colour; i++ )
151 r[i] = SavedImage->ImageDesc.ColorMap->Colors[i].Red;
152 g[i] = SavedImage->ImageDesc.ColorMap->Colors[i].Green;
153 b[i] = SavedImage->ImageDesc.ColorMap->Colors[i].Blue;
155 res = new MyPalette();
156 res->Create( nb_colour, r, g, b );
157 return res;
160 return res;
163 //*************************************************
165 bool wxIndexedGIF::SetPalette( wxPalette _pal )
167 int ColorCount = _pal.GetColoursCount();
168 if( !IsOk() || !_pal.IsOk() || ColorCount <= 0 || ColorCount > 256 )
169 return false;
171 // Fill the palette of the gif
172 unsigned char r,g,b;
173 GifColorType *ColorMap = new GifColorType[ColorCount];
174 for( int i = 0; i < _pal.GetColoursCount(); i ++)
176 _pal.GetRGB( i, &r,&g,&b );
177 ColorMap[i].Red = r;
178 ColorMap[i].Green = g;
179 ColorMap[i].Blue = b;
182 // Fill remaining palette with black
183 for( int i = _pal.GetColoursCount(); i < ColorCount; i ++)
185 ColorMap[i].Red = 0;
186 ColorMap[i].Green = 0;
187 ColorMap[i].Blue = 0;
190 // Replace old palette object in gif struct
191 delete[] SavedImage->ImageDesc.ColorMap->Colors;
192 SavedImage->ImageDesc.ColorMap->Colors = ColorMap;
193 SavedImage->ImageDesc.ColorMap->ColorCount = _pal.GetColoursCount();
195 return true;
199 //*************************************************
201 bool wxIndexedGIF::Remap( unsigned char *remap, int sz_remap )
203 if( ! IsOk() )
204 return false;
206 for( int i = 0; i < height * width; i++)
208 // Out of bound, remap too small
209 if( (int) RasterBits[i] >= sz_remap )
210 return false;
212 RasterBits[i] = remap[ RasterBits[i] ];
214 return true;
218 //*************************************************
220 unsigned char * wxIndexedGIF::GetDatas( int& datas_size )
222 if( ! IsOk() )
224 datas_size = 0;
225 return NULL;
228 datas_size = height * width;
229 unsigned char *res = new unsigned char[ datas_size ];
230 for( int i =0; i < datas_size; i++ )
231 res[i] = RasterBits[i];
233 return res;
237 //*************************************************
239 void wxIndexedGIF::SetDatas( unsigned char *datas, int datas_size )
241 if( ! IsOk() || datas == NULL || datas_size == 0 )
242 return;
244 delete[] RasterBits;
245 RasterBits = datas;
246 SavedImage->RasterBits = RasterBits;
250 //*************************************************
252 bool wxIndexedGIF::SetPixel( int x, int y, int ind )
254 if( ! IsOk() || x > width || y > height )
255 return false;
257 RasterBits[x + y*width] = ind;
258 return true;
262 //*************************************************
264 bool wxIndexedGIF::SaveAs( const wxString& str_GIFFile )
266 return WriteGIF( str_GIFFile ) == 0;
270 //***************************************************
272 struct ColorMapObject* Duplicate_ColorMap( struct ColorMapObject* cm )
274 ColorMapObject *res = new ColorMapObject;
275 res->ColorCount = cm->ColorCount;
276 res->BitsPerPixel = cm->BitsPerPixel;
277 if( res->ColorCount == 0 )
278 res->Colors = NULL;
279 else
281 res->Colors = new GifColorType[res->ColorCount];
282 for( int i = 0; i < res->ColorCount; i++ )
283 res->Colors[i] = cm->Colors[i];
285 return res;
289 //***************************************************
290 struct SavedImage* Duplicate_SavedImage( struct SavedImage* si )
293 SavedImage* res = (SavedImage*) malloc( sizeof(SavedImage));
294 res->ExtensionBlockCount = 0;
295 res->ExtensionBlocks = NULL;
296 res->ImageDesc.Left = si->ImageDesc.Left;
297 res->ImageDesc.Top = si->ImageDesc.Top;
298 res->ImageDesc.Width = si->ImageDesc.Width;
299 res->ImageDesc.Height = si->ImageDesc.Height;
300 res->ImageDesc.Interlace = si->ImageDesc.Interlace;
301 if( si->ImageDesc.ColorMap == NULL )
302 res->ImageDesc.ColorMap = NULL;
303 else
305 ColorMapObject *SColorMap = (ColorMapObject*)
306 malloc( sizeof(ColorMapObject));
307 SColorMap->ColorCount = si->ImageDesc.ColorMap->ColorCount;
308 SColorMap->BitsPerPixel = si->ImageDesc.ColorMap->BitsPerPixel;
309 if( SColorMap->ColorCount == 0 )
310 SColorMap->Colors = NULL;
311 else
313 SColorMap->Colors = (GifColorType*)
314 malloc( sizeof(GifColorType) * SColorMap->ColorCount );
315 for( int i = 0; i < SColorMap->ColorCount; i++ )
316 SColorMap->Colors[i] = si->ImageDesc.ColorMap->Colors[i];
318 res->ImageDesc.ColorMap = Duplicate_ColorMap( si->ImageDesc.ColorMap );
321 if( si->RasterBits == NULL || res->ImageDesc.Width == 0 || res->ImageDesc.Height == 0 )
322 res->RasterBits = NULL;
323 else
325 int nb_pix = res->ImageDesc.Width * res->ImageDesc.Height;
326 res->RasterBits = (unsigned char*) malloc(nb_pix);
327 for (int i = 0; i < nb_pix; i++ )
328 res->RasterBits[i] = si->RasterBits[i];
331 return res;
334 //*************************************************
336 int wxIndexedGIF::ReadGIF( const wxString& _str_fn)
338 str_fn = _str_fn;
341 // Try to open source file
342 int gif_err = 0;
343 GifFileType *GifFileIn = DGifOpenFileName( (char*)_str_fn.c_str(), &gif_err);
345 // Load the gif
346 bool b_err = true;
347 if( GifFileIn != NULL )
349 int res = DGifSlurp(GifFileIn);
350 if( res == GIF_OK||gif_err == 0 /*|| gif_err == E_GIF_ERR_DATA_TOO_BIG */)
351 b_err = (GifFileIn->ImageCount <= 0 );
354 if( b_err )
356 if( GifFileIn != NULL )
357 DGifCloseFile(GifFileIn, &gif_err);
358 GifFileIn = NULL;
359 return 1;
361 Reset();
363 height = GifFileIn->Image.Height;
364 width = GifFileIn->Image.Width;
366 SWidth = GifFileIn->SWidth;
367 SHeight = GifFileIn->SHeight;
368 SColorResolution = GifFileIn->SColorResolution;
369 SBackGroundColor = GifFileIn->SBackGroundColor;
371 SavedImage = Duplicate_SavedImage( &GifFileIn->SavedImages[0] );
372 if( SavedImage->ImageDesc.ColorMap == NULL )
373 SavedImage->ImageDesc.ColorMap = Duplicate_ColorMap( GifFileIn->SColorMap );
375 if( SavedImage->ImageDesc.ColorMap == NULL )
377 Reset();
378 return 1;
381 RasterBits = SavedImage->RasterBits;
383 DGifCloseFile(GifFileIn, &gif_err);
384 GifFileIn = NULL;
386 return 0;
390 void MakeSavedImage(GifFileType *f, struct SavedImage *si) {
391 std::cerr << "warning: MakeSavedImage not implemented yet\n";
394 //*************************************************
396 int wxIndexedGIF::WriteGIF( const wxString& str_dest )
398 // Verificatin on opened gif
399 if( ! IsOk() )
400 return 1;
402 // Don't permit source == destinatino
403 if( str_fn == str_dest )
404 return 1;
406 // Try to open dest file
407 int gif_err = 0;
408 GifFileType *GifFileOut = EGifOpenFileName((char*) str_dest.c_str(), false, &gif_err);
409 if( GifFileOut == NULL )
410 return 1;
412 // Copy data from the source file
413 GifFileOut->SWidth = SWidth;
414 GifFileOut->SHeight = SHeight;
415 GifFileOut->SColorResolution = SColorResolution;
416 GifFileOut->SBackGroundColor = SBackGroundColor;
417 GifFileOut->SColorMap = NULL;
419 struct SavedImage *si = Duplicate_SavedImage( SavedImage );
420 MakeSavedImage(GifFileOut, si );
422 int res = 0;
423 if (EGifSpew(GifFileOut) == GIF_ERROR)
424 res = 1;
426 EGifCloseFile( GifFileOut, &gif_err );
427 return res;