CTWM
Loading...
Searching...
No Matches
/usr/src/RPM/BUILD/ctwm-4.1.0/image_jpeg.c
Go to the documentation of this file.
1/*
2 * JPEG image handling functions
3 */
4
5#include "ctwm.h"
6
7#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
10#include <stdint.h>
11
12#include "screen.h"
13#include "image.h"
14#include "image_jpeg.h"
15
16/* Bits needed for libjpeg and interaction */
17#include <setjmp.h>
18#include <jpeglib.h>
19#include <jerror.h>
20
21#include <X11/Xlib.h>
22
23
24/* Various internal bits */
25static Image *LoadJpegImage(const char *name);
26static Image *LoadJpegImageCp(const char *name, ColorPair cp);
27static void convert_for_16(int w, int x, int y, int r, int g, int b);
28static void convert_for_32(int w, int x, int y, int r, int g, int b);
30
35
36typedef struct jpeg_error *jerr_ptr;
37
40
41
42/*
43 * External entry point
44 */
45Image *
46GetJpegImage(const char *name)
47{
48 ColorPair dummy = {0};
49
50 /* Non-animated */
51 if(! strchr(name, '%')) {
52 return (LoadJpegImage(name));
53 }
54
55 /* Animated */
57}
58
59
60/*
61 * Internal backend func
62 */
63
64/* Trivial thunk for get_image_anim_cp() callback */
65static Image *
66LoadJpegImageCp(const char *name, ColorPair cp)
67{
68 return LoadJpegImage(name);
69}
70
71/* The actual loader */
72static Image *
73LoadJpegImage(const char *name)
74{
75 char *fullname;
77 FILE *infile;
78 Image *image;
80 void (*store_data)(int w, int x, int y, int r, int g, int b);
82 struct jpeg_error jerr;
84 int width, height;
85 int row_stride;
86 int g, i, a;
87 int bpix;
88 GC gc;
89
91 if(! fullname) {
92 return NULL;
93 }
94
95 image = AllocImage();
96 if(image == NULL) {
98 return NULL;
99 }
100
101 if((infile = fopen(fullname, "rb")) == NULL) {
103 fprintf(stderr, "unable to locate %s\n", fullname);
104 }
105 fflush(stdout);
106 free(image);
107 free(fullname);
108 return NULL;
109 }
110 free(fullname);
111 cinfo.err = jpeg_std_error(&jerr.pub);
112 jerr.pub.error_exit = jpeg_error_exit;
113
114 if(sigsetjmp(jerr.setjmp_buffer, 1)) {
116 free(image);
117 fclose(infile);
118 return NULL;
119 }
123 cinfo.do_fancy_upsampling = FALSE;
124 cinfo.do_block_smoothing = FALSE;
126 width = cinfo.output_width;
127 height = cinfo.output_height;
128
129 if(Scr->d_depth == 16) {
131 buffer_16bpp = malloc((width) * (height) * 2);
133 (char *) buffer_16bpp, width, height, 16, width * 2);
134 }
135 else if(Scr->d_depth == 24 || Scr->d_depth == 32) {
137 buffer_32bpp = malloc(width * height * 4);
139 (char *) buffer_32bpp, width, height, 32, width * 4);
140 }
141 else {
142 fprintf(stderr, "Image %s unsupported depth : %d\n", name, Scr->d_depth);
143 free(image);
144 fclose(infile);
145 return NULL;
146 }
147 if(ximage == NULL) {
148 fprintf(stderr, "cannot create image for %s\n", name);
149 free(image);
150 fclose(infile);
151 return NULL;
152 }
153 g = 0;
154 row_stride = cinfo.output_width * cinfo.output_components;
155 buffer = (*cinfo.mem->alloc_sarray)
157
158 bpix = cinfo.output_components;
159 while(cinfo.output_scanline < cinfo.output_height) {
161 a = 0;
162 for(i = 0; i < bpix * cinfo.output_width; i += bpix) {
163 (*store_data)(width, a, g, buffer[0][i], buffer[0][i + 1], buffer[0][i + 2]);
164 a++;
165 }
166 g++;
167 }
170 fclose(infile);
171
172 gc = DefaultGC(dpy, Scr->screen);
173 if((width > (Scr->rootw / 2)) || (height > (Scr->rooth / 2))) {
174 int x, y;
175
176 pixret = XCreatePixmap(dpy, Scr->Root, Scr->rootw, Scr->rooth, Scr->d_depth);
177 x = (Scr->rootw - width) / 2;
178 y = (Scr->rooth - height) / 2;
179 XFillRectangle(dpy, pixret, gc, 0, 0, Scr->rootw, Scr->rooth);
180 XPutImage(dpy, pixret, gc, ximage, 0, 0, x, y, width, height);
181 image->width = Scr->rootw;
182 image->height = Scr->rooth;
183 }
184 else {
185 pixret = XCreatePixmap(dpy, Scr->Root, width, height, Scr->d_depth);
186 XPutImage(dpy, pixret, gc, ximage, 0, 0, 0, 0, width, height);
187 image->width = width;
188 image->height = height;
189 }
190 if(ximage) {
192 }
193 image->pixmap = pixret;
194
195 return image;
196}
197
198
199
200/*
201 * Utils
202 */
203static void
204convert_for_16(int w, int x, int y, int r, int g, int b)
205{
206 buffer_16bpp [y * w + x] = ((r >> 3) << 11) + ((g >> 2) << 5) + (b >> 3);
207}
208
209static void
210convert_for_32(int w, int x, int y, int r, int g, int b)
211{
212 buffer_32bpp [y * w + x] = ((r << 16) + (g << 8) + b) & 0xFFFFFFFF;
213}
214
215static void
217{
218 jerr_ptr errmgr = (jerr_ptr) cinfo->err;
219 cinfo->err->output_message(cinfo);
220 siglongjmp(errmgr->setjmp_buffer, 1);
221 return;
222}
static int PlaceX
Definition add_window.c:82
Display * dpy
Definition ctwm_main.c:84
#define Scr
bool reportfilenotfound
Definition image.c:29
Image * get_image_anim_cp(const char *name, ColorPair cp, Image *(*imgloader)(const char *, ColorPair))
Definition image.c:277
Image * AllocImage(void)
Definition image.c:158
char * ExpandPixmapPath(const char *name)
Definition image.c:208
static uint16_t * buffer_16bpp
Definition image_jpeg.c:38
Image * GetJpegImage(const char *name)
Definition image_jpeg.c:46
static void jpeg_error_exit(j_common_ptr cinfo)
Definition image_jpeg.c:216
static Image * LoadJpegImage(const char *name)
Definition image_jpeg.c:73
static void convert_for_32(int w, int x, int y, int r, int g, int b)
Definition image_jpeg.c:210
static uint32_t * buffer_32bpp
Definition image_jpeg.c:39
static Image * LoadJpegImageCp(const char *name, ColorPair cp)
Definition image_jpeg.c:66
static void convert_for_16(int w, int x, int y, int r, int g, int b)
Definition image_jpeg.c:204
struct jpeg_error * jerr_ptr
Definition image_jpeg.c:36
int y
Definition menus.c:70
int x
Definition menus.c:69
Definition image.h:9
int height
Definition image.h:13
Pixmap pixmap
Definition image.h:10
int width
Definition image.h:12
sigjmp_buf setjmp_buffer
Definition image_jpeg.c:33
struct jpeg_error_mgr pub
Definition image_jpeg.c:32