8
#include "exifrestorer.h"
12
ExifRestorer::ExifRestorer()
14
jpegSections_.setAutoDelete(true);
20
ExifRestorer::~ExifRestorer()
25
void ExifRestorer::clear()
27
jpegSections_.clear();
38
int ExifRestorer::readFile(const QString& filename, ReadMode mode)
42
std::ifstream filestream;
44
filestream.open(filename.latin1(), ios::binary | ios::in);
45
if (!filestream.good()) {
46
qWarning("Failed to Open file");
50
unsigned char header[2];
51
filestream.read((char*)header, 2);
52
if (filestream.gcount() != 2) {
53
qWarning("Failed to read header");
59
if ((header[0] == 0xFF) && (header[1] == M_SOI)) {
61
// Smells like a jpeg file
63
bool found_SOSorEOI = false;
65
while (!filestream.eof()) {
67
// Padding bytes between sections
68
for (int i=0; i<7; i++){
69
filestream.read((char*)&marker, 1);
70
if (marker != 0xFF) break;
72
// 0xff is legal padding, but if we get
73
// that many times, something's wrong.
74
qWarning("Too many padding bytes");
79
JpegSection *section = new JpegSection;
83
filestream.read((char*)&lh, 1);
84
filestream.read((char*)&ll, 1);
85
section->size = (lh << 8) | ll;
86
if (section->size < 2) {
87
qWarning("Invalid Marker found");
91
section->data = new unsigned char[section->size];
92
section->data[0] = lh;
93
section->data[1] = ll;
94
filestream.read((char*)&(section->data[2]), section->size-2);
95
if (filestream.gcount() != (unsigned)section->size-2) {
96
qWarning("Premature File End");
100
section->type = marker;
101
jpegSections_.append(section);
103
//cout << (dec) << section->size << " ";
104
//cout << (hex) << int(section->type) << endl;
113
if (mode == EntireImage) {
115
currPos = filestream.tellg();
116
filestream.seekg(0,ios::end);
117
endPos = filestream.tellg();
118
filestream.seekg(currPos);
120
imageData_ = new JpegSection;
121
imageData_->size = endPos - currPos;
123
new unsigned char[imageData_->size];
125
filestream.read((char*)imageData_->data,
127
imageData_->type = marker;
150
if (section->data[2] == 'E' &&
151
section->data[3] == 'x' &&
152
section->data[4] == 'i' &&
153
section->data[5] == 'f') {
155
exifData_ = new JpegSection;
156
exifData_->size = section->size;
157
exifData_->type = section->type;
159
new unsigned char[section->size];
160
memcpy(exifData_->data, section->data,
172
if (found_SOSorEOI) {
173
if (mode == ExifOnly)
174
jpegSections_.clear();
180
qWarning("End of file without SOS or EOI");
185
qWarning("Not a jpeg file");
190
int ExifRestorer::writeFile(const QString& filename)
192
std::ofstream outStream (filename.latin1(), ios::binary | ios::out);
195
qWarning("Error in opening output file");
202
for (unsigned int i=0; i<jpegSections_.count(); i++) {
204
outStream.put(jpegSections_.at(i)->type);
205
if (!outStream.write((char*)jpegSections_.at(i)->data,
206
jpegSections_.at(i)->size))
210
if (!outStream.write((char*)imageData_->data, imageData_->size)) {
211
qWarning("Error in writing to file");
220
void ExifRestorer::insertExifData(JpegSection *exifSection)
222
QPtrList<JpegSection> newSections;
223
newSections.setAutoDelete(false);
225
// Check if the first section of jpegSections is a JFIF
226
// If so, add this to newSections
227
if (jpegSections_.at(0)->type == M_JFIF) {
228
newSections.append(jpegSections_.at(0));
232
// Now add the new exif section
233
JpegSection* newExifSection = new JpegSection;
234
newExifSection->type = exifSection->type;
235
newExifSection->size = exifSection->size;
236
newExifSection->data = new unsigned char[exifSection->size];
237
memcpy(newExifSection->data, exifSection->data,
240
newSections.append(newExifSection);
242
// Check if image already has exif section. if so replace it
243
for (JpegSection* section = jpegSections_.first(); section;
244
section = jpegSections_.next()) {
245
if (section->type == M_EXIF) {
246
jpegSections_.remove(section);
251
// Add the rest of the sections;
252
for (unsigned int i=1; i<jpegSections_.count(); i++) {
253
newSections.append(jpegSections_.at(i));
256
jpegSections_.setAutoDelete(false);
257
jpegSections_.clear();
259
for (unsigned int i=0; i<newSections.count(); i++) {
260
jpegSections_.append(newSections.at(i));
263
jpegSections_.setAutoDelete(true);