File size: 20,780 Bytes
9375c9a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<html><!-- Created using the cpp_pretty_printer from the dlib C++ library.  See http://dlib.net for updates. --><head><title>dlib C++ Library - jpeg_loader.cpp</title></head><body bgcolor='white'><pre>
<font color='#009900'>// Copyright (C) 2010  Davis E. King ([email protected]), Nils Labugt
</font><font color='#009900'>// License: Boost Software License   See LICENSE.txt for the full license.
</font><font color='#0000FF'>#ifndef</font> DLIB_JPEG_LOADER_CPp_
<font color='#0000FF'>#define</font> DLIB_JPEG_LOADER_CPp_

<font color='#009900'>// only do anything with this file if DLIB_JPEG_SUPPORT is defined
</font><font color='#0000FF'>#ifdef</font> DLIB_JPEG_SUPPORT

<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='../array2d.h.html'>../array2d.h</a>"
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='../pixel.h.html'>../pixel.h</a>"
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='../dir_nav.h.html'>../dir_nav.h</a>"
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='jpeg_loader.h.html'>jpeg_loader.h</a>"
<font color='#0000FF'>#include</font> <font color='#5555FF'>&lt;</font>stdio.h<font color='#5555FF'>&gt;</font>
<font color='#0000FF'>#ifdef</font> DLIB_JPEG_STATIC
#   include "<font color='#CC0000'>../external/libjpeg/jpeglib.h</font>"
<font color='#0000FF'>#else</font>
#   include <font color='#5555FF'>&lt;</font>jpeglib.h<font color='#5555FF'>&gt;</font>
<font color='#0000FF'>#endif</font>
<font color='#0000FF'>#include</font> <font color='#5555FF'>&lt;</font>sstream<font color='#5555FF'>&gt;</font>
<font color='#0000FF'>#include</font> <font color='#5555FF'>&lt;</font>setjmp.h<font color='#5555FF'>&gt;</font>

