-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbmp-to-file.cc
123 lines (95 loc) · 3.7 KB
/
bmp-to-file.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// bmp-to-file.cc
// Usage:
// bmp-to-file INPUT OUTPUT length
// Takes a bitmap file named INPUT and rips the 54-byte header out and
// appends it to the file OUTPUT. Additionally, if length is provided,
// it truncates the output to be length bytes.
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv)
{
// --------------------------------------
// default parameters and input parsing
// --------------------------------------
const char* default_output_filename = "output";
char* output_filename = const_cast<char*>(default_output_filename);
int length = 0;
if(argc<=1) {
std::cout << "Please specify an input file! Usage:" << std::endl;
std::cout << " bmp-to-file INPUT OUTPUT length" << std::endl;
return 1;
}
std::cout << "Parsing arguments..." << std::endl;
char* input_filename = argv[1];
if(argc>=3) {
output_filename = argv[2];
}
if(argc==4) {
length = atoi(argv[2]);
if(length<=0) {
std::cout << "Invalid length specified! Please specify a length greater than zero." << std::endl;
return 1;
}
}
if(argc>4) {
std::cout << "Too many arguments specified!" << std::endl;
return 1;
}
std::cout << "-Using input=" << input_filename << std::endl;
std::cout << "-Using output=" << output_filename << std::endl;
std::cout << "-Using length=" << length << std::endl;
// -------------------------------------------------------
// Open the input file and load it into the buffer
// -------------------------------------------------------
FILE* input_file_pointer;
long input_filesize;
char* input_file_buffer;
size_t result;
// open the file
input_file_pointer = fopen(input_filename, "rb");
if(input_file_pointer==NULL) {
std::cout << "Unable to open the input file!" << std::endl;
return 1;
}
// get the size
fseek(input_file_pointer, 0, SEEK_END);
input_filesize = ftell(input_file_pointer);
rewind(input_file_pointer);
// check the header information at 0x0E (tells how much remaining header)
fseek(input_file_pointer, 0x0E, SEEK_CUR);
int* remaining_header = (int*) malloc(sizeof(int));
fread(remaining_header, 1, 1, input_file_pointer);
std::cout << "-Remaining header size=" << remaining_header[0] << std::endl;
int header_size = remaining_header[0]+0x0E;
std::cout << "-Total header size=" << header_size << std::endl;
// Move pointer position past header
rewind(input_file_pointer);
fseek(input_file_pointer, header_size, SEEK_CUR);
// allocate memory to read the file
input_file_buffer = (char*) malloc(sizeof(char)*(input_filesize-header_size));
if(input_file_buffer == NULL) {
std::cout << "Unable to allocate the input file slice!" << std::endl;
return 1;
}
// copy the file into the input_file_buffer:
result = fread(input_file_buffer,1,input_filesize-header_size,input_file_pointer);
fclose(input_file_pointer);
std::cout << "-Input file size=" << input_filesize << std::endl;
// ---------------------------------------------------
// Open the output file and start writing bitmap data
// ---------------------------------------------------
FILE *output_file_pointer;
if(NULL==(output_file_pointer=fopen(output_filename, "a"))) {
std::cout << "Unable to open the output file!" << std::endl;
return 1;
}
//fseek(output_file_pointer, 1, SEEK_END);
fwrite(input_file_buffer, 1, input_filesize-54, output_file_pointer);
// Close files and free data buffers
fclose(output_file_pointer);
free(input_file_buffer);
std::cout << output_filename << " successfully exported" << std::endl;
std::cout << "File size = " << ((double)(input_filesize-54))/1024.0/1024.0 << " MB" << std::endl;
return 0;
}