Coverity: fix WPrefs editmenu uninitialized scalar variables
[wmaker-crm.git] / wrlib / save_jpeg.c
blob5320ba47bcdf57d33873a73a49d14dbfb11cb538
1 /* save_jpeg.c - save JPEG image
3 * Raster graphics library
5 * Copyright (c) 2023 Window Maker Team
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
20 * MA 02110-1301, USA.
23 #include <config.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <assert.h>
29 #include <errno.h>
30 #include <jpeglib.h>
32 #include "wraster.h"
33 #include "imgformat.h"
34 #include "wr_i18n.h"
37 * Save RImage to JPEG image
40 Bool RSaveJPEG(RImage *img, const char *filename, char *title)
42 FILE *file;
43 int x, y, img_depth;
44 char *buffer;
45 RColor pixel;
46 struct jpeg_compress_struct cinfo;
47 struct jpeg_error_mgr jerr;
48 JSAMPROW row_pointer;
50 file = fopen(filename, "wb");
51 if (!file) {
52 RErrorCode = RERR_OPEN;
53 return False;
56 if (img->format == RRGBAFormat)
57 img_depth = 4;
58 else
59 img_depth = 3;
61 /* collect separate RGB values to a buffer */
62 buffer = malloc(sizeof(char) * 3 * img->width * img->height);
63 for (y = 0; y < img->height; y++) {
64 for (x = 0; x < img->width; x++) {
65 RGetPixel(img, x, y, &pixel);
66 buffer[y*img->width*3+x*3+0] = (char)(pixel.red);
67 buffer[y*img->width*3+x*3+1] = (char)(pixel.green);
68 buffer[y*img->width*3+x*3+2] = (char)(pixel.blue);
72 /* Setup Exception handling */
73 cinfo.err = jpeg_std_error(&jerr);
75 /* Initialize cinfo structure */
76 jpeg_create_compress(&cinfo);
77 jpeg_stdio_dest(&cinfo, file);
79 cinfo.image_width = img->width;
80 cinfo.image_height = img->height;
81 cinfo.input_components = 3;
82 cinfo.in_color_space = JCS_RGB;
84 jpeg_set_defaults(&cinfo);
85 jpeg_set_quality(&cinfo, 85, TRUE);
86 jpeg_start_compress(&cinfo, TRUE);
88 /* Set title if any */
89 if (title)
90 jpeg_write_marker(&cinfo, JPEG_COM, (const JOCTET*)title, strlen(title));
92 while (cinfo.next_scanline < cinfo.image_height) {
93 row_pointer = (JSAMPROW) &buffer[cinfo.next_scanline * img_depth * img->width];
94 jpeg_write_scanlines(&cinfo, &row_pointer, 1);
97 jpeg_finish_compress(&cinfo);
99 /* Clean */
100 free(buffer);
101 fclose(file);
103 return True;