<font color='#0000FF'>namespace</font> dlib
<b>{</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    jpeg_loader::
    <b><a name='jpeg_loader'></a>jpeg_loader</b><font face='Lucida Console'>(</font> <font color='#0000FF'>const</font> <font color='#0000FF'><u>char</u></font><font color='#5555FF'>*</font> filename <font face='Lucida Console'>)</font> : height_<font face='Lucida Console'>(</font> <font color='#979000'>0</font> <font face='Lucida Console'>)</font>, width_<font face='Lucida Console'>(</font> <font color='#979000'>0</font> <font face='Lucida Console'>)</font>, output_components_<font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#BB00BB'>read_image</font><font face='Lucida Console'>(</font> <font color='#BB00BB'>check_file</font><font face='Lucida Console'>(</font> filename <font face='Lucida Console'>)</font>, NULL, <font color='#979000'>0</font>L <font face='Lucida Console'>)</font>;
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    jpeg_loader::
    <b><a name='jpeg_loader'></a>jpeg_loader</b><font face='Lucida Console'>(</font> <font color='#0000FF'>const</font> std::string<font color='#5555FF'>&amp;</font> filename <font face='Lucida Console'>)</font> : height_<font face='Lucida Console'>(</font> <font color='#979000'>0</font> <font face='Lucida Console'>)</font>, width_<font face='Lucida Console'>(</font> <font color='#979000'>0</font> <font face='Lucida Console'>)</font>, output_components_<font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#BB00BB'>read_image</font><font face='Lucida Console'>(</font> <font color='#BB00BB'>check_file</font><font face='Lucida Console'>(</font> filename.<font color='#BB00BB'>c_str</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font face='Lucida Console'>)</font>, NULL, <font color='#979000'>0</font>L <font face='Lucida Console'>)</font>;
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    jpeg_loader::
    <b><a name='jpeg_loader'></a>jpeg_loader</b><font face='Lucida Console'>(</font> <font color='#0000FF'>const</font> dlib::file<font color='#5555FF'>&amp;</font> f <font face='Lucida Console'>)</font> : height_<font face='Lucida Console'>(</font> <font color='#979000'>0</font> <font face='Lucida Console'>)</font>, width_<font face='Lucida Console'>(</font> <font color='#979000'>0</font> <font face='Lucida Console'>)</font>, output_components_<font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#BB00BB'>read_image</font><font face='Lucida Console'>(</font> <font color='#BB00BB'>check_file</font><font face='Lucida Console'>(</font> f.<font color='#BB00BB'>full_name</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>c_str</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font face='Lucida Console'>)</font>, NULL, <font color='#979000'>0</font>L <font face='Lucida Console'>)</font>;
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>    
    jpeg_loader::
    <b><a name='jpeg_loader'></a>jpeg_loader</b><font face='Lucida Console'>(</font> <font color='#0000FF'>const</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>char</u></font><font color='#5555FF'>*</font> imgbuffer, <font color='#0000FF'><u>size_t</u></font> imgbuffersize <font face='Lucida Console'>)</font> : height_<font face='Lucida Console'>(</font> <font color='#979000'>0</font> <font face='Lucida Console'>)</font>, width_<font face='Lucida Console'>(</font> <font color='#979000'>0</font> <font face='Lucida Console'>)</font>, output_components_<font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#BB00BB'>read_image</font><font face='Lucida Console'>(</font> NULL, imgbuffer, imgbuffersize <font face='Lucida Console'>)</font>;
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'><u>bool</u></font> jpeg_loader::<b><a name='is_gray'></a>is_gray</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>
    <b>{</b>
        <font color='#0000FF'>return</font> <font face='Lucida Console'>(</font>output_components_ <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>1</font><font face='Lucida Console'>)</font>;
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'><u>bool</u></font> jpeg_loader::<b><a name='is_rgb'></a>is_rgb</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>
    <b>{</b>
        <font color='#0000FF'>return</font> <font face='Lucida Console'>(</font>output_components_ <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>3</font><font face='Lucida Console'>)</font>;
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'><u>bool</u></font> jpeg_loader::<b><a name='is_rgba'></a>is_rgba</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>
    <b>{</b>
        <font color='#0000FF'>return</font> <font face='Lucida Console'>(</font>output_components_ <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>4</font><font face='Lucida Console'>)</font>;
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'>struct</font> <b><a name='jpeg_loader_error_mgr'></a>jpeg_loader_error_mgr</b> 
    <b>{</b>
        jpeg_error_mgr pub;    <font color='#009900'>/* "public" fields */</font>
        jmp_buf setjmp_buffer;  <font color='#009900'>/* for return to caller */</font>
        <font color='#0000FF'><u>char</u></font> jpegLastErrorMsg[JMSG_LENGTH_MAX];
    <b>}</b>;

    <font color='#0000FF'><u>void</u></font> <b><a name='jpeg_loader_error_exit'></a>jpeg_loader_error_exit</b> <font face='Lucida Console'>(</font>j_common_ptr cinfo<font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#009900'>/* cinfo-&gt;err really points to a jpeg_loader_error_mgr struct, so coerce pointer */</font>
        jpeg_loader_error_mgr<font color='#5555FF'>*</font> myerr <font color='#5555FF'>=</font> <font face='Lucida Console'>(</font>jpeg_loader_error_mgr<font color='#5555FF'>*</font><font face='Lucida Console'>)</font> cinfo<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>err;
        
        <font color='#009900'>/* Create the message */</font>
        <font face='Lucida Console'>(</font> <font color='#5555FF'>*</font><font face='Lucida Console'>(</font> cinfo<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>err<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>format_message <font face='Lucida Console'>)</font> <font face='Lucida Console'>)</font> <font face='Lucida Console'>(</font> cinfo, myerr<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>jpegLastErrorMsg <font face='Lucida Console'>)</font>;

        <font color='#009900'>/* Return control to the setjmp point */</font>
        <font color='#BB00BB'>longjmp</font><font face='Lucida Console'>(</font>myerr<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>setjmp_buffer, <font color='#979000'>1</font><font face='Lucida Console'>)</font>;
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>    FILE <font color='#5555FF'>*</font> jpeg_loader::<b><a name='check_file'></a>check_file</b><font face='Lucida Console'>(</font> <font color='#0000FF'>const</font> <font color='#0000FF'><u>char</u></font><font color='#5555FF'>*</font> filename <font face='Lucida Console'>)</font>
    <b>{</b>
      <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font> filename <font color='#5555FF'>=</font><font color='#5555FF'>=</font> NULL <font face='Lucida Console'>)</font>
      <b>{</b>
          <font color='#0000FF'>throw</font> <font color='#BB00BB'>image_load_error</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>jpeg_loader: invalid filename, it is NULL</font>"<font face='Lucida Console'>)</font>;
      <b>}</b>
      FILE <font color='#5555FF'>*</font>fp <font color='#5555FF'>=</font> <font color='#BB00BB'>fopen</font><font face='Lucida Console'>(</font> filename, "<font color='#CC0000'>rb</font>" <font face='Lucida Console'>)</font>;
      <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font> <font color='#5555FF'>!</font>fp <font face='Lucida Console'>)</font>
      <b>{</b>
          <font color='#0000FF'>throw</font> <font color='#BB00BB'>image_load_error</font><font face='Lucida Console'>(</font>std::<font color='#BB00BB'>string</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>jpeg_loader: unable to open file </font>"<font face='Lucida Console'>)</font> <font color='#5555FF'>+</font> filename<font face='Lucida Console'>)</font>;
      <b>}</b>
      <font color='#0000FF'>return</font> fp;
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'><u>void</u></font> jpeg_loader::<b><a name='read_image'></a>read_image</b><font face='Lucida Console'>(</font> FILE <font color='#5555FF'>*</font> file, <font color='#0000FF'>const</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>char</u></font><font color='#5555FF'>*</font> imgbuffer, <font color='#0000FF'><u>size_t</u></font> imgbuffersize <font face='Lucida Console'>)</font>
    <b>{</b>
        
        jpeg_decompress_struct cinfo;
        jpeg_loader_error_mgr jerr;

        cinfo.err <font color='#5555FF'>=</font> <font color='#BB00BB'>jpeg_std_error</font><font face='Lucida Console'>(</font><font color='#5555FF'>&amp;</font>jerr.pub<font face='Lucida Console'>)</font>;

        jerr.pub.error_exit <font color='#5555FF'>=</font> jpeg_loader_error_exit;

        <font color='#009900'>/* Establish the setjmp return context for my_error_exit to use. */</font>
        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font><font color='#BB00BB'>setjmp</font><font face='Lucida Console'>(</font>jerr.setjmp_buffer<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font> 
        <b>{</b>
            <font color='#009900'>/* If we get here, the JPEG code has signaled an error.
             * We need to clean up the JPEG object, and return.
             */</font>
            <font color='#BB00BB'>jpeg_destroy_decompress</font><font face='Lucida Console'>(</font><font color='#5555FF'>&amp;</font>cinfo<font face='Lucida Console'>)</font>;
            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>file <font color='#5555FF'>!</font><font color='#5555FF'>=</font> NULL<font face='Lucida Console'>)</font> <font color='#BB00BB'>fclose</font><font face='Lucida Console'>(</font>file<font face='Lucida Console'>)</font>;
            <font color='#0000FF'>throw</font> <font color='#BB00BB'>image_load_error</font><font face='Lucida Console'>(</font>std::<font color='#BB00BB'>string</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>jpeg_loader: error while loading image: </font>"<font face='Lucida Console'>)</font> <font color='#5555FF'>+</font> jerr.jpegLastErrorMsg<font face='Lucida Console'>)</font>;
        <b>}</b>


        <font color='#BB00BB'>jpeg_create_decompress</font><font face='Lucida Console'>(</font><font color='#5555FF'>&amp;</font>cinfo<font face='Lucida Console'>)</font>;
        
        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>file <font color='#5555FF'>!</font><font color='#5555FF'>=</font> NULL<font face='Lucida Console'>)</font> <font color='#BB00BB'>jpeg_stdio_src</font><font face='Lucida Console'>(</font><font color='#5555FF'>&amp;</font>cinfo, file<font face='Lucida Console'>)</font>;
        <font color='#0000FF'>else</font> <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>imgbuffer <font color='#5555FF'>!</font><font color='#5555FF'>=</font> NULL<font face='Lucida Console'>)</font> <font color='#BB00BB'>jpeg_mem_src</font><font face='Lucida Console'>(</font><font color='#5555FF'>&amp;</font>cinfo, <font face='Lucida Console'>(</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>char</u></font><font color='#5555FF'>*</font><font face='Lucida Console'>)</font>imgbuffer, imgbuffersize<font face='Lucida Console'>)</font>;
        <font color='#0000FF'>else</font> <font color='#0000FF'>throw</font> <font color='#BB00BB'>image_load_error</font><font face='Lucida Console'>(</font>std::<font color='#BB00BB'>string</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>jpeg_loader: no valid image source</font>"<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;

        <font color='#BB00BB'>jpeg_read_header</font><font face='Lucida Console'>(</font><font color='#5555FF'>&amp;</font>cinfo, TRUE<font face='Lucida Console'>)</font>;

        <font color='#BB00BB'>jpeg_start_decompress</font><font face='Lucida Console'>(</font><font color='#5555FF'>&amp;</font>cinfo<font face='Lucida Console'>)</font>;

        height_ <font color='#5555FF'>=</font> cinfo.output_height;
        width_ <font color='#5555FF'>=</font> cinfo.output_width;
        output_components_ <font color='#5555FF'>=</font> cinfo.output_components;

        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>output_components_ <font color='#5555FF'>!</font><font color='#5555FF'>=</font> <font color='#979000'>1</font> <font color='#5555FF'>&amp;</font><font color='#5555FF'>&amp;</font> 
            output_components_ <font color='#5555FF'>!</font><font color='#5555FF'>=</font> <font color='#979000'>3</font> <font color='#5555FF'>&amp;</font><font color='#5555FF'>&amp;</font>
            output_components_ <font color='#5555FF'>!</font><font color='#5555FF'>=</font> <font color='#979000'>4</font><font face='Lucida Console'>)</font>
        <b>{</b>
            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>file <font color='#5555FF'>!</font><font color='#5555FF'>=</font> NULL<font face='Lucida Console'>)</font> <font color='#BB00BB'>fclose</font><font face='Lucida Console'>(</font>file<font face='Lucida Console'>)</font>;
            <font color='#BB00BB'>jpeg_destroy_decompress</font><font face='Lucida Console'>(</font><font color='#5555FF'>&amp;</font>cinfo<font face='Lucida Console'>)</font>;
            std::ostringstream sout;
            sout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>jpeg_loader: Unsupported number of colors (</font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> output_components_ <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>) in image</font>";
            <font color='#0000FF'>throw</font> <font color='#BB00BB'>image_load_error</font><font face='Lucida Console'>(</font>sout.<font color='#BB00BB'>str</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
        <b>}</b>

        std::vector<font color='#5555FF'>&lt;</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>char</u></font><font color='#5555FF'>*</font><font color='#5555FF'>&gt;</font> rows;
        rows.<font color='#BB00BB'>resize</font><font face='Lucida Console'>(</font>height_<font face='Lucida Console'>)</font>;

        <font color='#009900'>// size the image buffer
</font>        data.<font color='#BB00BB'>resize</font><font face='Lucida Console'>(</font>height_<font color='#5555FF'>*</font>width_<font color='#5555FF'>*</font>output_components_<font face='Lucida Console'>)</font>;

        <font color='#009900'>// setup pointers to each row
</font>        <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> i <font color='#5555FF'>=</font> <font color='#979000'>0</font>; i <font color='#5555FF'>&lt;</font> rows.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>i<font face='Lucida Console'>)</font>
            rows[i] <font color='#5555FF'>=</font> <font color='#5555FF'>&amp;</font>data[i<font color='#5555FF'>*</font>width_<font color='#5555FF'>*</font>output_components_];

        <font color='#009900'>// read the data into the buffer
</font>        <font color='#0000FF'>while</font> <font face='Lucida Console'>(</font>cinfo.output_scanline <font color='#5555FF'>&lt;</font> cinfo.output_height<font face='Lucida Console'>)</font>
        <b>{</b>
            <font color='#BB00BB'>jpeg_read_scanlines</font><font face='Lucida Console'>(</font><font color='#5555FF'>&amp;</font>cinfo, <font color='#5555FF'>&amp;</font>rows[cinfo.output_scanline], <font color='#979000'>100</font><font face='Lucida Console'>)</font>;
        <b>}</b>

        <font color='#BB00BB'>jpeg_finish_decompress</font><font face='Lucida Console'>(</font><font color='#5555FF'>&amp;</font>cinfo<font face='Lucida Console'>)</font>;
        <font color='#BB00BB'>jpeg_destroy_decompress</font><font face='Lucida Console'>(</font><font color='#5555FF'>&amp;</font>cinfo<font face='Lucida Console'>)</font>;

        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>file <font color='#5555FF'>!</font><font color='#5555FF'>=</font> NULL<font face='Lucida Console'>)</font> <font color='#BB00BB'>fclose</font><font face='Lucida Console'>(</font>file<font face='Lucida Console'>)</font>;
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<b>}</b>

<font color='#0000FF'>#endif</font> <font color='#009900'>// DLIB_JPEG_SUPPORT
</font>
<font color='#0000FF'>#endif</font> <font color='#009900'>// DLIB_JPEG_LOADER_CPp_
</font>


</pre></body></html>