| 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 | } |
nothing calls this directly
no test coverage detected