MCPcopy Create free account
hub / github.com/davisking/dlib / read_image

Method read_image

dlib/image_loader/jpeg_loader.cpp:131–198  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

129// ----------------------------------------------------------------------------------------
130
131 void jpeg_loader::read_image( FILE * file, const unsigned char* imgbuffer, size_t imgbuffersize )
132 {
133
134 jpeg_decompress_struct cinfo;
135 jpeg_loader_error_mgr jerr;
136
137 cinfo.err = jpeg_std_error(&jerr.pub);
138
139 jerr.pub.error_exit = jpeg_loader_error_exit;
140
141 /* Establish the setjmp return context for my_error_exit to use. */
142 if (setjmp(jerr.setjmp_buffer))
143 {
144 /* If we get here, the JPEG code has signaled an error.
145 * We need to clean up the JPEG object, and return.
146 */
147 jpeg_destroy_decompress(&cinfo);
148 if (file != NULL) fclose(file);
149 throw image_load_error(std::string("jpeg_loader: error while loading image: ") + jerr.jpegLastErrorMsg);
150 }
151
152
153 jpeg_create_decompress(&cinfo);
154
155 if (file != NULL) jpeg_stdio_src(&cinfo, file);
156 else if (imgbuffer != NULL) jpeg_mem_src(&cinfo, (unsigned char*)imgbuffer, imgbuffersize);
157 else throw image_load_error(std::string("jpeg_loader: no valid image source"));
158
159 jpeg_read_header(&cinfo, TRUE);
160
161 jpeg_start_decompress(&cinfo);
162
163 height_ = cinfo.output_height;
164 width_ = cinfo.output_width;
165 output_components_ = cinfo.output_components;
166
167 if (output_components_ != 1 &&
168 output_components_ != 3 &&
169 output_components_ != 4)
170 {
171 if (file != NULL) fclose(file);
172 jpeg_destroy_decompress(&cinfo);
173 std::ostringstream sout;
174 sout << "jpeg_loader: Unsupported number of colors (" << output_components_ << ") in image";
175 throw image_load_error(sout.str());
176 }
177
178 std::vector<unsigned char*> rows;
179 rows.resize(height_);
180
181 // size the image buffer
182 data.resize(height_*width_*output_components_);
183
184 // setup pointers to each row
185 for (size_t i = 0; i < rows.size(); ++i)
186 rows[i] = &data[i*width_*output_components_];
187
188 // read the data into the buffer

Callers

nothing calls this directly

Calls 12

jpeg_std_errorFunction · 0.85
jpeg_destroy_decompressFunction · 0.85
jpeg_stdio_srcFunction · 0.85
jpeg_mem_srcFunction · 0.85
jpeg_read_headerFunction · 0.85
jpeg_start_decompressFunction · 0.85
jpeg_read_scanlinesFunction · 0.85
jpeg_finish_decompressFunction · 0.85
image_load_errorClass · 0.70
strMethod · 0.45
resizeMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected