Skip to content

Commit 6c35f19

Browse files
committed
eifviewer [add] convert functions
[ref] move code to eif lib
1 parent 20c4482 commit 6c35f19

File tree

4 files changed

+75
-57
lines changed

4 files changed

+75
-57
lines changed

EifViewer/CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ cmake_minimum_required(VERSION 3.10)
44

55
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/bin")
66

7-
add_executable(eifviewer EifImage.cpp imgViewer.cpp ./EasyBMP/EasyBMP.cpp)
7+
add_library(eif EifConverter.cpp ./EasyBMP/EasyBMP.cpp)
8+
add_executable(eifviewer main.cpp)
9+
target_link_libraries(eifviewer eif)
810
include_directories(../cxxopts/include)
911
include_directories(../CRCpp/inc)
12+
include_directories(../utils)
1013
include_directories(./EasyBMP)
1114

1215
install(TARGETS eifviewer)
Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
// Created by bigun on 12/28/2018.
33
//
44

5-
#include "EifImage.h"
5+
#include "EifConverter.h"
66
#include "FordPalette.h"
77
#include <fstream>
88
#include <limits>
99

10+
using namespace EIF;
11+
1012
int EifImage8bit::openBmp(std::string file_name) {
1113

1214
BMP bmp_image;
@@ -77,7 +79,7 @@ void EifImage8bit::saveEif(std::string file_name) {
7779
void EifImage8bit::saveBmp(std::string file_name){
7880

7981
BMP bmp_image;
80-
bmp_image.SetSize(width, height);
82+
bmp_image.SetSize((int)width, (int)height);
8183

8284
for(auto i =0; i < height; i++){
8385
for(auto j=0; j < width; j++){
@@ -301,7 +303,7 @@ void EifImage16bit::saveBmp(std::string file_name) {
301303

302304
BMP bmp_image;
303305
bmp_image.SetBitDepth(32);
304-
bmp_image.SetSize(width, height);
306+
bmp_image.SetSize((int)width, (int)height);
305307

306308
for(auto i =0; i < height; i++){
307309
for(auto j=0; j < width; j++){
@@ -373,7 +375,7 @@ void EifImage32bit::saveBmp(std::string file_name) {
373375

374376
BMP bmp_image;
375377
bmp_image.SetBitDepth(32);
376-
bmp_image.SetSize(width, height);
378+
bmp_image.SetSize((int)width, (int)height);
377379

378380
for(auto i =0; i < height; i++){
379381
for(auto j=0; j < width; j++){
@@ -455,3 +457,46 @@ void EifImage32bit::saveEif(std::string file_name) {
455457
}
456458
out_file.close();
457459
}
460+
461+
void EifConverter::eifToBmpFile(const std::vector<uint8_t>& data, const std::string& out_file_name) {
462+
463+
EifImageBase* image;
464+
465+
if(data[7] == EIF_TYPE_MONOCHROME) {
466+
image = new EifImage8bit;
467+
} else if(data[7] == EIF_TYPE_MULTICOLOR) {
468+
image = new EifImage16bit;
469+
} else if(data[7] == EIF_TYPE_SUPERCOLOR){
470+
image = new EifImage32bit;
471+
} else {
472+
throw std::runtime_error("Unsupported EIF format");
473+
}
474+
475+
image->openEif(data);
476+
image->saveBmp(out_file_name);
477+
478+
delete image;
479+
}
480+
481+
void EifConverter::bmpFileToEifFile(const std::string& file_name, uint8_t depth, const std::string& out_file_name) {
482+
483+
EifImageBase* image;
484+
switch (depth) {
485+
case 8:
486+
image = new EifImage8bit;
487+
break;
488+
case 16:
489+
image = new EifImage16bit;
490+
break;
491+
case 32:
492+
image = new EifImage32bit;
493+
break;
494+
default:
495+
throw std::runtime_error("Incorrect depth value");
496+
}
497+
498+
image->openBmp(file_name);
499+
image->saveEif(out_file_name);
500+
501+
delete image;
502+
}
Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#ifndef VBFEDIT_EIFIMAGE_H
1414
#define VBFEDIT_EIFIMAGE_H
1515

16+
namespace EIF {
17+
1618
static const uint8_t EIF_SIGNATURE[] = {'E','B','D',0x10,'E','I','F'};
1719
static const uint8_t EIF_TYPE_MONOCHROME = 0x04;
1820
static const uint8_t EIF_TYPE_MULTICOLOR = 0x07;
@@ -37,7 +39,7 @@ class EifImageBase {
3739
virtual int openBmp(std::string fileName) = 0;
3840
virtual void saveBmp(std::string fileName) = 0;
3941
virtual void saveEif(std::string fileName) = 0;
40-
virtual ~EifImageBase(){};
42+
virtual ~EifImageBase()= default;
4143
};
4244

4345
class EifImage8bit: public EifImageBase {
@@ -50,7 +52,7 @@ class EifImage8bit: public EifImageBase {
5052

5153
class EifImage16bit: public EifImageBase {
5254
std::vector<uint8_t> palette;
53-
uint8_t searchPixel(RGBApixel rgb_pixel);
55+
static uint8_t searchPixel(RGBApixel rgb_pixel);
5456
public:
5557
int openEif(const std::vector<uint8_t>& data) override;
5658
void saveBmp(std::string file_name) override;
@@ -66,5 +68,13 @@ class EifImage32bit: public EifImageBase {
6668
void saveEif(std::string file_name) override;
6769
};
6870

71+
class EifConverter {
72+
73+
public:
74+
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);
76+
};
77+
78+
}
6979
#endif //VBFEDIT_EIFIMAGE_H
7080

Lines changed: 10 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22
// Created by bigun on 12/23/2018.
33
//
44
#include <iostream>
5-
#include <fstream>
65
#include <vector>
7-
#include <iomanip>
86
#include <cxxopts.hpp>
9-
#include <exception>
7+
#include <utils.h>
108

11-
#include "EifImage.h"
9+
#include "EifConverter.h"
1210

1311
using namespace std;
1412

@@ -20,7 +18,7 @@ int main(int argc, char **argv)
2018
bool unpack = false;
2119
unsigned depth = 32;
2220

23-
cxxopts::Options options("eif2bitmap", "Ford EDB.EIF viewer ");
21+
cxxopts::Options options("eifconverter", "Ford EDB.EIF converter");
2422
options.add_options()
2523
("p,pack","Pack VBF file", cxxopts::value<bool>(pack))
2624
("u,unpack","Unpack VBF file", cxxopts::value<bool>(unpack))
@@ -49,65 +47,27 @@ int main(int argc, char **argv)
4947
}
5048

5149
if(unpack) {
52-
ifstream in_file(input_file_name, ios::binary | ios::ate);
53-
if(in_file.fail())
54-
throw runtime_error("File open error");
55-
56-
auto pos = in_file.tellg();
57-
in_file.seekg(0, ios::beg);
58-
59-
vector<uint8_t> file_content(pos);
60-
in_file.read(reinterpret_cast<char *>(&file_content[0]), file_content.size());
61-
if(in_file.fail())
62-
throw runtime_error("File open error");
63-
in_file.close();
6450

6551
if(out_file_name.empty()){
6652
out_file_name = input_file_name + ".bmp";
6753
}
6854

69-
EifImageBase* image;
70-
71-
if(file_content[7] == EIF_TYPE_MONOCHROME) {
72-
image = new EifImage8bit;
73-
} else if(file_content[7] == EIF_TYPE_MULTICOLOR) {
74-
image = new EifImage16bit;
75-
} else if(file_content[7] == EIF_TYPE_SUPERCOLOR){
76-
image = new EifImage32bit;
77-
} else {
78-
throw runtime_error("unsupported format");
79-
}
80-
81-
image->openEif(file_content);
82-
image->saveBmp(out_file_name);
83-
84-
delete image;
55+
vector<uint8_t> v;
56+
FTUtils::fileToVector(input_file_name, v);
57+
EIF::EifConverter::eifToBmpFile(v, out_file_name);
8558

8659
} else if(pack) {
8760

8861
if(out_file_name.empty()){
8962
out_file_name = input_file_name + ".eif";
9063
}
9164

92-
EifImageBase* image;
93-
switch (depth) {
94-
case 8:
95-
image = new EifImage8bit;
96-
break;
97-
case 16:
98-
image = new EifImage16bit;
99-
break;
100-
case 32:
101-
image = new EifImage32bit;
102-
break;
103-
default:
104-
throw runtime_error("Incorrect depth value");
65+
if(depth != 8 && depth != 16 && depth != 32) {
66+
cout << "Incorrect color depth value. It's may be only 8|16|32" << std::endl;
67+
return 0;
10568
}
10669

107-
image->openBmp(input_file_name);
108-
image->saveEif(out_file_name);
109-
110-
delete image;
70+
EIF::EifConverter::bmpFileToEifFile(input_file_name, depth, out_file_name);
11171
}
11272
} catch (const cxxopts::OptionException& e){
11373
cout << "error parsing options: " << e.what() << endl;

0 commit comments

Comments
 (0)