Skip to content

Commit d04f9f0

Browse files
committed
eifviewer [fix] conversion form BMP to 16bit EIFs
Not all EIF contains same color palette, so the -s param has been added. It's allow specify source EIF palette. Also 16bit EIFs palettes are now extracted and has .pal prefix.
1 parent 9715e6b commit d04f9f0

File tree

4 files changed

+66
-97
lines changed

4 files changed

+66
-97
lines changed

EifViewer/EifConverter.cpp

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
//
44

55
#include "EifConverter.h"
6-
#include "FordPalette.h"
76
#include <fstream>
87
#include <limits>
8+
#include <utils.h>
99

1010
using 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

231228
int 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+
325346
int 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;

EifViewer/EifConverter.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,19 @@ class EifImage8bit: public EifImageBase {
4747
int openEif(const std::vector<uint8_t>& data) override;
4848
void saveBmp(std::string file_name) override;
4949
int openBmp(std::string file_name) override;
50-
void saveEif(std::string file_name)override;
50+
void saveEif(std::string file_name) override;
5151
};
5252

5353
class EifImage16bit: public EifImageBase {
5454
std::vector<uint8_t> palette;
55-
static uint8_t searchPixel(RGBApixel rgb_pixel);
55+
uint8_t searchPixel(RGBApixel rgb_pixel);
5656
public:
5757
int openEif(const std::vector<uint8_t>& data) override;
5858
void saveBmp(std::string file_name) override;
5959
int openBmp(std::string file_name) override;
60-
void saveEif(std::string file_name)override;
60+
void saveEif(std::string file_name) override;
61+
int setPalette(const std::vector<uint8_t>& data);
62+
void savePalette(const std::string& file_name);
6163
};
6264

6365
class EifImage32bit: public EifImageBase {
@@ -72,7 +74,8 @@ class EifConverter {
7274

7375
public:
7476
static void eifToBmpFile(const std::vector<uint8_t>& data, const std::string& out_file_name);
75-
static void bmpFileToEifFile(const std::string& file_name, uint8_t depth, const std::string& out_file_name);
77+
static void bmpFileToEifFile(const std::string& file_name, uint8_t depth, const std::string& out_file_name,
78+
const std::string& palette_file_name = "");
7679
};
7780

7881
}

EifViewer/FordPalette.h

Lines changed: 0 additions & 74 deletions
This file was deleted.

EifViewer/main.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ int main(int argc, char **argv)
2525
("d,depth","Output Eif type 8/16/32", cxxopts::value<unsigned>(depth))
2626
("i,input","Input file", cxxopts::value<string>())
2727
("o,output","Output file", cxxopts::value<string>())
28+
("s,scheme","Color scheme file", cxxopts::value<string>())
2829
("h,help","Print help");
2930

3031
options.parse_positional({"input"});
@@ -67,7 +68,16 @@ int main(int argc, char **argv)
6768
return 0;
6869
}
6970

70-
EIF::EifConverter::bmpFileToEifFile(input_file_name, depth, out_file_name);
71+
if(depth == 16) {
72+
if(!result.count("scheme")){
73+
cout << "Please, specify color scheme file" << std::endl;
74+
return 0;
75+
}
76+
EIF::EifConverter::bmpFileToEifFile(input_file_name, depth, out_file_name,
77+
result["scheme"].as<string>());
78+
} else {
79+
EIF::EifConverter::bmpFileToEifFile(input_file_name, depth, out_file_name);
80+
}
7181
}
7282
} catch (const cxxopts::OptionException& e){
7383
cout << "error parsing options: " << e.what() << endl;

0 commit comments

Comments
 (0)