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

Function save_jpeg

dlib/image_saver/save_jpeg.cpp:45–102  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

43// ----------------------------------------------------------------------------------------
44
45 void save_jpeg (
46 const array2d<rgb_pixel>& img,
47 const std::string& filename,
48 int quality
49 )
50 {
51 // make sure requires clause is not broken
52 DLIB_CASSERT(img.size() != 0,
53 "\t save_jpeg()"
54 << "\n\t You can't save an empty image as a JPEG."
55 );
56 DLIB_CASSERT(0 <= quality && quality <= 100,
57 "\t save_jpeg()"
58 << "\n\t Invalid quality value."
59 << "\n\t quality: " << quality
60 );
61
62 FILE* outfile = fopen(filename.c_str(), "wb");
63 if (!outfile)
64 throw image_save_error("Can't open file " + filename + " for writing.");
65
66 jpeg_compress_struct cinfo;
67
68 jpeg_saver_error_mgr jerr;
69 cinfo.err = jpeg_std_error(&jerr.pub);
70 jerr.pub.error_exit = jpeg_saver_error_exit;
71 /* Establish the setjmp return context for my_error_exit to use. */
72 if (setjmp(jerr.setjmp_buffer))
73 {
74 /* If we get here, the JPEG code has signaled an error.
75 * We need to clean up the JPEG object, close the input file, and return.
76 */
77 jpeg_destroy_compress(&cinfo);
78 fclose(outfile);
79 throw image_save_error("save_jpeg: error while writing " + filename);
80 }
81
82 jpeg_create_compress(&cinfo);
83 jpeg_stdio_dest(&cinfo, outfile);
84
85 cinfo.image_width = img.nc();
86 cinfo.image_height = img.nr();
87 cinfo.input_components = 3;
88 cinfo.in_color_space = JCS_RGB;
89 jpeg_set_defaults(&cinfo);
90 jpeg_set_quality (&cinfo, quality, TRUE);
91 jpeg_start_compress(&cinfo, TRUE);
92
93 // now write out the rows one at a time
94 while (cinfo.next_scanline < cinfo.image_height) {
95 JSAMPROW row_pointer = (JSAMPROW) &img[cinfo.next_scanline][0];
96 jpeg_write_scanlines(&cinfo, &row_pointer, 1);
97 }
98
99 jpeg_finish_compress(&cinfo);
100 jpeg_destroy_compress(&cinfo);
101 fclose( outfile );
102 }

Callers

nothing calls this directly

Calls 12

jpeg_std_errorFunction · 0.85
jpeg_destroy_compressFunction · 0.85
jpeg_stdio_destFunction · 0.85
jpeg_set_defaultsFunction · 0.85
jpeg_set_qualityFunction · 0.85
jpeg_start_compressFunction · 0.85
jpeg_write_scanlinesFunction · 0.85
jpeg_finish_compressFunction · 0.85
image_save_errorClass · 0.70
sizeMethod · 0.45
ncMethod · 0.45
nrMethod · 0.45

Tested by

no test coverage detected