| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
#include "glk.h" |
| 11 |
#include "gi_blorb.h" |
| 12 |
|
| 13 |
#ifndef NULL |
| 14 |
#define NULL 0 |
| 15 |
#endif |
| 16 |
#ifndef TRUE |
| 17 |
#define TRUE 1 |
| 18 |
#endif |
| 19 |
#ifndef FALSE |
| 20 |
#define FALSE 0 |
| 21 |
#endif |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
#define giblorb_native4(v) \ |
| 26 |
( (((glui32)((v)[3]) ) & 0x000000ff) \ |
| 27 |
| (((glui32)((v)[2]) << 8) & 0x0000ff00) \ |
| 28 |
| (((glui32)((v)[1]) << 16) & 0x00ff0000) \ |
| 29 |
| (((glui32)((v)[0]) << 24) & 0xff000000)) |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
#define giblorb_ID_FORM (giblorb_make_id('F', 'O', 'R', 'M')) |
| 34 |
#define giblorb_ID_IFRS (giblorb_make_id('I', 'F', 'R', 'S')) |
| 35 |
#define giblorb_ID_RIdx (giblorb_make_id('R', 'I', 'd', 'x')) |
| 36 |
|
| 37 |
|
| 38 |
typedef struct giblorb_chunkdesc_struct { |
| 39 |
glui32 type; |
| 40 |
glui32 len; |
| 41 |
glui32 startpos; |
| 42 |
glui32 datpos; |
| 43 |
|
| 44 |
void *ptr; |
| 45 |
int auxdatnum; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
} giblorb_chunkdesc_t; |
| 50 |
|
| 51 |
|
| 52 |
typedef struct giblorb_resdesc_struct { |
| 53 |
glui32 usage; |
| 54 |
glui32 resnum; |
| 55 |
glui32 chunknum; |
| 56 |
} giblorb_resdesc_t; |
| 57 |
|
| 58 |
|
| 59 |
typedef struct giblorb_auxpict_struct { |
| 60 |
int loaded; |
| 61 |
glui32 width; |
| 62 |
glui32 height; |
| 63 |
char *alttext; |
| 64 |
} giblorb_auxpict_t; |
| 65 |
|
| 66 |
|
| 67 |
struct giblorb_map_struct { |
| 68 |
glui32 inited; |
| 69 |
|
| 70 |
strid_t file; |
| 71 |
|
| 72 |
int numchunks; |
| 73 |
giblorb_chunkdesc_t *chunks; |
| 74 |
|
| 75 |
int numresources; |
| 76 |
giblorb_resdesc_t *resources; |
| 77 |
giblorb_resdesc_t **ressorted; |
| 78 |
|
| 79 |
|
| 80 |
giblorb_auxpict_t *auxpict; |
| 81 |
}; |
| 82 |
|
| 83 |
#define giblorb_Inited_Magic (0xB7012BED) |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
static int lib_inited = FALSE; |
| 88 |
|
| 89 |
static giblorb_err_t giblorb_initialize(void); |
| 90 |
static giblorb_err_t giblorb_initialize_map(giblorb_map_t *map); |
| 91 |
static giblorb_err_t giblorb_image_get_size_jpeg(unsigned char *ptr, glui32 length, giblorb_auxpict_t *auxpict); |
| 92 |
static giblorb_err_t giblorb_image_get_size_png(unsigned char *ptr, glui32 length, giblorb_auxpict_t *auxpict); |
| 93 |
static void giblorb_qsort(giblorb_resdesc_t **list, int len); |
| 94 |
static giblorb_resdesc_t *giblorb_bsearch(giblorb_resdesc_t *sample, |
| 95 |
giblorb_resdesc_t **list, int len); |
| 96 |
static void *giblorb_malloc(glui32 len); |
| 97 |
static void *giblorb_realloc(void *ptr, glui32 len); |
| 98 |
static void giblorb_free(void *ptr); |
| 99 |
|
| 100 |
static giblorb_err_t giblorb_initialize() |
| 101 |
{ |
| 102 |
return giblorb_err_None; |
| 103 |
} |
| 104 |
|
| 105 |
giblorb_err_t giblorb_create_map(strid_t file, giblorb_map_t **newmap) |
| 106 |
{ |
| 107 |
giblorb_err_t err; |
| 108 |
giblorb_map_t *map; |
| 109 |
glui32 readlen; |
| 110 |
glui32 nextpos, totallength; |
| 111 |
giblorb_chunkdesc_t *chunks; |
| 112 |
int chunks_size, numchunks; |
| 113 |
char buffer[16]; |
| 114 |
|
| 115 |
*newmap = NULL; |
| 116 |
|
| 117 |
if (!lib_inited) { |
| 118 |
err = giblorb_initialize(); |
| 119 |
if (err) |
| 120 |
return err; |
| 121 |
lib_inited = TRUE; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
glk_stream_set_position(file, 0, seekmode_Start); |
| 127 |
|
| 128 |
readlen = glk_get_buffer_stream(file, buffer, 12); |
| 129 |
if (readlen != 12) |
| 130 |
return giblorb_err_Read; |
| 131 |
|
| 132 |
if (giblorb_native4(buffer+0) != giblorb_ID_FORM) |
| 133 |
return giblorb_err_Format; |
| 134 |
if (giblorb_native4(buffer+8) != giblorb_ID_IFRS) |
| 135 |
return giblorb_err_Format; |
| 136 |
|
| 137 |
totallength = giblorb_native4(buffer+4) + 8; |
| 138 |
nextpos = 12; |
| 139 |
|
| 140 |
chunks_size = 8; |
| 141 |
numchunks = 0; |
| 142 |
chunks = (giblorb_chunkdesc_t *)giblorb_malloc(sizeof(giblorb_chunkdesc_t) |
| 143 |
* chunks_size); |
| 144 |
|
| 145 |
while (nextpos < totallength) { |
| 146 |
glui32 type, len; |
| 147 |
int chunum; |
| 148 |
giblorb_chunkdesc_t *chu; |
| 149 |
|
| 150 |
glk_stream_set_position(file, nextpos, seekmode_Start); |
| 151 |
|
| 152 |
readlen = glk_get_buffer_stream(file, buffer, 8); |
| 153 |
if (readlen != 8) { |
| 154 |
giblorb_free(chunks); |
| 155 |
return giblorb_err_Read; |
| 156 |
} |
| 157 |
|
| 158 |
type = giblorb_native4(buffer+0); |
| 159 |
len = giblorb_native4(buffer+4); |
| 160 |
|
| 161 |
if (numchunks >= chunks_size) { |
| 162 |
chunks_size *= 2; |
| 163 |
chunks = (giblorb_chunkdesc_t *)giblorb_realloc(chunks, |
| 164 |
sizeof(giblorb_chunkdesc_t) * chunks_size); |
| 165 |
} |
| 166 |
|
| 167 |
chunum = numchunks; |
| 168 |
chu = &(chunks[chunum]); |
| 169 |
numchunks++; |
| 170 |
|
| 171 |
chu->type = type; |
| 172 |
chu->startpos = nextpos; |
| 173 |
if (type == giblorb_ID_FORM) { |
| 174 |
chu->datpos = nextpos; |
| 175 |
chu->len = len+8; |
| 176 |
} |
| 177 |
else { |
| 178 |
chu->datpos = nextpos+8; |
| 179 |
chu->len = len; |
| 180 |
} |
| 181 |
chu->ptr = NULL; |
| 182 |
chu->auxdatnum = -1; |
| 183 |
|
| 184 |
nextpos = nextpos + len + 8; |
| 185 |
if (nextpos & 1) |
| 186 |
nextpos++; |
| 187 |
|
| 188 |
if (nextpos > totallength) { |
| 189 |
giblorb_free(chunks); |
| 190 |
return giblorb_err_Format; |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
map = (giblorb_map_t *)giblorb_malloc(sizeof(giblorb_map_t)); |
| 198 |
if (!map) { |
| 199 |
giblorb_free(chunks); |
| 200 |
return giblorb_err_Alloc; |
| 201 |
} |
| 202 |
|
| 203 |
map->inited = giblorb_Inited_Magic; |
| 204 |
map->file = file; |
| 205 |
map->chunks = chunks; |
| 206 |
map->numchunks = numchunks; |
| 207 |
map->resources = NULL; |
| 208 |
map->ressorted = NULL; |
| 209 |
map->numresources = 0; |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
map->auxpict = NULL; |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
err = giblorb_initialize_map(map); |
| 222 |
if (err) { |
| 223 |
giblorb_destroy_map(map); |
| 224 |
return err; |
| 225 |
} |
| 226 |
|
| 227 |
*newmap = map; |
| 228 |
return giblorb_err_None; |
| 229 |
} |
| 230 |
|
| 231 |
static giblorb_err_t giblorb_initialize_map(giblorb_map_t *map) |
| 232 |
{ |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
int ix, jx; |
| 238 |
giblorb_result_t chunkres; |
| 239 |
giblorb_err_t err; |
| 240 |
char *ptr; |
| 241 |
glui32 len; |
| 242 |
glui32 numres; |
| 243 |
int gotindex = FALSE; |
| 244 |
int pictcount = 0; |
| 245 |
|
| 246 |
for (ix=0; ix<map->numchunks; ix++) { |
| 247 |
giblorb_chunkdesc_t *chu = &map->chunks[ix]; |
| 248 |
|
| 249 |
switch (chu->type) { |
| 250 |
|
| 251 |
case giblorb_ID_RIdx: |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
if (gotindex) |
| 256 |
return giblorb_err_Format; |
| 257 |
err = giblorb_load_chunk_by_number(map, giblorb_method_Memory, |
| 258 |
&chunkres, ix); |
| 259 |
if (err) |
| 260 |
return err; |
| 261 |
|
| 262 |
ptr = chunkres.data.ptr; |
| 263 |
len = chunkres.length; |
| 264 |
numres = giblorb_native4(ptr+0); |
| 265 |
|
| 266 |
if (numres & 0xF0000000) |
| 267 |
return giblorb_err_Format; |
| 268 |
|
| 269 |
if (numres) { |
| 270 |
int ix2; |
| 271 |
giblorb_resdesc_t *resources = NULL; |
| 272 |
giblorb_resdesc_t **ressorted = NULL; |
| 273 |
|
| 274 |
if (len != numres*12+4) |
| 275 |
return giblorb_err_Format; |
| 276 |
|
| 277 |
resources = (giblorb_resdesc_t *)giblorb_malloc(numres |
| 278 |
* sizeof(giblorb_resdesc_t)); |
| 279 |
if (!resources) { |
| 280 |
return giblorb_err_Alloc; |
| 281 |
} |
| 282 |
ressorted = (giblorb_resdesc_t **)giblorb_malloc(numres |
| 283 |
* sizeof(giblorb_resdesc_t *)); |
| 284 |
if (!ressorted) { |
| 285 |
giblorb_free(resources); |
| 286 |
return giblorb_err_Alloc; |
| 287 |
} |
| 288 |
|
| 289 |
ix2 = 0; |
| 290 |
for (jx=0; jx<numres; jx++) { |
| 291 |
giblorb_resdesc_t *res = &(resources[jx]); |
| 292 |
glui32 respos; |
| 293 |
|
| 294 |
res->usage = giblorb_native4(ptr+jx*12+4); |
| 295 |
res->resnum = giblorb_native4(ptr+jx*12+8); |
| 296 |
respos = giblorb_native4(ptr+jx*12+12); |
| 297 |
|
| 298 |
while (ix2 < map->numchunks |
| 299 |
&& map->chunks[ix2].startpos < respos) |
| 300 |
ix2++; |
| 301 |
|
| 302 |
if (ix2 >= map->numchunks |
| 303 |
|| map->chunks[ix2].startpos != respos) { |
| 304 |
|
| 305 |
giblorb_free(resources); |
| 306 |
giblorb_free(ressorted); |
| 307 |
return giblorb_err_Format; |
| 308 |
} |
| 309 |
|
| 310 |
res->chunknum = ix2; |
| 311 |
|
| 312 |
ressorted[jx] = res; |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
giblorb_qsort(ressorted, numres); |
| 319 |
|
| 320 |
map->numresources = numres; |
| 321 |
map->resources = resources; |
| 322 |
map->ressorted = ressorted; |
| 323 |
} |
| 324 |
|
| 325 |
giblorb_unload_chunk(map, ix); |
| 326 |
gotindex = TRUE; |
| 327 |
break; |
| 328 |
|
| 329 |
case giblorb_ID_JPEG: |
| 330 |
case giblorb_ID_PNG: |
| 331 |
chu->auxdatnum = pictcount; |
| 332 |
pictcount++; |
| 333 |
break; |
| 334 |
|
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
if (pictcount) { |
| 339 |
map->auxpict = (giblorb_auxpict_t *)giblorb_malloc(pictcount |
| 340 |
* sizeof(giblorb_auxpict_t)); |
| 341 |
if (!map->auxpict) |
| 342 |
return giblorb_err_Alloc; |
| 343 |
for (ix=0; ix<pictcount; ix++) { |
| 344 |
giblorb_auxpict_t *auxpict = &(map->auxpict[ix]); |
| 345 |
auxpict->loaded = FALSE; |
| 346 |
auxpict->width = 0; |
| 347 |
auxpict->height = 0; |
| 348 |
auxpict->alttext = NULL; |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
return giblorb_err_None; |
| 353 |
} |
| 354 |
|
| 355 |
giblorb_err_t giblorb_destroy_map(giblorb_map_t *map) |
| 356 |
{ |
| 357 |
int ix; |
| 358 |
|
| 359 |
if (!map || !map->chunks || map->inited != giblorb_Inited_Magic) |
| 360 |
return giblorb_err_NotAMap; |
| 361 |
|
| 362 |
if (map->auxpict) { |
| 363 |
giblorb_free(map->auxpict); |
| 364 |
map->auxpict = NULL; |
| 365 |
} |
| 366 |
|
| 367 |
for (ix=0; ix<map->numchunks; ix++) { |
| 368 |
giblorb_chunkdesc_t *chu = &(map->chunks[ix]); |
| 369 |
if (chu->ptr) { |
| 370 |
giblorb_free(chu->ptr); |
| 371 |
chu->ptr = NULL; |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
if (map->chunks) { |
| 376 |
giblorb_free(map->chunks); |
| 377 |
map->chunks = NULL; |
| 378 |
} |
| 379 |
|
| 380 |
map->numchunks = 0; |
| 381 |
|
| 382 |
if (map->resources) { |
| 383 |
giblorb_free(map->resources); |
| 384 |
map->resources = NULL; |
| 385 |
} |
| 386 |
|
| 387 |
if (map->ressorted) { |
| 388 |
giblorb_free(map->ressorted); |
| 389 |
map->ressorted = NULL; |
| 390 |
} |
| 391 |
|
| 392 |
map->numresources = 0; |
| 393 |
|
| 394 |
map->file = NULL; |
| 395 |
map->inited = 0; |
| 396 |
|
| 397 |
giblorb_free(map); |
| 398 |
|
| 399 |
return giblorb_err_None; |
| 400 |
} |
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
giblorb_err_t giblorb_load_chunk_by_type(giblorb_map_t *map, |
| 405 |
glui32 method, giblorb_result_t *res, glui32 type, |
| 406 |
glui32 count) |
| 407 |
{ |
| 408 |
int ix; |
| 409 |
|
| 410 |
for (ix=0; ix < map->numchunks; ix++) { |
| 411 |
if (map->chunks[ix].type == type) { |
| 412 |
if (count == 0) |
| 413 |
break; |
| 414 |
count--; |
| 415 |
} |
| 416 |
} |
| 417 |
|
| 418 |
if (ix >= map->numchunks) { |
| 419 |
return giblorb_err_NotFound; |
| 420 |
} |
| 421 |
|
| 422 |
return giblorb_load_chunk_by_number(map, method, res, ix); |
| 423 |
} |
| 424 |
|
| 425 |
giblorb_err_t giblorb_load_chunk_by_number(giblorb_map_t *map, |
| 426 |
glui32 method, giblorb_result_t *res, glui32 chunknum) |
| 427 |
{ |
| 428 |
giblorb_chunkdesc_t *chu; |
| 429 |
|
| 430 |
if (chunknum >= map->numchunks) |
| 431 |
return giblorb_err_NotFound; |
| 432 |
|
| 433 |
chu = &(map->chunks[chunknum]); |
| 434 |
|
| 435 |
switch (method) { |
| 436 |
|
| 437 |
case giblorb_method_DontLoad: |
| 438 |
|
| 439 |
break; |
| 440 |
|
| 441 |
case giblorb_method_FilePos: |
| 442 |
res->data.startpos = chu->datpos; |
| 443 |
break; |
| 444 |
|
| 445 |
case giblorb_method_Memory: |
| 446 |
if (!chu->ptr) { |
| 447 |
glui32 readlen; |
| 448 |
void *dat = giblorb_malloc(chu->len); |
| 449 |
|
| 450 |
if (!dat) |
| 451 |
return giblorb_err_Alloc; |
| 452 |
|
| 453 |
glk_stream_set_position(map->file, chu->datpos, |
| 454 |
seekmode_Start); |
| 455 |
|
| 456 |
readlen = glk_get_buffer_stream(map->file, dat, |
| 457 |
chu->len); |
| 458 |
if (readlen != chu->len) |
| 459 |
return giblorb_err_Read; |
| 460 |
|
| 461 |
chu->ptr = dat; |
| 462 |
} |
| 463 |
res->data.ptr = chu->ptr; |
| 464 |
break; |
| 465 |
} |
| 466 |
|
| 467 |
res->chunknum = chunknum; |
| 468 |
res->length = chu->len; |
| 469 |
res->chunktype = chu->type; |
| 470 |
|
| 471 |
return giblorb_err_None; |
| 472 |
} |
| 473 |
|
| 474 |
giblorb_err_t giblorb_load_resource(giblorb_map_t *map, glui32 method, |
| 475 |
giblorb_result_t *res, glui32 usage, glui32 resnum) |
| 476 |
{ |
| 477 |
giblorb_resdesc_t sample; |
| 478 |
giblorb_resdesc_t *found; |
| 479 |
|
| 480 |
sample.usage = usage; |
| 481 |
sample.resnum = resnum; |
| 482 |
|
| 483 |
found = giblorb_bsearch(&sample, map->ressorted, map->numresources); |
| 484 |
|
| 485 |
if (!found) |
| 486 |
return giblorb_err_NotFound; |
| 487 |
|
| 488 |
return giblorb_load_chunk_by_number(map, method, res, found->chunknum); |
| 489 |
} |
| 490 |
|
| 491 |
giblorb_err_t giblorb_unload_chunk(giblorb_map_t *map, glui32 chunknum) |
| 492 |
{ |
| 493 |
giblorb_chunkdesc_t *chu; |
| 494 |
|
| 495 |
if (chunknum >= map->numchunks) |
| 496 |
return giblorb_err_NotFound; |
| 497 |
|
| 498 |
chu = &(map->chunks[chunknum]); |
| 499 |
|
| 500 |
if (chu->ptr) { |
| 501 |
giblorb_free(chu->ptr); |
| 502 |
chu->ptr = NULL; |
| 503 |
} |
| 504 |
|
| 505 |
return giblorb_err_None; |
| 506 |
} |
| 507 |
|
| 508 |
giblorb_err_t giblorb_count_resources(giblorb_map_t *map, glui32 usage, |
| 509 |
glui32 *num, glui32 *min, glui32 *max) |
| 510 |
{ |
| 511 |
int ix; |
| 512 |
int count; |
| 513 |
glui32 val; |
| 514 |
glui32 minval, maxval; |
| 515 |
|
| 516 |
count = 0; |
| 517 |
minval = 0; |
| 518 |
maxval = 0; |
| 519 |
|
| 520 |
for (ix=0; ix<map->numresources; ix++) { |
| 521 |
if (map->resources[ix].usage == usage) { |
| 522 |
val = map->resources[ix].resnum; |
| 523 |
if (count == 0) { |
| 524 |
count++; |
| 525 |
minval = val; |
| 526 |
maxval = val; |
| 527 |
} |
| 528 |
else { |
| 529 |
count++; |
| 530 |
if (val < minval) |
| 531 |
minval = val; |
| 532 |
if (val > maxval) |
| 533 |
maxval = val; |
| 534 |
} |
| 535 |
} |
| 536 |
} |
| 537 |
|
| 538 |
if (num) |
| 539 |
*num = count; |
| 540 |
if (min) |
| 541 |
*min = minval; |
| 542 |
if (max) |
| 543 |
*max = maxval; |
| 544 |
|
| 545 |
return giblorb_err_None; |
| 546 |
} |
| 547 |
|
| 548 |
giblorb_err_t giblorb_load_image_info(giblorb_map_t *map, |
| 549 |
glui32 resnum, giblorb_image_info_t *res) |
| 550 |
{ |
| 551 |
giblorb_resdesc_t sample; |
| 552 |
giblorb_resdesc_t *found; |
| 553 |
glui32 chunknum; |
| 554 |
giblorb_chunkdesc_t *chu; |
| 555 |
giblorb_auxpict_t *auxpict; |
| 556 |
|
| 557 |
sample.usage = giblorb_ID_Pict; |
| 558 |
sample.resnum = resnum; |
| 559 |
|
| 560 |
found = giblorb_bsearch(&sample, map->ressorted, map->numresources); |
| 561 |
|
| 562 |
if (!found) |
| 563 |
return giblorb_err_NotFound; |
| 564 |
|
| 565 |
chunknum = found->chunknum; |
| 566 |
if (chunknum >= map->numchunks) |
| 567 |
return giblorb_err_NotFound; |
| 568 |
|
| 569 |
chu = &(map->chunks[chunknum]); |
| 570 |
if (chu->auxdatnum < 0) |
| 571 |
return giblorb_err_NotFound; |
| 572 |
|
| 573 |
auxpict = &(map->auxpict[chu->auxdatnum]); |
| 574 |
if (!auxpict->loaded) { |
| 575 |
giblorb_result_t res; |
| 576 |
giblorb_err_t err = giblorb_load_chunk_by_number(map, giblorb_method_Memory, &res, chunknum); |
| 577 |
if (err) |
| 578 |
return err; |
| 579 |
|
| 580 |
if (chu->type == giblorb_ID_JPEG) |
| 581 |
err = giblorb_image_get_size_jpeg(res.data.ptr, res.length, auxpict); |
| 582 |
else if (chu->type == giblorb_ID_PNG) |
| 583 |
err = giblorb_image_get_size_png(res.data.ptr, res.length, auxpict); |
| 584 |
else |
| 585 |
err = giblorb_err_Format; |
| 586 |
|
| 587 |
giblorb_unload_chunk(map, chunknum); |
| 588 |
|
| 589 |
if (err) |
| 590 |
return err; |
| 591 |
|
| 592 |
auxpict->loaded = TRUE; |
| 593 |
} |
| 594 |
|
| 595 |
res->chunktype = chu->type; |
| 596 |
res->width = auxpict->width; |
| 597 |
res->height = auxpict->height; |
| 598 |
res->alttext = auxpict->alttext; |
| 599 |
return giblorb_err_None; |
| 600 |
} |
| 601 |
|
| 602 |
static giblorb_err_t giblorb_image_get_size_jpeg(unsigned char *arr, glui32 length, giblorb_auxpict_t *auxpict) |
| 603 |
{ |
| 604 |
int pos = 0; |
| 605 |
while (pos < length) { |
| 606 |
unsigned char marker; |
| 607 |
int chunklen; |
| 608 |
if (arr[pos] != 0xFF) { |
| 609 |
|
| 610 |
return giblorb_err_Format; |
| 611 |
} |
| 612 |
while (arr[pos] == 0xFF) |
| 613 |
pos += 1; |
| 614 |
marker = arr[pos]; |
| 615 |
pos += 1; |
| 616 |
if (marker == 0x01 || (marker >= 0xD0 && marker <= 0xD9)) { |
| 617 |
|
| 618 |
continue; |
| 619 |
} |
| 620 |
chunklen = (arr[pos+0] << 8) | (arr[pos+1]); |
| 621 |
if (marker >= 0xC0 && marker <= 0xCF && marker != 0xC8) { |
| 622 |
if (chunklen < 7) { |
| 623 |
|
| 624 |
return giblorb_err_Format; |
| 625 |
} |
| 626 |
auxpict->height = (arr[pos+3] << 8) | (arr[pos+4]); |
| 627 |
auxpict->width = (arr[pos+5] << 8) | (arr[pos+6]); |
| 628 |
return giblorb_err_None; |
| 629 |
} |
| 630 |
pos += chunklen; |
| 631 |
} |
| 632 |
|
| 633 |
|
| 634 |
return giblorb_err_Format; |
| 635 |
} |
| 636 |
|
| 637 |
static giblorb_err_t giblorb_image_get_size_png(unsigned char *arr, glui32 length, giblorb_auxpict_t *auxpict) |
| 638 |
{ |
| 639 |
int pos = 0; |
| 640 |
if (length < 8) |
| 641 |
return giblorb_err_Format; |
| 642 |
if (arr[0] != 0x89 || arr[1] != 'P' || arr[2] != 'N' || arr[3] != 'G') { |
| 643 |
|
| 644 |
return giblorb_err_Format; |
| 645 |
} |
| 646 |
pos += 8; |
| 647 |
while (pos < length) { |
| 648 |
glui32 chunklen = giblorb_native4(arr+pos); |
| 649 |
glui32 chunktype; |
| 650 |
pos += 4; |
| 651 |
chunktype = giblorb_native4(arr+pos); |
| 652 |
pos += 4; |
| 653 |
if (chunktype == giblorb_make_id('I', 'H', 'D', 'R')) { |
| 654 |
auxpict->width = giblorb_native4(arr+pos); |
| 655 |
pos += 4; |
| 656 |
auxpict->height = giblorb_native4(arr+pos); |
| 657 |
pos += 4; |
| 658 |
return giblorb_err_None; |
| 659 |
} |
| 660 |
pos += chunklen; |
| 661 |
pos += 4; |
| 662 |
} |
| 663 |
|
| 664 |
|
| 665 |
return giblorb_err_Format; |
| 666 |
} |
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
static int sortsplot(giblorb_resdesc_t *v1, giblorb_resdesc_t *v2) |
| 671 |
{ |
| 672 |
if (v1->usage < v2->usage) |
| 673 |
return -1; |
| 674 |
if (v1->usage > v2->usage) |
| 675 |
return 1; |
| 676 |
if (v1->resnum < v2->resnum) |
| 677 |
return -1; |
| 678 |
if (v1->resnum > v2->resnum) |
| 679 |
return 1; |
| 680 |
return 0; |
| 681 |
} |
| 682 |
|
| 683 |
static void giblorb_qsort(giblorb_resdesc_t **list, int len) |
| 684 |
{ |
| 685 |
int ix, jx, res; |
| 686 |
giblorb_resdesc_t *tmpptr, *pivot; |
| 687 |
|
| 688 |
if (len < 6) { |
| 689 |
|
| 690 |
for (jx=len-1; jx>0; jx--) { |
| 691 |
for (ix=0; ix<jx; ix++) { |
| 692 |
res = sortsplot(list[ix], list[ix+1]); |
| 693 |
if (res > 0) { |
| 694 |
tmpptr = list[ix]; |
| 695 |
list[ix] = list[ix+1]; |
| 696 |
list[ix+1] = tmpptr; |
| 697 |
} |
| 698 |
} |
| 699 |
} |
| 700 |
} |
| 701 |
else { |
| 702 |
|
| 703 |
pivot = list[len/2]; |
| 704 |
ix=0; |
| 705 |
jx=len; |
| 706 |
while (1) { |
| 707 |
while (ix < jx-1 && sortsplot(list[ix], pivot) < 0) |
| 708 |
ix++; |
| 709 |
while (ix < jx-1 && sortsplot(list[jx-1], pivot) > 0) |
| 710 |
jx--; |
| 711 |
if (ix >= jx-1) |
| 712 |
break; |
| 713 |
tmpptr = list[ix]; |
| 714 |
list[ix] = list[jx-1]; |
| 715 |
list[jx-1] = tmpptr; |
| 716 |
} |
| 717 |
ix++; |
| 718 |
|
| 719 |
giblorb_qsort(list+0, ix); |
| 720 |
giblorb_qsort(list+ix, len-ix); |
| 721 |
} |
| 722 |
} |
| 723 |
|
| 724 |
giblorb_resdesc_t *giblorb_bsearch(giblorb_resdesc_t *sample, |
| 725 |
giblorb_resdesc_t **list, int len) |
| 726 |
{ |
| 727 |
int top, bot, val, res; |
| 728 |
|
| 729 |
bot = 0; |
| 730 |
top = len; |
| 731 |
|
| 732 |
while (bot < top) { |
| 733 |
val = (top+bot) / 2; |
| 734 |
res = sortsplot(list[val], sample); |
| 735 |
if (res == 0) |
| 736 |
return list[val]; |
| 737 |
if (res < 0) { |
| 738 |
bot = val+1; |
| 739 |
} |
| 740 |
else { |
| 741 |
top = val; |
| 742 |
} |
| 743 |
} |
| 744 |
|
| 745 |
return NULL; |
| 746 |
} |
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
#include <stdlib.h> |
| 753 |
|
| 754 |
|
| 755 |
static void *giblorb_malloc(glui32 len) |
| 756 |
{ |
| 757 |
return malloc(len); |
| 758 |
} |
| 759 |
|
| 760 |
static void *giblorb_realloc(void *ptr, glui32 len) |
| 761 |
{ |
| 762 |
return realloc(ptr, len); |
| 763 |
} |
| 764 |
|
| 765 |
static void giblorb_free(void *ptr) |
| 766 |
{ |
| 767 |
free(ptr); |
| 768 |
} |
| 769 |
|
| 770 |
|