33//
44
55#include " EifConverter.h"
6- #include " FordPalette.h"
76#include < fstream>
87#include < limits>
8+ #include < utils.h>
99
1010using namespace EIF ;
1111
@@ -18,9 +18,6 @@ int EifImage8bit::openBmp(std::string file_name) {
1818 width = (unsigned )bmp_image.TellWidth ();
1919 height = (unsigned )bmp_image.TellHeight ();
2020
21- std::cout << " Open pic: " << file_name << " Bit depth: "
22- << depth << " W: " << width <<" H: " << height;
23-
2421 auto aligned_width = (width % 4 ) ? (width/4 + 1 )*4 : width;
2522 bitmap_data.clear ();
2623 bitmap_data.resize (height * aligned_width);
@@ -206,10 +203,10 @@ uint8_t EifImage16bit::searchPixel(RGBApixel rgb_pixel) {
206203 double color_distance = std::numeric_limits<double >::max ();
207204 uint8_t closest_index =0 ;
208205
209- for (auto i=0 ; i < palette_bin_len ; i+=3 ) {
210- int R = palette_bin [i];
211- int G = palette_bin [i+1 ];
212- int B = palette_bin [i+2 ];
206+ for (auto i=0 ; i < EIF_MULTICOLOR_PALETTE_SIZE ; i+=3 ) {
207+ int R = palette [i];
208+ int G = palette [i+1 ];
209+ int B = palette [i+2 ];
213210
214211 double color_distance_new = sqrt (
215212 pow ((rgb_pixel.Blue - B),2 ) +
@@ -230,16 +227,18 @@ uint8_t EifImage16bit::searchPixel(RGBApixel rgb_pixel) {
230227
231228int EifImage16bit::openBmp (std::string file_name) {
232229
230+ if (palette.empty ()) {
231+ std::cerr << " Can't open bmp file: " << file_name << " Palette required" << std::endl;
232+ return 0 ;
233+ }
234+
233235 BMP bmp_image;
234236 bmp_image.ReadFromFile (file_name.c_str ());
235237
236238 auto depth = bmp_image.TellBitDepth ();
237239 width = (unsigned )bmp_image.TellWidth ();
238240 height = (unsigned )bmp_image.TellHeight ();
239241
240- std::cout << " Open pic: " << file_name << " Bit depth: "
241- << depth << " W: " << width <<" H: " << height;
242-
243242 auto aligned_width = (width % 2 ) ? (width/2 + 1 )*2 : width;
244243 bitmap_data.clear ();
245244 bitmap_data.resize (height * aligned_width * 2 );
@@ -283,8 +282,8 @@ void EifImage16bit::saveEif(std::string file_name) {
283282 header.length = bitmap_data.size ();
284283
285284 /* set palette */
286- for (int i=0 ; i < palette_bin_len ; i++) {
287- eif_img.push_back (palette_bin [i]);
285+ for (int i=0 ; i < EIF_MULTICOLOR_PALETTE_SIZE ; i++) {
286+ eif_img.push_back (palette [i]);
288287 }
289288
290289 /* set pixels */
@@ -322,6 +321,28 @@ void EifImage16bit::saveBmp(std::string file_name) {
322321 bmp_image.WriteToFile (file_name.c_str ());
323322}
324323
324+ int EifImage16bit::setPalette (const std::vector<uint8_t > &data) {
325+
326+ if (data.size () != EIF_MULTICOLOR_PALETTE_SIZE) {
327+ std::cerr << " Error: palette wrong size" << std::endl;
328+ return -1 ;
329+ }
330+
331+ palette = data;
332+
333+ return 0 ;
334+ }
335+
336+ void EifImage16bit::savePalette (const std::string& file_name) {
337+
338+ if (palette.size () != EIF_MULTICOLOR_PALETTE_SIZE) {
339+ std::cerr << " Error: palette wrong size" << std::endl;
340+ return ;
341+ }
342+
343+ FTUtils::bufferToFile (file_name, (char *)palette.data (), EIF_MULTICOLOR_PALETTE_SIZE);
344+ }
345+
325346int EifImage32bit::openEif (const std::vector<uint8_t > &data) {
326347
327348 if (data.size () < sizeof (EifBaseHeader)){
@@ -402,9 +423,6 @@ int EifImage32bit::openBmp(std::string file_name) {
402423 width = (unsigned )bmp_image.TellWidth ();
403424 height = (unsigned )bmp_image.TellHeight ();
404425
405- std::cout << " Open pic: " << file_name << " Bit depth: "
406- << depth << " W: " << width <<" H: " << height;
407-
408426 bitmap_data.clear ();
409427 bitmap_data.resize (height * width * 4 );
410428
@@ -474,19 +492,31 @@ void EifConverter::eifToBmpFile(const std::vector<uint8_t>& data, const std::str
474492
475493 image->openEif (data);
476494 image->saveBmp (out_file_name);
495+ if (data[7 ] == EIF_TYPE_MULTICOLOR) {
496+ dynamic_cast <EifImage16bit*>(image)->savePalette (out_file_name+" .pal" );
497+ }
477498
478499 delete image;
479500}
480501
481- void EifConverter::bmpFileToEifFile (const std::string& file_name, uint8_t depth, const std::string& out_file_name) {
502+ void EifConverter::bmpFileToEifFile (const std::string& file_name, uint8_t depth, const std::string& out_file_name,
503+ const std::string& palette_file_name)
504+ {
482505
483506 EifImageBase* image;
484507 switch (depth) {
485508 case 8 :
486509 image = new EifImage8bit;
487510 break ;
488- case 16 :
511+ case 16 : {
489512 image = new EifImage16bit;
513+ if (palette_file_name.empty ()) {
514+ throw std::runtime_error (" Incorrect palette path" );
515+ }
516+ std::vector<uint8_t > palette;
517+ FTUtils::fileToVector (palette_file_name, palette);
518+ dynamic_cast <EifImage16bit*>(image)->setPalette (palette);
519+ }
490520 break ;
491521 case 32 :
492522 image = new EifImage32bit;
0 commit comments