| 1 |
/* |
| 2 |
* Copyright (C) 2005-2018 Team Kodi |
| 3 |
* This file is part of Kodi - https://kodi.tv |
| 4 |
* |
| 5 |
* SPDX-License-Identifier: GPL-2.0-or-later |
| 6 |
* See LICENSES/README.md for more information. |
| 7 |
*/ |
| 8 |
|
| 9 |
//-------------------------------------------------------------------------- |
| 10 |
// This module gathers information about a digital image file. This includes: |
| 11 |
// - File name and path |
| 12 |
// - File size |
| 13 |
// - Resolution (if available) |
| 14 |
// - IPTC information (if available) |
| 15 |
// - EXIF information (if available) |
| 16 |
// All gathered information is stored in a vector of 'description' and 'value' |
| 17 |
// pairs (where both description and value fields are of CStdString types). |
| 18 |
//-------------------------------------------------------------------------- |
| 19 |
|
| 20 |
#include "JpegParse.h" |
| 21 |
|
| 22 |
#include "filesystem/File.h" |
| 23 |
|
| 24 |
#ifdef TARGET_WINDOWS |
| 25 |
#include <windows.h> |
| 26 |
#else |
| 27 |
#include <memory.h> |
| 28 |
#include <cstring> |
| 29 |
typedef unsigned char BYTE; |
| 30 |
#endif |
| 31 |
|
| 32 |
#ifndef min |
| 33 |
#define min(a,b) (a)>(b)?(b):(a) |
| 34 |
#endif |
| 35 |
|
| 36 |
using namespace XFILE; |
| 37 |
|
| 38 |
//-------------------------------------------------------------------------- |
| 39 |
#define JPEG_PARSE_STRING_ID_BASE 21500 |
| 40 |
enum { |
| 41 |
ProcessUnknown = JPEG_PARSE_STRING_ID_BASE, |
| 42 |
ProcessSof0, |
| 43 |
ProcessSof1, |
| 44 |
ProcessSof2, |
| 45 |
ProcessSof3, |
| 46 |
ProcessSof5, |
| 47 |
ProcessSof6, |
| 48 |
ProcessSof7, |
| 49 |
ProcessSof9, |
| 50 |
ProcessSof10, |
| 51 |
ProcessSof11, |
| 52 |
ProcessSof13, |
| 53 |
ProcessSof14, |
| 54 |
ProcessSof15, |
| 55 |
}; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
//-------------------------------------------------------------------------- |
| 61 |
// Constructor |
| 62 |
//-------------------------------------------------------------------------- |
| 63 |
CJpegParse::CJpegParse(): |
| 64 |
m_SectionBuffer(NULL) |
| 65 |
{ |
| 66 |
memset(&m_ExifInfo, 0, sizeof(m_ExifInfo)); |
| 67 |
memset(&m_IPTCInfo, 0, sizeof(m_IPTCInfo)); |
| 68 |
} |
| 69 |
|
| 70 |
//-------------------------------------------------------------------------- |
| 71 |
// Process a SOFn marker. This is useful for the image dimensions |
| 72 |
//-------------------------------------------------------------------------- |
| 73 |
void CJpegParse::ProcessSOFn (void) |
| 74 |
{ |
| 75 |
m_ExifInfo.Height = CExifParse::Get16(m_SectionBuffer+3); |
| 76 |
m_ExifInfo.Width = CExifParse::Get16(m_SectionBuffer+5); |
| 77 |
|
| 78 |
unsigned char num_components = m_SectionBuffer[7]; |
| 79 |
if (num_components != 3) |
| 80 |
{ |
| 81 |
m_ExifInfo.IsColor = 0; |
| 82 |
} |
| 83 |
else |
| 84 |
{ |
| 85 |
m_ExifInfo.IsColor = 1; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
//-------------------------------------------------------------------------- |
| 91 |
// Read a section from a JPEG file. Note that this function allocates memory. |
| 92 |
// It must be called in pair with ReleaseSection |
| 93 |
//-------------------------------------------------------------------------- |
| 94 |
bool CJpegParse::GetSection (CFile& infile, const unsigned short sectionLength) |
| 95 |
{ |
| 96 |
if (sectionLength < 2) |
| 97 |
{ |
| 98 |
printf("JpgParse: invalid section length"); |
| 99 |
return false; |
| 100 |
} |
| 101 |
|
| 102 |
m_SectionBuffer = new unsigned char[sectionLength]; |
| 103 |
if (m_SectionBuffer == NULL) |
| 104 |
{ |
| 105 |
printf("JpgParse: could not allocate memory"); |
| 106 |
return false; |
| 107 |
} |
| 108 |
// Store first two pre-read bytes. |
| 109 |
m_SectionBuffer[0] = (unsigned char)(sectionLength >> 8); |
| 110 |
m_SectionBuffer[1] = (unsigned char)(sectionLength & 0x00FF); |
| 111 |
|
| 112 |
unsigned int len = (unsigned int)sectionLength; |
| 113 |
|
| 114 |
size_t bytesRead = infile.Read(m_SectionBuffer+sizeof(sectionLength), len-sizeof(sectionLength)); |
| 115 |
if (bytesRead != sectionLength-sizeof(sectionLength)) |
| 116 |
{ |
| 117 |
printf("JpgParse: premature end of file?"); |
| 118 |
ReleaseSection(); |
| 119 |
return false; |
| 120 |
} |
| 121 |
return true; |
| 122 |
} |
| 123 |
|
| 124 |
//-------------------------------------------------------------------------- |
| 125 |
// Deallocate memory allocated in GetSection. This function must always |
| 126 |
// be paired by a preceding GetSection call. |
| 127 |
//-------------------------------------------------------------------------- |
| 128 |
void CJpegParse::ReleaseSection (void) |
| 129 |
{ |
| 130 |
delete[] m_SectionBuffer; |
| 131 |
m_SectionBuffer = NULL; |
| 132 |
} |
| 133 |
|
| 134 |
//-------------------------------------------------------------------------- |
| 135 |
// Parse the marker stream until SOS or EOI is seen; infile has already been |
| 136 |
// successfully open |
| 137 |
//-------------------------------------------------------------------------- |
| 138 |
bool CJpegParse::ExtractInfo (CFile& infile) |
| 139 |
{ |
| 140 |
// Get file marker (two bytes - must be 0xFFD8 for JPEG files |
| 141 |
BYTE a; |
| 142 |
size_t bytesRead = infile.Read(&a, sizeof(BYTE)); |
| 143 |
if ((bytesRead != sizeof(BYTE)) || (a != 0xFF)) |
| 144 |
{ |
| 145 |
return false; |
| 146 |
} |
| 147 |
bytesRead = infile.Read(&a, sizeof(BYTE)); |
| 148 |
if ((bytesRead != sizeof(BYTE)) || (a != M_SOI)) |
| 149 |
{ |
| 150 |
return false; |
| 151 |
} |
| 152 |
|
| 153 |
for(;;) |
| 154 |
{ |
| 155 |
BYTE marker = 0; |
| 156 |
for (a=0; a<7; a++) { |
| 157 |
bytesRead = infile.Read(&marker, sizeof(BYTE)); |
| 158 |
if (marker != 0xFF) |
| 159 |
break; |
| 160 |
|
| 161 |
if (a >= 6) |
| 162 |
{ |
| 163 |
printf("JpgParse: too many padding bytes"); |
| 164 |
return false; |
| 165 |
} |
| 166 |
marker = 0; |
| 167 |
} |
| 168 |
|
| 169 |
// Read the length of the section. |
| 170 |
unsigned short itemlen = 0; |
| 171 |
bytesRead = infile.Read(&itemlen, sizeof(itemlen)); |
| 172 |
itemlen = CExifParse::Get16(&itemlen); |
| 173 |
|
| 174 |
if ((bytesRead != sizeof(itemlen)) || (itemlen < sizeof(itemlen))) |
| 175 |
{ |
| 176 |
printf("JpgParse: invalid marker"); |
| 177 |
return false; |
| 178 |
} |
| 179 |
|
| 180 |
switch(marker) |
| 181 |
{ |
| 182 |
case M_SOS: // stop before hitting compressed data |
| 183 |
return true; |
| 184 |
|
| 185 |
case M_EOI: // in case it's a tables-only JPEG stream |
| 186 |
printf("JpgParse: No image in jpeg!"); |
| 187 |
return false; |
| 188 |
break; |
| 189 |
|
| 190 |
case M_COM: // Comment section |
| 191 |
GetSection(infile, itemlen); |
| 192 |
if (m_SectionBuffer != NULL) |
| 193 |
{ |
| 194 |
// CExifParse::FixComment(comment); // Ensure comment is printable |
| 195 |
unsigned short length = min(itemlen - 2, MAX_COMMENT); |
| 196 |
strncpy(m_ExifInfo.FileComment, (char *)&m_SectionBuffer[2], length); |
| 197 |
m_ExifInfo.FileComment[length] = '\0'; |
| 198 |
} |
| 199 |
ReleaseSection(); |
| 200 |
break; |
| 201 |
|
| 202 |
case M_SOF0: |
| 203 |
case M_SOF1: |
| 204 |
case M_SOF2: |
| 205 |
case M_SOF3: |
| 206 |
case M_SOF5: |
| 207 |
case M_SOF6: |
| 208 |
case M_SOF7: |
| 209 |
case M_SOF9: |
| 210 |
case M_SOF10: |
| 211 |
case M_SOF11: |
| 212 |
case M_SOF13: |
| 213 |
case M_SOF14: |
| 214 |
case M_SOF15: |
| 215 |
GetSection(infile, itemlen); |
| 216 |
if ((m_SectionBuffer != NULL) && (itemlen >= 7)) |
| 217 |
{ |
| 218 |
ProcessSOFn(); |
| 219 |
m_ExifInfo.Process = marker; |
| 220 |
} |
| 221 |
ReleaseSection(); |
| 222 |
break; |
| 223 |
|
| 224 |
case M_IPTC: |
| 225 |
GetSection(infile, itemlen); |
| 226 |
if (m_SectionBuffer != NULL) |
| 227 |
{ |
| 228 |
CIptcParse::Process(m_SectionBuffer, itemlen, &m_IPTCInfo); |
| 229 |
} |
| 230 |
ReleaseSection(); |
| 231 |
break; |
| 232 |
|
| 233 |
case M_EXIF: |
| 234 |
// Seen files from some 'U-lead' software with Vivitar scanner |
| 235 |
// that uses marker 31 for non exif stuff. Thus make sure |
| 236 |
// it says 'Exif' in the section before treating it as exif. |
| 237 |
GetSection(infile, itemlen); |
| 238 |
if (m_SectionBuffer != NULL) |
| 239 |
{ |
| 240 |
CExifParse exif; |
| 241 |
exif.Process(m_SectionBuffer, itemlen, &m_ExifInfo); |
| 242 |
} |
| 243 |
ReleaseSection(); |
| 244 |
break; |
| 245 |
|
| 246 |
case M_JFIF: |
| 247 |
// Regular jpegs always have this tag, exif images have the exif |
| 248 |
// marker instead, although ACDsee will write images with both markers. |
| 249 |
// this program will re-create this marker on absence of exif marker. |
| 250 |
// hence no need to keep the copy from the file. |
| 251 |
// fall through to default case |
| 252 |
default: |
| 253 |
// Skip any other sections. |
| 254 |
GetSection(infile, itemlen); |
| 255 |
ReleaseSection(); |
| 256 |
break; |
| 257 |
} |
| 258 |
} |
| 259 |
return true; |
| 260 |
} |
| 261 |
|
| 262 |
//-------------------------------------------------------------------------- |
| 263 |
// Process a file. Check if it is JPEG. Extract exif/iptc info if it is. |
| 264 |
//-------------------------------------------------------------------------- |
| 265 |
bool CJpegParse::Process (const char *picFileName) |
| 266 |
{ |
| 267 |
CFile file; |
| 268 |
|
| 269 |
if (!file.Open(picFileName)) |
| 270 |
return false; |
| 271 |
|
| 272 |
// File exists and successfully opened. Start processing |
| 273 |
// Gather all information about the file |
| 274 |
|
| 275 |
/* // Get file name... |
| 276 |
CStdString tmp, urlFName, path; |
| 277 |
CURL url(picFileName); |
| 278 |
url.GetURLWithoutUserDetails(urlFName); |
| 279 |
CUtil::Split(urlFName, path, tmp); |
| 280 |
m_JpegInfo[SLIDESHOW_FILE_NAME] = tmp; |
| 281 |
// ...then path... |
| 282 |
m_JpegInfo[SLIDESHOW_FILE_PATH] = path; |
| 283 |
|
| 284 |
// ...then size... |
| 285 |
__stat64 fileStat; |
| 286 |
CFile::Stat(picFileName, &fileStat); |
| 287 |
float fileSize = (float)fileStat.st_size; |
| 288 |
tmp = ""; |
| 289 |
if (fileSize > 1024) |
| 290 |
{ |
| 291 |
fileSize /= 1024; |
| 292 |
tmp = "KB"; |
| 293 |
} |
| 294 |
if (fileSize > 1024) |
| 295 |
{ |
| 296 |
fileSize /= 1024; |
| 297 |
tmp = "MB"; |
| 298 |
} |
| 299 |
if (fileSize > 1024) |
| 300 |
{ |
| 301 |
fileSize /= 1024; |
| 302 |
tmp = "GB"; |
| 303 |
} |
| 304 |
tmp.Format("%.2f %s", fileSize, tmp); |
| 305 |
m_JpegInfo[SLIDESHOW_FILE_SIZE] = tmp; |
| 306 |
|
| 307 |
// ...then date and time... |
| 308 |
CDateTime date((time_t)fileStat.st_mtime); |
| 309 |
tmp.Format("%s %s", date.GetAsLocalizedDate(), date.GetAsLocalizedTime()); |
| 310 |
m_JpegInfo[SLIDESHOW_FILE_DATE] = tmp;*/ |
| 311 |
|
| 312 |
bool result = ExtractInfo(file); |
| 313 |
file.Close(); |
| 314 |
return result; |
| 315 |
} |
| 316 |
|