Change to the linux kernel coding style
[wmaker-crm.git] / util / directjpeg.c
Commit [+]AuthorDateLineData
416e3a82 dan1999-01-25 19:06:50 +00001/* directjpeg.c- loads a jpeg file directly into a XImage
2 *
3 * WindowMaker window manager
6830b057 dan2004-10-12 21:28:27 +00004 *
4153e2fd dan2003-01-16 23:30:45 +00005 * Copyright (c) 1999-2003 Alfredo K. Kojima
6830b057 dan2004-10-12 21:28:27 +00006 *
416e3a82 dan1999-01-25 19:06:50 +00007 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program 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
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
6830b057 dan2004-10-12 21:28:27 +000019 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
416e3a82 dan1999-01-25 19:06:50 +000020 * USA.
21 */
22
416e3a82 dan1999-01-25 19:06:50 +000023#include "../src/config.h"
24
416e3a82 dan1999-01-25 19:06:50 +000025#ifdef USE_JPEG
26
27#include <stdlib.h>
28#include <stdio.h>
29#include <string.h>
30
31#include <jpeglib.h>
32
33#include "../wrlib/wraster.h"
34
35#include <setjmp.h>
36
416e3a82 dan1999-01-25 19:06:50 +000037struct my_error_mgr {
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020038 struct jpeg_error_mgr pub; /* "public" fields */
416e3a82 dan1999-01-25 19:06:50 +000039
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020040 jmp_buf setjmp_buffer; /* for return to caller */
416e3a82 dan1999-01-25 19:06:50 +000041};
42
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020043typedef struct my_error_mgr *my_error_ptr;
416e3a82 dan1999-01-25 19:06:50 +000044
45/*
46 * Here's the routine that will replace the standard error_exit method:
47 */
48
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020049static void my_error_exit(j_common_ptr cinfo)
416e3a82 dan1999-01-25 19:06:50 +000050{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020051 /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
52 my_error_ptr myerr = (my_error_ptr) cinfo->err;
416e3a82 dan1999-01-25 19:06:50 +000053
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020054 /* Always display the message. */
55 /* We could postpone this until after returning, if we chose. */
56 (*cinfo->err->output_message) (cinfo);
416e3a82 dan1999-01-25 19:06:50 +000057
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020058 /* Return control to the setjmp point */
59 longjmp(myerr->setjmp_buffer, 1);
416e3a82 dan1999-01-25 19:06:50 +000060}
61
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020062static Bool canLoad(RContext * rc)
416e3a82 dan1999-01-25 19:06:50 +000063{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020064 if (rc->depth != 16 || rc->vclass != TrueColor
65 || rc->red_offset != 11 || rc->green_offset != 5 || rc->blue_offset != 0)
66 return False;
416e3a82 dan1999-01-25 19:06:50 +000067
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020068 return True;
416e3a82 dan1999-01-25 19:06:50 +000069}
70
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020071static void readData(RContext * rc, struct jpeg_decompress_struct *cinfo, JSAMPROW * buffer, RXImage * ximg)
416e3a82 dan1999-01-25 19:06:50 +000072{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020073 int i, j;
74 unsigned long pixel;
75 int y = 0;
416e3a82 dan1999-01-25 19:06:50 +000076
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020077 /* for 16bpp only */
78 while (cinfo->output_scanline < cinfo->output_height) {
416e3a82 dan1999-01-25 19:06:50 +000079
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020080 jpeg_read_scanlines(cinfo, buffer, (JDIMENSION) 1);
416e3a82 dan1999-01-25 19:06:50 +000081
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020082 if (cinfo->out_color_space == JCS_RGB) {
83 for (i = 0, j = 0; i < cinfo->image_width; i++) {
6830b057 dan2004-10-12 21:28:27 +000084
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020085 printf("%i %i %i\n",
86 (((unsigned long)buffer[0][j]) & 0xf8) << 8,
87 (((unsigned long)buffer[0][j + 1]) & 0xf4) << 3,
88 (((unsigned long)buffer[0][j + 2])) >> 3);
6830b057 dan2004-10-12 21:28:27 +000089
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020090 pixel = (((unsigned long)buffer[0][j++]) & 0xf8) << 8
91 | (((unsigned long)buffer[0][j++]) & 0xf4) << 3
92 | (((unsigned long)buffer[0][j++])) >> 3;
6830b057 dan2004-10-12 21:28:27 +000093
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020094 XPutPixel(ximg->image, i, y, pixel);
95 }
96 } else {
97 for (i = 0, j = 0; i < cinfo->image_width; i++, j++) {
6830b057 dan2004-10-12 21:28:27 +000098
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020099 pixel = (unsigned long)buffer[0][j] << 8
100 | (unsigned long)buffer[0][j] << 3 | (unsigned long)buffer[0][j] >> 3;
6830b057 dan2004-10-12 21:28:27 +0000101
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200102 XPutPixel(ximg->image, i, y, pixel);
103 }
104 }
105 y++;
106 }
416e3a82 dan1999-01-25 19:06:50 +0000107}
108
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200109Pixmap LoadJPEG(RContext * rc, char *file_name, int *width, int *height)
416e3a82 dan1999-01-25 19:06:50 +0000110{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200111 struct jpeg_decompress_struct cinfo;
112 JSAMPROW buffer[1];
113 FILE *file;
114 struct my_error_mgr jerr;
115 RXImage *ximg = NULL;
116 unsigned char buf[8];
117 Pixmap p = None;
118
119 if (!canLoad(rc))
120 return None;
121
122 file = fopen(file_name, "rb");
123 if (!file) {
124 return None;
125 }
126 if (fread(buf, 2, 1, file) != 1) {
127 fclose(file);
128 return None;
129 }
130 if (buf[0] != 0xff || buf[1] != 0xd8) {
131 fclose(file);
132 return None;
133 }
134 rewind(file);
135
136 cinfo.err = jpeg_std_error(&jerr.pub);
137 jerr.pub.error_exit = my_error_exit;
138 /* Establish the setjmp return context for my_error_exit to use. */
139 if (setjmp(jerr.setjmp_buffer)) {
140 /* If we get here, the JPEG code has signaled an error.
141 * We need to clean up the JPEG object, close the input file, and return.
142 */
143 jpeg_destroy_decompress(&cinfo);
144 fclose(file);
145
146 if (ximg) {
147 RDestroyXImage(rc, ximg);
148 }
149
150 return None;
151 }
152
153 jpeg_create_decompress(&cinfo);
154
155 jpeg_stdio_src(&cinfo, file);
156
157 jpeg_read_header(&cinfo, TRUE);
158
159 buffer[0] = (JSAMPROW) malloc(cinfo.image_width * cinfo.num_components);
160 if (!buffer[0]) {
161 RErrorCode = RERR_NOMEMORY;
162 goto bye;
163 }
164
165 if (cinfo.jpeg_color_space == JCS_GRAYSCALE) {
166 cinfo.out_color_space = JCS_GRAYSCALE;
167 } else
168 cinfo.out_color_space = JCS_RGB;
169 cinfo.quantize_colors = FALSE;
170 cinfo.do_fancy_upsampling = FALSE;
171 cinfo.do_block_smoothing = FALSE;
172 jpeg_calc_output_dimensions(&cinfo);
173
174 ximg = RCreateXImage(rc, rc->depth, cinfo.image_width, cinfo.image_height);
175 if (!ximg) {
176 goto bye;
177 }
178 jpeg_start_decompress(&cinfo);
179
180 readData(rc, &cinfo, buffer, ximg);
181
182 jpeg_finish_decompress(&cinfo);
183
184 p = XCreatePixmap(rc->dpy, rc->drawable, cinfo.image_width, cinfo.image_height, rc->depth);
185
186 RPutXImage(rc, p, rc->copy_gc, ximg, 0, 0, 0, 0, cinfo.image_width, cinfo.image_height);
187
188 *width = cinfo.image_width;
189 *height = cinfo.image_height;
190
191 bye:
192 jpeg_destroy_decompress(&cinfo);
193
194 fclose(file);
195
196 if (buffer[0])
197 free(buffer[0]);
198
199 if (ximg)
200 RDestroyXImage(rc, ximg);
201
202 return p;
416e3a82 dan1999-01-25 19:06:50 +0000203}
204
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200205#endif /* USE_JPEG */