1
+ /* M///////////////////////////////////////////////////////////////////////////////////////
2
+ //
3
+ // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4
+ //
5
+ // By downloading, copying, installing or using the software you agree to this
6
+ license.
7
+ // If you do not agree to this license, do not download, install,
8
+ // copy or use the software.
9
+ //
10
+ //
11
+ // License Agreement
12
+ // For Open Source Computer Vision Library
13
+ //
14
+ // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
15
+ // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
16
+ // Third party copyrights are property of their respective owners.
17
+ //
18
+ // Redistribution and use in source and binary forms, with or without
19
+ modification,
20
+ // are permitted provided that the following conditions are met:
21
+ //
22
+ // * Redistribution's of source code must retain the above copyright notice,
23
+ // this list of conditions and the following disclaimer.
24
+ //
25
+ // * Redistribution's in binary form must reproduce the above copyright
26
+ notice,
27
+ // this list of conditions and the following disclaimer in the documentation
28
+ // and/or other materials provided with the distribution.
29
+ //
30
+ // * The name of the copyright holders may not be used to endorse or promote
31
+ products
32
+ // derived from this software without specific prior written permission.
33
+ //
34
+ // This software is provided by the copyright holders and contributors "as is"
35
+ and
36
+ // any express or implied warranties, including, but not limited to, the implied
37
+ // warranties of merchantability and fitness for a particular purpose are
38
+ disclaimed.
39
+ // In no event shall the Intel Corporation or contributors be liable for any
40
+ direct,
41
+ // indirect, incidental, special, exemplary, or consequential damages
42
+ // (including, but not limited to, procurement of substitute goods or services;
43
+ // loss of use, data, or profits; or business interruption) however caused
44
+ // and on any theory of liability, whether in contract, strict liability,
45
+ // or tort (including negligence or otherwise) arising in any way out of
46
+ // the use of this software, even if advised of the possibility of such damage.
47
+ //
48
+ //M*/
1
49
#pragma once
50
+ #include < jpeglib.h>
2
51
#include < torch/types.h>
52
+ #include < vector>
3
53
4
54
namespace vision {
5
55
namespace image {
@@ -13,7 +63,7 @@ constexpr uint16_t ORIENTATION_EXIF_TAG = 0x0112;
13
63
constexpr uint16_t INCORRECT_TAG = -1 ;
14
64
15
65
// Functions in this module are taken from OpenCV
16
- // https://github.com/opencv/opencv/blob/097891e311fae1d8354eb092a0fd0171e630d78c/modules/modules/ imgcodecs/src/exif.cpp
66
+ // https://github.com/opencv/opencv/blob/097891e311fae1d8354eb092a0fd0171e630d78c/modules/imgcodecs/src/exif.cpp
17
67
inline uint16_t get_endianness (const std::vector<unsigned char >& exif_data) {
18
68
if ((exif_data.size () < 1 ) ||
19
69
(exif_data.size () > 1 && exif_data[0 ] != exif_data[1 ])) {
@@ -58,6 +108,59 @@ inline uint32_t get_uint32(
58
108
(exif_data[offset + 2 ] << 8 ) + exif_data[offset + 3 ];
59
109
}
60
110
111
+ inline int fetch_exif_orientation (j_decompress_ptr cinfo) {
112
+ int exif_orientation = -1 ;
113
+ // Check for Exif marker APP1
114
+ jpeg_saved_marker_ptr exif_marker = 0 ;
115
+ jpeg_saved_marker_ptr cmarker = cinfo->marker_list ;
116
+ while (cmarker && exif_marker == 0 ) {
117
+ if (cmarker->marker == APP1) {
118
+ exif_marker = cmarker;
119
+ }
120
+ cmarker = cmarker->next ;
121
+ }
122
+
123
+ if (exif_marker) {
124
+ // Code below is inspired from OpenCV
125
+ // https://github.com/opencv/opencv/blob/097891e311fae1d8354eb092a0fd0171e630d78c/modules/imgcodecs/src/exif.cpp
126
+
127
+ // Bytes from Exif size field to the first TIFF header
128
+ constexpr size_t start_offset = 6 ;
129
+ if (exif_marker->data_length > start_offset) {
130
+ auto * exif_data_ptr = exif_marker->data + start_offset;
131
+ auto size = exif_marker->data_length - start_offset;
132
+ std::vector<unsigned char > exif_data_vec (
133
+ exif_data_ptr, exif_data_ptr + size);
134
+
135
+ auto endianness = get_endianness (exif_data_vec);
136
+
137
+ // Checking whether Tag Mark (0x002A) correspond to one contained in the
138
+ // Jpeg file
139
+ uint16_t tag_mark = get_uint16 (exif_data_vec, endianness, 2 );
140
+ if (tag_mark == REQ_EXIF_TAG_MARK) {
141
+ auto offset = get_uint32 (exif_data_vec, endianness, 4 );
142
+ size_t num_entry = get_uint16 (exif_data_vec, endianness, offset);
143
+ offset += 2 ; // go to start of tag fields
144
+ constexpr size_t tiff_field_size = 12 ;
145
+ for (size_t entry = 0 ; entry < num_entry; entry++) {
146
+ // Here we just search for orientation tag and parse it
147
+ auto tag_num = get_uint16 (exif_data_vec, endianness, offset);
148
+ if (tag_num == INCORRECT_TAG) {
149
+ break ;
150
+ }
151
+ if (tag_num == ORIENTATION_EXIF_TAG) {
152
+ exif_orientation =
153
+ get_uint16 (exif_data_vec, endianness, offset + 8 );
154
+ break ;
155
+ }
156
+ offset += tiff_field_size;
157
+ }
158
+ }
159
+ }
160
+ }
161
+ return exif_orientation;
162
+ }
163
+
61
164
constexpr uint16_t IMAGE_ORIENTATION_TL = 1 ; // normal orientation
62
165
constexpr uint16_t IMAGE_ORIENTATION_TR = 2 ; // needs horizontal flip
63
166
constexpr uint16_t IMAGE_ORIENTATION_BR = 3 ; // needs 180 rotation
0 commit comments