• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "image_io/jpeg/jpeg_marker.h"
2 
3 #include <iomanip>
4 #include <limits>
5 #include <sstream>
6 #include <string>
7 #include <vector>
8 
9 namespace photos_editing_formats {
10 namespace image_io {
11 
12 using std::string;
13 using std::stringstream;
14 using std::vector;
15 
16 // Storage for class (static) data members.
17 const size_t JpegMarker::kLength;      // = 2;
18 const size_t JpegMarker::kTypeOffset;  // = 1;
19 const Byte JpegMarker::kZERO;          // = 0x00;
20 const Byte JpegMarker::kStart;         // = 0xFF;
21 const Byte JpegMarker::kSOS;           // = 0xDA;
22 const Byte JpegMarker::kSOI;           // = 0xD8;
23 const Byte JpegMarker::kEOI;           // = 0xD9;
24 const Byte JpegMarker::kAPP0;          // = 0xE0;
25 const Byte JpegMarker::kAPP1;          // = 0xE1;
26 const Byte JpegMarker::kAPP2;          // = 0xE2;
27 const Byte JpegMarker::kFILL;          // = 0xFF;
28 
GetName() const29 const std::string JpegMarker::GetName() const {
30   switch (type_) {
31     case 0x01:
32       return "TEM";
33     case 0xC4:
34       return "DHT";
35     case 0xC8:
36       return "JPG";
37     case 0xCC:
38       return "DAC";
39     case JpegMarker::kSOI:
40       return"SOI";
41     case JpegMarker::kEOI:
42       return "EOI";
43     case JpegMarker::kSOS:
44       return "SOS";
45     case 0xDB:
46       return "DQT";
47     case 0xDC:
48       return "DNL";
49     case 0xDD:
50       return "DRI";
51     case 0xDE:
52       return "DHP";
53     case 0xDF:
54       return "EXP";
55     case 0xFE:
56       return "COM";
57   }
58 
59   stringstream name_stream;
60 
61   if (0xC0 <= type_ && type_ <= 0xC0+15) {
62     name_stream << "SOF" << type_-0xC0;
63     return name_stream.str();
64   }
65   if (0xD0 <= type_ && type_ <= 0xD0+7) {
66     name_stream << "RST" << type_-0xD0;
67     return name_stream.str();
68   }
69   if (JpegMarker::kAPP0 <= type_ && type_ <= JpegMarker::kAPP0+15) {
70     name_stream << "APP" << type_-JpegMarker::kAPP0;
71     return name_stream.str();
72   }
73   if (0xF0 <= type_ && type_ <= 0xF0+13) {
74     name_stream << "JPG" << type_-0xF0;
75     return name_stream.str();
76   }
77   return GetHexString("0x");
78 }
79 
GetHexString(const std::string & prefix) const80 const std::string JpegMarker::GetHexString(const std::string& prefix) const {
81     stringstream name_stream;
82     name_stream << prefix << std::hex << std::uppercase << std::setfill('0')
83                 << std::setw(2) << static_cast<int>(type_);
84     return name_stream.str();
85 }
86 
HasVariablePayloadSize() const87 bool JpegMarker::HasVariablePayloadSize() const {
88   return type_ != 0x00 && type_ != 0x01 && (type_ < 0xD0 || type_ > 0xD7) &&
89       type_ != JpegMarker::kSOI && type_ != JpegMarker::kEOI &&
90       type_ != 0xFF;
91 }
92 
IsEntropySegmentDelimiter() const93 bool JpegMarker::IsEntropySegmentDelimiter() const {
94   return (type_ == kSOS || (type_ >= 0xD0 && type_ <= 0xD7));
95 }
96 
97 }  // namespace image_io
98 }  // namespace photos_editing_formats
99