| 38 | |
| 39 | #ifdef USE_JPEG |
| 40 | void write_jpeg(libraw_processed_image_t *img, const char *basename, int quality) |
| 41 | { |
| 42 | char fn[1024]; |
| 43 | if(img->colors != 1 && img->colors != 3) |
| 44 | { |
| 45 | printf("Only BW and 3-color images supported for JPEG output\n"); |
| 46 | return; |
| 47 | } |
| 48 | snprintf(fn, 1024, "%s.jpg", basename); |
| 49 | FILE *f = fopen(fn, "wb"); |
| 50 | if (!f) |
| 51 | return; |
| 52 | struct jpeg_compress_struct cinfo; |
| 53 | struct jpeg_error_mgr jerr; |
| 54 | JSAMPROW row_pointer[1]; /* pointer to JSAMPLE row[s] */ |
| 55 | int row_stride; /* physical row width in image buffer */ |
| 56 | |
| 57 | cinfo.err = jpeg_std_error(&jerr); |
| 58 | jpeg_create_compress(&cinfo); |
| 59 | jpeg_stdio_dest(&cinfo, f); |
| 60 | cinfo.image_width = img->width; /* image width and height, in pixels */ |
| 61 | cinfo.image_height = img->height; |
| 62 | cinfo.input_components = img->colors; /* # of color components per pixel */ |
| 63 | cinfo.in_color_space = img->colors==3?JCS_RGB:JCS_GRAYSCALE; /* colorspace of input image */ |
| 64 | jpeg_set_defaults(&cinfo); |
| 65 | jpeg_set_quality(&cinfo, quality, TRUE); |
| 66 | jpeg_start_compress(&cinfo, TRUE); |
| 67 | row_stride = img->width * img->colors; /* JSAMPLEs per row in image_buffer */ |
| 68 | while (cinfo.next_scanline < cinfo.image_height) { |
| 69 | row_pointer[0] = &img->data[cinfo.next_scanline * row_stride]; |
| 70 | (void)jpeg_write_scanlines(&cinfo, row_pointer, 1); |
| 71 | } |
| 72 | jpeg_finish_compress(&cinfo); |
| 73 | fclose(f); |
| 74 | jpeg_destroy_compress(&cinfo); |
| 75 | } |
| 76 | |
| 77 | #endif |
| 78 | |