| 1 |
/* libjpeg wrapper for Allegro 5 iio addon. |
| 2 |
* by Elias Pschernig |
| 3 |
*/ |
| 4 |
|
| 5 |
#include <stdio.h> |
| 6 |
#include <stdlib.h> |
| 7 |
#include <setjmp.h> |
| 8 |
#ifdef ALLEGRO_HAVE_STDINT_H |
| 9 |
#include <stdint.h> |
| 10 |
#endif |
| 11 |
|
| 12 |
#define BUFFER_SIZE 4096 |
| 13 |
|
| 14 |
#include "allegro5/allegro.h" |
| 15 |
#include "allegro5/allegro_image.h" |
| 16 |
#include "allegro5/internal/aintern_image.h" |
| 17 |
|
| 18 |
#include "iio.h" |
| 19 |
|
| 20 |
#include <jpeglib.h> |
| 21 |
#include <jerror.h> |
| 22 |
|
| 23 |
ALLEGRO_DEBUG_CHANNEL("image") |
| 24 |
|
| 25 |
|
| 26 |
struct my_src_mgr |
| 27 |
{ |
| 28 |
struct jpeg_source_mgr pub; |
| 29 |
JOCTET *buffer; |
| 30 |
ALLEGRO_FILE *fp; |
| 31 |
}; |
| 32 |
|
| 33 |
struct my_dest_mgr |
| 34 |
{ |
| 35 |
struct jpeg_destination_mgr pub; |
| 36 |
JOCTET *buffer; |
| 37 |
ALLEGRO_FILE *fp; |
| 38 |
}; |
| 39 |
|
| 40 |
struct my_err_mgr |
| 41 |
{ |
| 42 |
struct jpeg_error_mgr pub; |
| 43 |
jmp_buf jmpenv; |
| 44 |
}; |
| 45 |
|
| 46 |
static void init_source(j_decompress_ptr cinfo) |
| 47 |
{ |
| 48 |
(void)cinfo; |
| 49 |
} |
| 50 |
|
| 51 |
static void init_destination(j_compress_ptr cinfo) |
| 52 |
{ |
| 53 |
struct my_dest_mgr *dest = (void *)cinfo->dest; |
| 54 |
dest->pub.next_output_byte = dest->buffer; |
| 55 |
dest->pub.free_in_buffer = BUFFER_SIZE; |
| 56 |
} |
| 57 |
|
| 58 |
static boolean fill_input_buffer(j_decompress_ptr cinfo) |
| 59 |
{ |
| 60 |
struct my_src_mgr *src = (void *)cinfo->src; |
| 61 |
src->pub.next_input_byte = src->buffer; |
| 62 |
src->pub.bytes_in_buffer = al_fread(src->fp, src->buffer, BUFFER_SIZE); |
| 63 |
return 1; |
| 64 |
} |
| 65 |
|
| 66 |
static boolean empty_output_buffer(j_compress_ptr cinfo) |
| 67 |
{ |
| 68 |
struct my_dest_mgr *dest = (void *)cinfo->dest; |
| 69 |
al_fwrite(dest->fp, dest->buffer, BUFFER_SIZE); |
| 70 |
dest->pub.next_output_byte = dest->buffer; |
| 71 |
dest->pub.free_in_buffer = BUFFER_SIZE; |
| 72 |
return 1; |
| 73 |
} |
| 74 |
|
| 75 |
static void skip_input_data(j_decompress_ptr cinfo, long num_bytes) |
| 76 |
{ |
| 77 |
struct my_src_mgr *src = (void *)cinfo->src; |
| 78 |
if (num_bytes <= (long)src->pub.bytes_in_buffer) { |
| 79 |
src->pub.next_input_byte += num_bytes; |
| 80 |
src->pub.bytes_in_buffer -= num_bytes; |
| 81 |
} |
| 82 |
else { |
| 83 |
long skip = num_bytes - src->pub.bytes_in_buffer; |
| 84 |
al_fseek(src->fp, skip, ALLEGRO_SEEK_CUR); |
| 85 |
src->pub.bytes_in_buffer = 0; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
static void term_source(j_decompress_ptr cinfo) |
| 90 |
{ |
| 91 |
(void)cinfo; |
| 92 |
} |
| 93 |
|
| 94 |
static void term_destination(j_compress_ptr cinfo) |
| 95 |
{ |
| 96 |
struct my_dest_mgr *dest = (void *)cinfo->dest; |
| 97 |
al_fwrite(dest->fp, dest->buffer, BUFFER_SIZE - dest->pub.free_in_buffer); |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
static void jpeg_packfile_src(j_decompress_ptr cinfo, ALLEGRO_FILE *fp, |
| 102 |
JOCTET *buffer) |
| 103 |
{ |
| 104 |
struct my_src_mgr *src; |
| 105 |
|
| 106 |
if (!cinfo->src) |
| 107 |
cinfo->src = |
| 108 |
(*cinfo->mem->alloc_small) ((void *)cinfo, JPOOL_PERMANENT, |
| 109 |
sizeof *src); |
| 110 |
|
| 111 |
src = (void *)cinfo->src; |
| 112 |
src->pub.init_source = init_source; |
| 113 |
src->pub.fill_input_buffer = fill_input_buffer; |
| 114 |
src->pub.skip_input_data = skip_input_data; |
| 115 |
src->pub.resync_to_restart = jpeg_resync_to_restart; |
| 116 |
src->pub.term_source = term_source; |
| 117 |
src->pub.bytes_in_buffer = 0; |
| 118 |
src->buffer = buffer; |
| 119 |
src->fp = fp; |
| 120 |
} |
| 121 |
|
| 122 |
static void jpeg_packfile_dest(j_compress_ptr cinfo, ALLEGRO_FILE *fp, |
| 123 |
JOCTET *buffer) |
| 124 |
{ |
| 125 |
struct my_dest_mgr *dest; |
| 126 |
|
| 127 |
if (!cinfo->dest) |
| 128 |
cinfo->dest = |
| 129 |
(*cinfo->mem->alloc_small) ((void *)cinfo, JPOOL_PERMANENT, |
| 130 |
sizeof *dest); |
| 131 |
|
| 132 |
dest = (void *)cinfo->dest; |
| 133 |
dest->pub.init_destination = init_destination; |
| 134 |
dest->pub.empty_output_buffer = empty_output_buffer; |
| 135 |
dest->pub.term_destination = term_destination; |
| 136 |
dest->pub.free_in_buffer = 0; |
| 137 |
dest->buffer = buffer; |
| 138 |
dest->fp = fp; |
| 139 |
} |
| 140 |
|
| 141 |
static void my_error_exit(j_common_ptr cinfo) |
| 142 |
{ |
| 143 |
char buffer[JMSG_LENGTH_MAX]; |
| 144 |
struct my_err_mgr *jerr = (void *)cinfo->err; |
| 145 |
jerr->pub.format_message(cinfo, buffer); |
| 146 |
ALLEGRO_ERROR("jpeg error: %s\n", buffer); |
| 147 |
longjmp(jerr->jmpenv, 1); |
| 148 |
} |
| 149 |
|
| 150 |
/* We keep data for load_jpg_entry_helper in a structure allocated in the |
| 151 |
* caller's stack frame to avoid problems with automatic variables being |
| 152 |
* undefined after a longjmp. |
| 153 |
*/ |
| 154 |
struct load_jpg_entry_helper_data { |
| 155 |
bool error; |
| 156 |
ALLEGRO_BITMAP *bmp; |
| 157 |
JOCTET *buffer; |
| 158 |
unsigned char *row; |
| 159 |
}; |
| 160 |
|
| 161 |
static void load_jpg_entry_helper(ALLEGRO_FILE *fp, |
| 162 |
struct load_jpg_entry_helper_data *data, int flags) |
| 163 |
{ |
| 164 |
struct jpeg_decompress_struct cinfo; |
| 165 |
struct my_err_mgr jerr; |
| 166 |
ALLEGRO_LOCKED_REGION *lock; |
| 167 |
int w, h, s; |
| 168 |
|
| 169 |
/* ALLEGRO_NO_PREMULTIPLIED_ALPHA does not apply. |
| 170 |
* ALLEGRO_KEEP_INDEX does not apply. |
| 171 |
*/ |
| 172 |
(void)flags; |
| 173 |
|
| 174 |
data->error = false; |
| 175 |
|
| 176 |
cinfo.err = jpeg_std_error(&jerr.pub); |
| 177 |
jerr.pub.error_exit = my_error_exit; |
| 178 |
if (setjmp(jerr.jmpenv) != 0) { |
| 179 |
/* Longjmp'd. */ |
| 180 |
data->error = true; |
| 181 |
goto longjmp_error; |
| 182 |
} |
| 183 |
|
| 184 |
data->buffer = al_malloc(BUFFER_SIZE); |
| 185 |
if (!data->buffer) { |
| 186 |
data->error = true; |
| 187 |
goto error; |
| 188 |
} |
| 189 |
|
| 190 |
jpeg_create_decompress(&cinfo); |
| 191 |
jpeg_packfile_src(&cinfo, fp, data->buffer); |
| 192 |
jpeg_read_header(&cinfo, true); |
| 193 |
jpeg_start_decompress(&cinfo); |
| 194 |
|
| 195 |
w = cinfo.output_width; |
| 196 |
h = cinfo.output_height; |
| 197 |
s = cinfo.output_components; |
| 198 |
|
| 199 |
/* Only one and three components make sense in a JPG file. */ |
| 200 |
if (s != 1 && s != 3) { |
| 201 |
data->error = true; |
| 202 |
ALLEGRO_ERROR("%d components makes no sense\n", s); |
| 203 |
goto error; |
| 204 |
} |
| 205 |
|
| 206 |
data->bmp = al_create_bitmap(w, h); |
| 207 |
if (!data->bmp) { |
| 208 |
data->error = true; |
| 209 |
ALLEGRO_ERROR("%dx%d bitmap creation failed\n", w, h); |
| 210 |
goto error; |
| 211 |
} |
| 212 |
|
| 213 |
/* Allegro's pixel format is endian independent, so that in |
| 214 |
* ALLEGRO_PIXEL_FORMAT_RGB_888 the lower 8 bits always hold the Blue |
| 215 |
* component. On a little endian system this is in byte 0. On a big |
| 216 |
* endian system this is in byte 2. |
| 217 |
* |
| 218 |
* libjpeg expects byte 0 to hold the Red component, byte 1 to hold the |
| 219 |
* Green component, byte 2 to hold the Blue component. Hence on little |
| 220 |
* endian systems we need the opposite format, ALLEGRO_PIXEL_FORMAT_BGR_888. |
| 221 |
*/ |
| 222 |
#ifdef ALLEGRO_BIG_ENDIAN |
| 223 |
lock = al_lock_bitmap(data->bmp, ALLEGRO_PIXEL_FORMAT_RGB_888, |
| 224 |
ALLEGRO_LOCK_WRITEONLY); |
| 225 |
#else |
| 226 |
lock = al_lock_bitmap(data->bmp, ALLEGRO_PIXEL_FORMAT_BGR_888, |
| 227 |
ALLEGRO_LOCK_WRITEONLY); |
| 228 |
#endif |
| 229 |
|
| 230 |
if (s == 3) { |
| 231 |
/* Colour. */ |
| 232 |
int y; |
| 233 |
|
| 234 |
for (y = cinfo.output_scanline; y < h; y = cinfo.output_scanline) { |
| 235 |
unsigned char *out[1]; |
| 236 |
out[0] = ((unsigned char *)lock->data) + y * lock->pitch; |
| 237 |
jpeg_read_scanlines(&cinfo, (void *)out, 1); |
| 238 |
} |
| 239 |
} |
| 240 |
else if (s == 1) { |
| 241 |
/* Greyscale. */ |
| 242 |
unsigned char *in; |
| 243 |
unsigned char *out; |
| 244 |
int x, y; |
| 245 |
|
| 246 |
data->row = al_malloc(w); |
| 247 |
for (y = cinfo.output_scanline; y < h; y = cinfo.output_scanline) { |
| 248 |
jpeg_read_scanlines(&cinfo, (void *)&data->row, 1); |
| 249 |
in = data->row; |
| 250 |
out = ((unsigned char *)lock->data) + y * lock->pitch; |
| 251 |
for (x = 0; x < w; x++) { |
| 252 |
*out++ = *in; |
| 253 |
*out++ = *in; |
| 254 |
*out++ = *in; |
| 255 |
in++; |
| 256 |
} |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
error: |
| 261 |
jpeg_finish_decompress(&cinfo); |
| 262 |
|
| 263 |
longjmp_error: |
| 264 |
jpeg_destroy_decompress(&cinfo); |
| 265 |
|
| 266 |
if (data->bmp) { |
| 267 |
if (al_is_bitmap_locked(data->bmp)) { |
| 268 |
al_unlock_bitmap(data->bmp); |
| 269 |
} |
| 270 |
if (data->error) { |
| 271 |
al_destroy_bitmap(data->bmp); |
| 272 |
data->bmp = NULL; |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
al_free(data->buffer); |
| 277 |
al_free(data->row); |
| 278 |
} |
| 279 |
|
| 280 |
ALLEGRO_BITMAP *_al_load_jpg_f(ALLEGRO_FILE *fp, int flags) |
| 281 |
{ |
| 282 |
struct load_jpg_entry_helper_data data; |
| 283 |
|
| 284 |
memset(&data, 0, sizeof(data)); |
| 285 |
load_jpg_entry_helper(fp, &data, flags); |
| 286 |
|
| 287 |
return data.bmp; |
| 288 |
} |
| 289 |
|
| 290 |
/* See comment about load_jpg_entry_helper_data. */ |
| 291 |
struct save_jpg_entry_helper_data { |
| 292 |
bool error; |
| 293 |
JOCTET *buffer; |
| 294 |
}; |
| 295 |
|
| 296 |
static void save_jpg_entry_helper(ALLEGRO_FILE *fp, ALLEGRO_BITMAP *bmp, |
| 297 |
struct save_jpg_entry_helper_data *data) |
| 298 |
{ |
| 299 |
struct jpeg_compress_struct cinfo; |
| 300 |
struct my_err_mgr jerr; |
| 301 |
ALLEGRO_LOCKED_REGION *lock; |
| 302 |
|
| 303 |
data->error = false; |
| 304 |
|
| 305 |
cinfo.err = jpeg_std_error(&jerr.pub); |
| 306 |
jerr.pub.error_exit = my_error_exit; |
| 307 |
if (setjmp(jerr.jmpenv)) { |
| 308 |
/* Longjmp'd. */ |
| 309 |
data->error = true; |
| 310 |
goto longjmp_error; |
| 311 |
} |
| 312 |
|
| 313 |
data->buffer = al_malloc(BUFFER_SIZE); |
| 314 |
if (!data->buffer) { |
| 315 |
data->error = true; |
| 316 |
goto error; |
| 317 |
} |
| 318 |
|
| 319 |
jpeg_create_compress(&cinfo); |
| 320 |
jpeg_packfile_dest(&cinfo, fp, data->buffer); |
| 321 |
|
| 322 |
cinfo.image_width = al_get_bitmap_width(bmp); |
| 323 |
cinfo.image_height = al_get_bitmap_height(bmp); |
| 324 |
cinfo.input_components = 3; |
| 325 |
cinfo.in_color_space = JCS_RGB; |
| 326 |
jpeg_set_defaults(&cinfo); |
| 327 |
|
| 328 |
const char* level = al_get_config_value(al_get_system_config(), "image", "jpeg_quality_level"); |
| 329 |
jpeg_set_quality(&cinfo, level ? strtol(level, NULL, 10) : 75, true); |
| 330 |
|
| 331 |
jpeg_start_compress(&cinfo, 1); |
| 332 |
|
| 333 |
/* See comment in load_jpg_entry_helper. */ |
| 334 |
#ifdef ALLEGRO_BIG_ENDIAN |
| 335 |
lock = al_lock_bitmap(bmp, ALLEGRO_PIXEL_FORMAT_RGB_888, |
| 336 |
ALLEGRO_LOCK_READONLY); |
| 337 |
#else |
| 338 |
lock = al_lock_bitmap(bmp, ALLEGRO_PIXEL_FORMAT_BGR_888, |
| 339 |
ALLEGRO_LOCK_READONLY); |
| 340 |
#endif |
| 341 |
|
| 342 |
while (cinfo.next_scanline < cinfo.image_height) { |
| 343 |
unsigned char *row[1]; |
| 344 |
row[0] = ((unsigned char *)lock->data) |
| 345 |
+ (signed)cinfo.next_scanline * lock->pitch; |
| 346 |
jpeg_write_scanlines(&cinfo, (void *)row, 1); |
| 347 |
} |
| 348 |
|
| 349 |
error: |
| 350 |
jpeg_finish_compress(&cinfo); |
| 351 |
|
| 352 |
longjmp_error: |
| 353 |
jpeg_destroy_compress(&cinfo); |
| 354 |
|
| 355 |
if (al_is_bitmap_locked(bmp)) { |
| 356 |
al_unlock_bitmap(bmp); |
| 357 |
} |
| 358 |
|
| 359 |
al_free(data->buffer); |
| 360 |
} |
| 361 |
|
| 362 |
bool _al_save_jpg_f(ALLEGRO_FILE *fp, ALLEGRO_BITMAP *bmp) |
| 363 |
{ |
| 364 |
struct save_jpg_entry_helper_data data; |
| 365 |
|
| 366 |
memset(&data, 0, sizeof(data)); |
| 367 |
save_jpg_entry_helper(fp, bmp, &data); |
| 368 |
|
| 369 |
return !data.error; |
| 370 |
} |
| 371 |
|
| 372 |
ALLEGRO_BITMAP *_al_load_jpg(char const *filename, int flags) |
| 373 |
{ |
| 374 |
ALLEGRO_FILE *fp; |
| 375 |
ALLEGRO_BITMAP *bmp; |
| 376 |
|
| 377 |
ALLEGRO_ASSERT(filename); |
| 378 |
|
| 379 |
fp = al_fopen(filename, "rb"); |
| 380 |
if (!fp) { |
| 381 |
ALLEGRO_ERROR("Unable to open %s for reading.\n", filename); |
| 382 |
return NULL; |
| 383 |
} |
| 384 |
|
| 385 |
bmp = _al_load_jpg_f(fp, flags); |
| 386 |
|
| 387 |
al_fclose(fp); |
| 388 |
|
| 389 |
return bmp; |
| 390 |
} |
| 391 |
|
| 392 |
bool _al_save_jpg(char const *filename, ALLEGRO_BITMAP *bmp) |
| 393 |
{ |
| 394 |
ALLEGRO_FILE *fp; |
| 395 |
bool retsave; |
| 396 |
bool retclose; |
| 397 |
|
| 398 |
ALLEGRO_ASSERT(filename); |
| 399 |
ALLEGRO_ASSERT(bmp); |
| 400 |
|
| 401 |
fp = al_fopen(filename, "wb"); |
| 402 |
if (!fp) { |
| 403 |
ALLEGRO_ERROR("Unable to open file %s for writing\n", filename); |
| 404 |
return false; |
| 405 |
} |
| 406 |
|
| 407 |
retsave = _al_save_jpg_f(fp, bmp); |
| 408 |
|
| 409 |
retclose = al_fclose(fp); |
| 410 |
|
| 411 |
return retsave && retclose; |
| 412 |
} |
| 413 |
|
| 414 |
/* vim: set sts=3 sw=3 et: */ |