| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
#include "sf_frotz.h" |
| 23 |
|
| 24 |
#include <stdarg.h> |
| 25 |
#include <stddef.h> |
| 26 |
#include <stdio.h> |
| 27 |
#include <stdlib.h> |
| 28 |
#include <string.h> |
| 29 |
|
| 30 |
#include <math.h> |
| 31 |
|
| 32 |
#include "png.h" |
| 33 |
#include <setjmp.h> |
| 34 |
|
| 35 |
#include "../blorb/blorblow.h" |
| 36 |
|
| 37 |
static zbyte toLinear[256]; |
| 38 |
static zbyte fromLinear[256]; |
| •39 |
extern int m_adaptiveMode; |
| 40 |
|
| 41 |
zlong sf_blend(int a, zlong s, zlong d) |
| 42 |
{ |
| 43 |
zlong r; |
| 44 |
r = fromLinear[(toLinear[s & 0xff] * a + |
| 45 |
toLinear[d & 0xff] * (256 - a)) >> 8]; |
| 46 |
s >>= 8; |
| 47 |
d >>= 8; |
| 48 |
r |= (fromLinear |
| 49 |
[(toLinear[s & 0xff] * a + |
| 50 |
toLinear[d & 0xff] * (256 - a)) >> 8]) << 8; |
| 51 |
s >>= 8; |
| 52 |
d >>= 8; |
| 53 |
r |= (fromLinear |
| 54 |
[(toLinear[s & 0xff] * a + |
| 55 |
toLinear[d & 0xff] * (256 - a)) >> 8]) << 16; |
| 56 |
return r; |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
void sf_setgamma(double gamma) |
| 62 |
{ |
| 63 |
int i; |
| 64 |
|
| 65 |
m_gamma = gamma; |
| 66 |
for (i = 0; i < 256; i++) |
| 67 |
toLinear[i] = (int)((pow(i / 255.0, gamma) * 255.0) + 0.5); |
| 68 |
gamma = 1.0 / gamma; |
| 69 |
for (i = 0; i < 256; i++) |
| 70 |
fromLinear[i] = (int)((pow(i / 255.0, gamma) * 255.0) + 0.5); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
typedef struct { |
| 79 |
zbyte *gfxData; |
| 80 |
zlong offset; |
| 81 |
} PNGData; |
| 82 |
|
| 83 |
|
| 84 |
static void readPNGData(png_structp png_ptr, png_bytep data, png_size_t length) |
| 85 |
{ |
| 86 |
PNGData *pngData = (PNGData *) png_get_io_ptr(png_ptr); |
| 87 |
memmove(data, pngData->gfxData + pngData->offset, length); |
| 88 |
pngData->offset += length; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
static int loadpng(zbyte * data, int UNUSED (length), sf_picture * graphic) |
| 93 |
{ |
| 94 |
png_bytep *rowPointers = NULL; |
| 95 |
png_structp png_ptr = NULL; |
| 96 |
png_infop info_ptr = NULL; |
| 97 |
png_infop end_info = NULL; |
| 98 |
PNGData pngData; |
| 99 |
png_uint_32 width, height; |
| 100 |
int color_type, size, bit_depth; |
| 101 |
double gamma; |
| 102 |
|
| 103 |
graphic->pixels = NULL; |
| 104 |
graphic->width = graphic->height = 0; |
| 105 |
|
| 106 |
if (!png_check_sig(data, 8)) |
| 107 |
return 0; |
| 108 |
|
| 109 |
png_ptr = png_create_read_struct |
| 110 |
(PNG_LIBPNG_VER_STRING, (png_voidp) NULL, NULL, NULL); |
| 111 |
if (!png_ptr) |
| 112 |
return 0; |
| 113 |
|
| 114 |
info_ptr = png_create_info_struct(png_ptr); |
| 115 |
if (!info_ptr) { |
| 116 |
png_destroy_read_struct(&png_ptr, |
| 117 |
(png_infopp) NULL, (png_infopp) NULL); |
| 118 |
return 0; |
| 119 |
} |
| 120 |
|
| 121 |
end_info = png_create_info_struct(png_ptr); |
| 122 |
if (!end_info) { |
| 123 |
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL); |
| 124 |
return 0; |
| 125 |
} |
| 126 |
|
| 127 |
if (setjmp(png_jmpbuf(png_ptr))) { |
| 128 |
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); |
| 129 |
if (rowPointers) |
| 130 |
free(rowPointers); |
| 131 |
if (graphic->pixels) { |
| 132 |
free(graphic->pixels); |
| 133 |
graphic->pixels = NULL; |
| 134 |
} |
| 135 |
return 0; |
| 136 |
} |
| 137 |
|
| 138 |
pngData.gfxData = data; |
| 139 |
pngData.offset = 8; |
| 140 |
png_set_read_fn(png_ptr, &pngData, readPNGData); |
| 141 |
|
| 142 |
png_set_sig_bytes(png_ptr, 8); |
| 143 |
png_read_info(png_ptr, info_ptr); |
| 144 |
|
| 145 |
width = png_get_image_width(png_ptr, info_ptr); |
| 146 |
height = png_get_image_height(png_ptr, info_ptr); |
| 147 |
bit_depth = png_get_bit_depth(png_ptr,info_ptr); |
| 148 |
color_type = png_get_color_type(png_ptr, info_ptr); |
| 149 |
|
| 150 |
graphic->width = width; |
| 151 |
graphic->height = height; |
| 152 |
graphic->usespalette = FALSE; |
| 153 |
|
| 154 |
if (png_get_gAMA(png_ptr, info_ptr, &gamma)) |
| 155 |
png_set_gamma(png_ptr, m_gamma, gamma); |
| 156 |
|
| 157 |
if (m_adaptiveMode && (color_type == PNG_COLOR_TYPE_PALETTE) && bit_depth <= 4) { |
| 158 |
graphic->usespalette = TRUE; |
| 159 |
png_set_packing(png_ptr); |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
png_bytep trans; |
| 165 |
int num_trans; |
| 166 |
png_color_16p trans_values; |
| 167 |
|
| 168 |
if (png_get_tRNS |
| 169 |
(png_ptr, info_ptr, &trans, &num_trans, &trans_values) |
| 170 |
&& num_trans >= 1) |
| 171 |
graphic->transparentcolor = trans[0]; |
| 172 |
|
| 173 |
size = width * height; |
| 174 |
graphic->pixels = (zbyte *) malloc(size); |
| 175 |
|
| 176 |
rowPointers = malloc(sizeof(png_bytep) * height); |
| 177 |
for (int i = 0; i < (int)height; i++) |
| 178 |
rowPointers[i] = graphic->pixels + (width * i); |
| 179 |
png_read_image(png_ptr, rowPointers); |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
png_colorp palette; |
| 185 |
int num_palette; |
| 186 |
if (png_get_PLTE(png_ptr, info_ptr, &palette, &num_palette)) { |
| 187 |
graphic->palette_entries = num_palette; |
| 188 |
for (int i = 0; i < num_palette; i++) { |
| 189 |
zlong color = |
| 190 |
palette[i].red | (palette[i]. |
| 191 |
green << 8) | (palette[i]. |
| 192 |
blue << |
| 193 |
16); |
| 194 |
graphic->palette[i] = color; |
| 195 |
} |
| 196 |
} |
| 197 |
} else { |
| 198 |
if (graphic->adaptive) |
| 199 |
os_fatal("Non-paletted graphics cannot be adaptive"); |
| 200 |
if (color_type == PNG_COLOR_TYPE_PALETTE && bit_depth <= 8) |
| 201 |
png_set_palette_to_rgb(png_ptr); |
| 202 |
if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) |
| 203 |
png_set_expand_gray_1_2_4_to_8(png_ptr); |
| 204 |
if (png_get_valid(png_ptr,info_ptr,PNG_INFO_tRNS)) |
| 205 |
png_set_tRNS_to_alpha(png_ptr); |
| 206 |
|
| 207 |
if (bit_depth == 16) |
| 208 |
png_set_strip_16(png_ptr); |
| 209 |
if (bit_depth < 8) |
| 210 |
png_set_packing(png_ptr); |
| 211 |
if (color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA) |
| 212 |
png_set_gray_to_rgb(png_ptr); |
| 213 |
|
| 214 |
png_set_filler(png_ptr,0xff,PNG_FILLER_AFTER); |
| 215 |
size = width*height*4; |
| 216 |
graphic->pixels = (zbyte *) malloc(size); |
| 217 |
rowPointers = malloc(sizeof(png_bytep) * height); |
| 218 |
for (int i = 0; i < (int)height; i++) |
| 219 |
rowPointers[i] = graphic->pixels + (width * i * 4); |
| 220 |
png_read_image(png_ptr, rowPointers); |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
png_read_end(png_ptr, end_info); |
| 225 |
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); |
| 226 |
if (rowPointers) |
| 227 |
free(rowPointers); |
| 228 |
|
| 229 |
return 1; |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
#include <jpeglib.h> |
| 239 |
|
| 240 |
|
| 241 |
struct JPEGErrorInfo { |
| 242 |
struct jpeg_error_mgr base; |
| 243 |
jmp_buf errorJump; |
| 244 |
}; |
| 245 |
|
| 246 |
|
| 247 |
static void errorJPEGExit(j_common_ptr cinfo) |
| 248 |
{ |
| 249 |
struct JPEGErrorInfo *error = (struct JPEGErrorInfo *)cinfo->err; |
| 250 |
(*cinfo->err->output_message) (cinfo); |
| 251 |
longjmp(error->errorJump, 1); |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
static void outputJPEGMessage(j_common_ptr cinfo) |
| 256 |
{ |
| 257 |
char buffer[JMSG_LENGTH_MAX]; |
| 258 |
(*cinfo->err->format_message) (cinfo, buffer); |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
static void memJPEGInit(j_decompress_ptr UNUSED (unused)) |
| 264 |
{ |
| 265 |
|
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
static int memJPEGFillInput(j_decompress_ptr UNUSED (unused)) |
| 270 |
{ |
| 271 |
return 0; |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
static void memJPEGSkipInput(j_decompress_ptr cinfo, long num_bytes) |
| 276 |
{ |
| 277 |
if (num_bytes > 0) { |
| 278 |
if (num_bytes > (long)cinfo->src->bytes_in_buffer) |
| 279 |
num_bytes = (long)cinfo->src->bytes_in_buffer; |
| 280 |
|
| 281 |
cinfo->src->next_input_byte += num_bytes; |
| 282 |
cinfo->src->bytes_in_buffer -= num_bytes; |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
static void memJPEGTerm(j_decompress_ptr UNUSED (unused)) |
| 288 |
{ |
| 289 |
|
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
static int loadjpeg(zbyte * data, int length, sf_picture * graphic) |
| 294 |
{ |
| 295 |
struct jpeg_decompress_struct info; |
| 296 |
struct JPEGErrorInfo error; |
| 297 |
int width, height, size; |
| 298 |
JSAMPARRAY buffer; |
| 299 |
|
| 300 |
graphic->pixels = NULL; |
| 301 |
graphic->width = graphic->height = 0; |
| 302 |
|
| 303 |
info.err = jpeg_std_error(&(error.base)); |
| 304 |
error.base.error_exit = errorJPEGExit; |
| 305 |
error.base.output_message = outputJPEGMessage; |
| 306 |
if (setjmp(error.errorJump)) { |
| 307 |
jpeg_destroy_decompress(&info); |
| 308 |
if (graphic->pixels) { |
| 309 |
free(graphic->pixels); |
| 310 |
graphic->pixels = NULL; |
| 311 |
} |
| 312 |
return 0; |
| 313 |
} |
| 314 |
|
| 315 |
jpeg_create_decompress(&info); |
| 316 |
|
| 317 |
info.src = (struct jpeg_source_mgr *)(info.mem->alloc_small) |
| 318 |
((j_common_ptr) (&info), JPOOL_PERMANENT, |
| 319 |
sizeof(struct jpeg_source_mgr)); |
| 320 |
info.src->init_source = memJPEGInit; |
| 321 |
info.src->fill_input_buffer = memJPEGFillInput; |
| 322 |
info.src->skip_input_data = memJPEGSkipInput; |
| 323 |
info.src->resync_to_restart = jpeg_resync_to_restart; |
| 324 |
info.src->term_source = memJPEGTerm; |
| 325 |
info.src->bytes_in_buffer = length; |
| 326 |
info.src->next_input_byte = data; |
| 327 |
|
| 328 |
jpeg_read_header(&info, TRUE); |
| 329 |
jpeg_calc_output_dimensions(&info); |
| 330 |
width = info.output_width; |
| 331 |
height = info.output_height; |
| 332 |
|
| 333 |
graphic->width = width; |
| 334 |
graphic->height = height; |
| 335 |
size = width * height * 4; |
| 336 |
graphic->pixels = (zbyte *) malloc(size); |
| 337 |
|
| 338 |
|
| 339 |
info.out_color_space = JCS_RGB; |
| 340 |
|
| 341 |
|
| 342 |
buffer = (*info.mem->alloc_sarray) |
| 343 |
((j_common_ptr) & info, JPOOL_IMAGE, width * 3, 1); |
| 344 |
|
| 345 |
jpeg_start_decompress(&info); |
| 346 |
while ((int)info.output_scanline < height) { |
| 347 |
zbyte *pixelRow; |
| 348 |
int i; |
| 349 |
jpeg_read_scanlines(&info, buffer, 1); |
| 350 |
|
| 351 |
pixelRow = graphic->pixels + |
| 352 |
(width * (info.output_scanline - 1) * 4); |
| 353 |
for (i = 0; i < width; i++) { |
| 354 |
pixelRow[(i * 4) + 0] = (*buffer)[(i * 3) + 0]; |
| 355 |
pixelRow[(i * 4) + 1] = (*buffer)[(i * 3) + 1]; |
| 356 |
pixelRow[(i * 4) + 2] = (*buffer)[(i * 3) + 2]; |
| 357 |
pixelRow[(i * 4) + 3] = 0xFF; |
| 358 |
} |
| 359 |
} |
| 360 |
jpeg_finish_decompress(&info); |
| 361 |
jpeg_destroy_decompress(&info); |
| 362 |
|
| 363 |
return 1; |
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
static int loadrect(zbyte * data, int UNUSED (length), sf_picture * graphic) |
| 372 |
{ |
| 373 |
graphic->width = |
| 374 |
(data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3]; |
| 375 |
graphic->height = |
| 376 |
(data[4] << 24) | (data[5] << 16) | (data[6] << 8) | data[7]; |
| 377 |
graphic->pixels = NULL; |
| 378 |
return 1; |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
static int sf_loadpic(int picture, sf_picture * graphic) |
| 385 |
{ |
| 386 |
myresource res; |
| 387 |
int st = 0; |
| 388 |
|
| 389 |
|
| 390 |
graphic->adaptive = sf_IsAdaptive(picture) ? TRUE : FALSE; |
| 391 |
|
| 392 |
if (sf_getresource(picture, 1, bb_method_Memory, &res) == bb_err_None) { |
| 393 |
zbyte *data = (zbyte *) res.bbres.data.ptr; |
| 394 |
int length = res.bbres.length; |
| 395 |
unsigned int id = res.type; |
| 396 |
|
| 397 |
|
| 398 |
if (id == bb_ID_PNG) { |
| 399 |
st = loadpng(data, length, graphic); |
| 400 |
if (!st) |
| 401 |
st = loadjpeg(data, length, graphic); |
| 402 |
} else if (id == bb_ID_JPEG) { |
| 403 |
st = loadjpeg(data, length, graphic); |
| 404 |
if (!st) |
| 405 |
st = loadpng(data, length, graphic); |
| 406 |
} else if (id == bb_ID_Rect) |
| 407 |
st = loadrect(data, length, graphic); |
| 408 |
sf_freeresource(&res); |
| 409 |
} |
| 410 |
|
| 411 |
if (st) |
| 412 |
graphic->number = picture; |
| 413 |
return st; |
| 414 |
} |
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
#define MAXCACHE 1 |
| 421 |
|
| 422 |
static sf_picture cached[MAXCACHE]; |
| 423 |
static int cacheinited = 0; |
| 424 |
|
| 425 |
static void cacheflush(void) |
| 426 |
{ |
| 427 |
int i; |
| 428 |
if (!cacheinited) |
| 429 |
return; |
| 430 |
for (i = 0; i < MAXCACHE; i++) { |
| 431 |
cached[i].number = -1; |
| 432 |
if (cached[i].pixels) |
| 433 |
free(cached[i].pixels); |
| 434 |
cached[i].pixels = NULL; |
| 435 |
} |
| 436 |
cacheinited = 0; |
| 437 |
} |
| 438 |
|
| 439 |
|
| 440 |
static void cacheinit(void) |
| 441 |
{ |
| 442 |
int i; |
| 443 |
if (cacheinited) |
| 444 |
return; |
| 445 |
CLEANREG(cacheflush); |
| 446 |
for (i = 0; i < MAXCACHE; i++) { |
| 447 |
cached[i].number = -1; |
| 448 |
cached[i].pixels = NULL; |
| 449 |
} |
| 450 |
cacheinited = 1; |
| 451 |
} |
| 452 |
|
| 453 |
|
| 454 |
static sf_picture *cachefind(int n) |
| 455 |
{ |
| 456 |
int i; |
| 457 |
for (i = 0; i < MAXCACHE; i++) |
| 458 |
if (cached[i].number == n) |
| 459 |
return (cached + i); |
| 460 |
if (n < 0) { |
| 461 |
cached[0].number = -1; |
| 462 |
if (cached[0].pixels) |
| 463 |
free(cached[0].pixels); |
| 464 |
cached[0].pixels = NULL; |
| 465 |
return (cached + 0); |
| 466 |
} |
| 467 |
return NULL; |
| 468 |
} |
| 469 |
|
| 470 |
|
| 471 |
sf_picture *sf_getpic(int num) |
| 472 |
{ |
| 473 |
sf_picture *res; |
| 474 |
cacheinit(); |
| 475 |
res = cachefind(num); |
| 476 |
if (res) |
| 477 |
return res; |
| 478 |
|
| 479 |
res = cachefind(-1); |
| 480 |
if (sf_loadpic(num, res)) |
| 481 |
return res; |
| 482 |
return NULL; |
| 483 |
} |