4 * Created on: 12 nov. 2008
12 #include <sys/types.h>
18 #include <wx/palette.h>
20 #include "images__gifFile.h"
21 #include "images__pngFile.h"
22 #include "images__MyPalette.h"
26 //---------------------------------------------------------------
28 imgFile::LoadImage( const wxString
& img_path
)
30 wxString ext
= img_path
.Right(3).Upper();
32 if( ext
== wxT("GIF") )
33 res
= new wxIndexedGIF( img_path
);
34 else if( ext
== wxT("PNG") )
35 res
= new wxIndexedPNG( img_path
);
39 if( res
->IsOk() == false )
49 //*************************************************
51 wxIndexedGIF::wxIndexedGIF()
57 //*************************************************
59 void wxIndexedGIF::Init()
62 SWidth
= SHeight
= SColorResolution
= SBackGroundColor
= 0;
70 //*************************************************
72 wxIndexedGIF::~wxIndexedGIF()
78 //*************************************************
80 void wxIndexedGIF::Reset()
83 if( RasterBits
!= NULL
)
87 if( SavedImage
!= NULL
)
89 delete[] SavedImage
->ImageDesc
.ColorMap
->Colors
;
90 delete SavedImage
->ImageDesc
.ColorMap
;
94 SWidth
= SHeight
= SColorResolution
= SBackGroundColor
= 0;
99 //*************************************************
101 bool wxIndexedGIF::IsOk()
103 return ! ( height
== 0 || width
== 0 || SavedImage
== NULL
);
107 //*************************************************
109 wxIndexedGIF::wxIndexedGIF( const wxString
& str_GIFFile
)
112 LoadFrom( str_GIFFile
);
116 //*************************************************
118 bool wxIndexedGIF::LoadFrom( const wxString
& str_GIFFile
)
121 return ReadGIF( str_GIFFile
) == 0;
125 //*************************************************
127 int wxIndexedGIF::GetIndex( const int x
, const int y
)
129 if( !IsOk() || x
>= width
|| y
>= height
)
132 return RasterBits
[y
*width
+x
];
136 //*************************************************
138 MyPalette
* wxIndexedGIF::GetPalette()
140 MyPalette
* res
= NULL
;
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
);
163 //*************************************************
165 bool wxIndexedGIF::SetPalette( wxPalette _pal
)
167 int ColorCount
= _pal
.GetColoursCount();
168 if( !IsOk() || !_pal
.IsOk() || ColorCount
<= 0 || ColorCount
> 256 )
171 // Fill the palette of the gif
173 GifColorType
*ColorMap
= new GifColorType
[ColorCount
];
174 for( int i
= 0; i
< _pal
.GetColoursCount(); i
++)
176 _pal
.GetRGB( i
, &r
,&g
,&b
);
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
++)
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();
199 //*************************************************
201 bool wxIndexedGIF::Remap( unsigned char *remap
, int sz_remap
)
206 for( int i
= 0; i
< height
* width
; i
++)
208 // Out of bound, remap too small
209 if( (int) RasterBits
[i
] >= sz_remap
)
212 RasterBits
[i
] = remap
[ RasterBits
[i
] ];
218 //*************************************************
220 unsigned char * wxIndexedGIF::GetDatas( int& datas_size
)
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
];
237 //*************************************************
239 void wxIndexedGIF::SetDatas( unsigned char *datas
, int datas_size
)
241 if( ! IsOk() || datas
== NULL
|| datas_size
== 0 )
246 SavedImage
->RasterBits
= RasterBits
;
250 //*************************************************
252 bool wxIndexedGIF::SetPixel( int x
, int y
, int ind
)
254 if( ! IsOk() || x
> width
|| y
> height
)
257 RasterBits
[x
+ y
*width
] = ind
;
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 )
281 res
->Colors
= new GifColorType
[res
->ColorCount
];
282 for( int i
= 0; i
< res
->ColorCount
; i
++ )
283 res
->Colors
[i
] = cm
->Colors
[i
];
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
;
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
;
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
;
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
];
334 //*************************************************
336 int wxIndexedGIF::ReadGIF( const wxString
& _str_fn
)
341 // Try to open source file
343 GifFileType
*GifFileIn
= DGifOpenFileName( (char*)_str_fn
.c_str(), &gif_err
);
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 );
356 if( GifFileIn
!= NULL
)
357 DGifCloseFile(GifFileIn
, &gif_err
);
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
)
381 RasterBits
= SavedImage
->RasterBits
;
383 DGifCloseFile(GifFileIn
, &gif_err
);
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
402 // Don't permit source == destinatino
403 if( str_fn
== str_dest
)
406 // Try to open dest file
408 GifFileType
*GifFileOut
= EGifOpenFileName((char*) str_dest
.c_str(), false, &gif_err
);
409 if( GifFileOut
== NULL
)
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
);
423 if (EGifSpew(GifFileOut
) == GIF_ERROR
)
426 EGifCloseFile( GifFileOut
, &gif_err
);