vrshoot

annotate libs/imago/file_rgbe.c @ 1:e7ca128b8713

looks nice :)
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 02 Feb 2014 00:35:22 +0200
parents
children
rev   line source
nuclear@0 1 /* This file contains code to read and write four byte rgbe file format
nuclear@0 2 * developed by Greg Ward. It handles the conversions between rgbe and
nuclear@0 3 * pixels consisting of floats. The data is assumed to be an array of floats.
nuclear@0 4 * By default there are three floats per pixel in the order red, green, blue.
nuclear@0 5 * (RGBE_DATA_??? values control this.)
nuclear@0 6 *
nuclear@0 7 * written by Bruce Walter (bjw@graphics.cornell.edu) 5/26/95
nuclear@0 8 * based on code written by Greg Ward
nuclear@0 9 * minor modifications by John Tsiombikas (nuclear@member.fsf.org) apr.9 2007
nuclear@0 10 */
nuclear@0 11
nuclear@0 12 #include <stdio.h>
nuclear@0 13 #include <stdlib.h>
nuclear@0 14 #include <string.h>
nuclear@0 15 #include <math.h>
nuclear@0 16 #include <ctype.h>
nuclear@0 17 #include <errno.h>
nuclear@0 18 #include "imago2.h"
nuclear@0 19 #include "ftype_module.h"
nuclear@0 20
nuclear@0 21
nuclear@0 22 typedef struct {
nuclear@0 23 int valid; /* indicate which fields are valid */
nuclear@0 24 char programtype[16]; /* listed at beginning of file to identify it
nuclear@0 25 * after "#?". defaults to "RGBE" */
nuclear@0 26 float gamma; /* image has already been gamma corrected with
nuclear@0 27 * given gamma. defaults to 1.0 (no correction) */
nuclear@0 28 float exposure; /* a value of 1.0 in an image corresponds to
nuclear@0 29 * <exposure> watts/steradian/m^2.
nuclear@0 30 * defaults to 1.0 */
nuclear@0 31 } rgbe_header_info;
nuclear@0 32
nuclear@0 33
nuclear@0 34 static int check(struct img_io *io);
nuclear@0 35 static int read(struct img_pixmap *img, struct img_io *io);
nuclear@0 36 static int write(struct img_pixmap *img, struct img_io *io);
nuclear@0 37
nuclear@0 38 static int rgbe_read_header(struct img_io *io, int *width, int *height, rgbe_header_info * info);
nuclear@0 39 static int rgbe_read_pixels_rle(struct img_io *io, float *data, int scanline_width, int num_scanlines);
nuclear@0 40
nuclear@0 41
nuclear@0 42 int img_register_rgbe(void)
nuclear@0 43 {
nuclear@0 44 static struct ftype_module mod = {".rgbe", check, read, write};
nuclear@0 45 return img_register_module(&mod);
nuclear@0 46 }
nuclear@0 47
nuclear@0 48
nuclear@0 49 static int check(struct img_io *io)
nuclear@0 50 {
nuclear@0 51 int xsz, ysz, res;
nuclear@0 52 long pos = io->seek(0, SEEK_CUR, io->uptr);
nuclear@0 53
nuclear@0 54 rgbe_header_info hdr;
nuclear@0 55 res = rgbe_read_header(io, &xsz, &ysz, &hdr);
nuclear@0 56
nuclear@0 57 io->seek(pos, SEEK_SET, io->uptr);
nuclear@0 58 return res;
nuclear@0 59 }
nuclear@0 60
nuclear@0 61 static int read(struct img_pixmap *img, struct img_io *io)
nuclear@0 62 {
nuclear@0 63 int xsz, ysz;
nuclear@0 64 rgbe_header_info hdr;
nuclear@0 65
nuclear@0 66 if(rgbe_read_header(io, &xsz, &ysz, &hdr) == -1) {
nuclear@0 67 return -1;
nuclear@0 68 }
nuclear@0 69
nuclear@0 70 if(img_set_pixels(img, xsz, ysz, IMG_FMT_RGBF, 0) == -1) {
nuclear@0 71 return -1;
nuclear@0 72 }
nuclear@0 73 if(rgbe_read_pixels_rle(io, img->pixels, xsz, ysz) == -1) {
nuclear@0 74 return -1;
nuclear@0 75 }
nuclear@0 76 return 0;
nuclear@0 77 }
nuclear@0 78
nuclear@0 79 static int write(struct img_pixmap *img, struct img_io *io)
nuclear@0 80 {
nuclear@0 81 return -1; /* TODO */
nuclear@0 82 }
nuclear@0 83
nuclear@0 84
nuclear@0 85 static int iofgetc(struct img_io *io)
nuclear@0 86 {
nuclear@0 87 char c;
nuclear@0 88 return io->read(&c, 1, io->uptr) < 1 ? -1 : c;
nuclear@0 89 }
nuclear@0 90
nuclear@0 91 static char *iofgets(char *buf, int size, struct img_io *io)
nuclear@0 92 {
nuclear@0 93 int c;
nuclear@0 94 char *ptr = buf;
nuclear@0 95
nuclear@0 96 while(--size > 0 && (c = iofgetc(io)) != -1) {
nuclear@0 97 *ptr++ = c;
nuclear@0 98 if(c == '\n') break;
nuclear@0 99 }
nuclear@0 100 *ptr = 0;
nuclear@0 101
nuclear@0 102 return ptr == buf ? 0 : buf;
nuclear@0 103 }
nuclear@0 104
nuclear@0 105
nuclear@0 106 /* flags indicating which fields in an rgbe_header_info are valid */
nuclear@0 107 #define RGBE_VALID_PROGRAMTYPE 0x01
nuclear@0 108 #define RGBE_VALID_GAMMA 0x02
nuclear@0 109 #define RGBE_VALID_EXPOSURE 0x04
nuclear@0 110
nuclear@0 111 /* return codes for rgbe routines */
nuclear@0 112 #define RGBE_RETURN_SUCCESS 0
nuclear@0 113 #define RGBE_RETURN_FAILURE -1
nuclear@0 114
nuclear@0 115
nuclear@0 116 #if defined(__cplusplus) || defined(GNUC) || __STDC_VERSION >= 199901L
nuclear@0 117 #define INLINE inline
nuclear@0 118 #else
nuclear@0 119 #define INLINE
nuclear@0 120 #endif
nuclear@0 121
nuclear@0 122 /* offsets to red, green, and blue components in a data (float) pixel */
nuclear@0 123 #define RGBE_DATA_RED 0
nuclear@0 124 #define RGBE_DATA_GREEN 1
nuclear@0 125 #define RGBE_DATA_BLUE 2
nuclear@0 126
nuclear@0 127 /* number of floats per pixel */
nuclear@0 128 #define RGBE_DATA_SIZE 3
nuclear@0 129
nuclear@0 130 enum rgbe_error_codes {
nuclear@0 131 rgbe_read_error,
nuclear@0 132 rgbe_write_error,
nuclear@0 133 rgbe_format_error,
nuclear@0 134 rgbe_memory_error
nuclear@0 135 };
nuclear@0 136
nuclear@0 137
nuclear@0 138 /* default error routine. change this to change error handling */
nuclear@0 139 static int rgbe_error(int rgbe_error_code, char *msg)
nuclear@0 140 {
nuclear@0 141 switch (rgbe_error_code) {
nuclear@0 142 case rgbe_read_error:
nuclear@0 143 fprintf(stderr, "RGBE read error: %s\n", strerror(errno));
nuclear@0 144 break;
nuclear@0 145
nuclear@0 146 case rgbe_write_error:
nuclear@0 147 fprintf(stderr, "RGBE write error: %s\n", strerror(errno));
nuclear@0 148 break;
nuclear@0 149
nuclear@0 150 case rgbe_format_error:
nuclear@0 151 fprintf(stderr, "RGBE bad file format: %s\n", msg);
nuclear@0 152 break;
nuclear@0 153
nuclear@0 154 default:
nuclear@0 155 case rgbe_memory_error:
nuclear@0 156 fprintf(stderr, "RGBE error: %s\n", msg);
nuclear@0 157 }
nuclear@0 158 return RGBE_RETURN_FAILURE;
nuclear@0 159 }
nuclear@0 160
nuclear@0 161 /* standard conversion from float pixels to rgbe pixels */
nuclear@0 162 /*static INLINE void float2rgbe(unsigned char rgbe[4], float red, float green, float blue)
nuclear@0 163 {
nuclear@0 164 float v;
nuclear@0 165 int e;
nuclear@0 166
nuclear@0 167 v = red;
nuclear@0 168 if(green > v)
nuclear@0 169 v = green;
nuclear@0 170 if(blue > v)
nuclear@0 171 v = blue;
nuclear@0 172 if(v < 1e-32) {
nuclear@0 173 rgbe[0] = rgbe[1] = rgbe[2] = rgbe[3] = 0;
nuclear@0 174 } else {
nuclear@0 175 v = frexp(v, &e) * 256.0 / v;
nuclear@0 176 rgbe[0] = (unsigned char)(red * v);
nuclear@0 177 rgbe[1] = (unsigned char)(green * v);
nuclear@0 178 rgbe[2] = (unsigned char)(blue * v);
nuclear@0 179 rgbe[3] = (unsigned char)(e + 128);
nuclear@0 180 }
nuclear@0 181 }*/
nuclear@0 182
nuclear@0 183 /* standard conversion from rgbe to float pixels */
nuclear@0 184 /* note: Ward uses ldexp(col+0.5,exp-(128+8)). However we wanted pixels */
nuclear@0 185 /* in the range [0,1] to map back into the range [0,1]. */
nuclear@0 186 static INLINE void rgbe2float(float *red, float *green, float *blue, unsigned char rgbe[4])
nuclear@0 187 {
nuclear@0 188 float f;
nuclear@0 189
nuclear@0 190 if(rgbe[3]) { /*nonzero pixel */
nuclear@0 191 f = ldexp(1.0, rgbe[3] - (int)(128 + 8));
nuclear@0 192 *red = rgbe[0] * f;
nuclear@0 193 *green = rgbe[1] * f;
nuclear@0 194 *blue = rgbe[2] * f;
nuclear@0 195 } else
nuclear@0 196 *red = *green = *blue = 0.0;
nuclear@0 197 }
nuclear@0 198
nuclear@0 199 #if 0
nuclear@0 200 /* default minimal header. modify if you want more information in header */
nuclear@0 201 static int rgbe_write_header(FILE * fp, int width, int height, rgbe_header_info * info)
nuclear@0 202 {
nuclear@0 203 char *programtype = "RGBE";
nuclear@0 204
nuclear@0 205 if(info && (info->valid & RGBE_VALID_PROGRAMTYPE))
nuclear@0 206 programtype = info->programtype;
nuclear@0 207 if(fprintf(fp, "#?%s\n", programtype) < 0)
nuclear@0 208 return rgbe_error(rgbe_write_error, NULL);
nuclear@0 209 /* The #? is to identify file type, the programtype is optional. */
nuclear@0 210 if(info && (info->valid & RGBE_VALID_GAMMA)) {
nuclear@0 211 if(fprintf(fp, "GAMMA=%g\n", info->gamma) < 0)
nuclear@0 212 return rgbe_error(rgbe_write_error, NULL);
nuclear@0 213 }
nuclear@0 214 if(info && (info->valid & RGBE_VALID_EXPOSURE)) {
nuclear@0 215 if(fprintf(fp, "EXPOSURE=%g\n", info->exposure) < 0)
nuclear@0 216 return rgbe_error(rgbe_write_error, NULL);
nuclear@0 217 }
nuclear@0 218 if(fprintf(fp, "FORMAT=32-bit_rle_rgbe\n\n") < 0)
nuclear@0 219 return rgbe_error(rgbe_write_error, NULL);
nuclear@0 220 if(fprintf(fp, "-Y %d +X %d\n", height, width) < 0)
nuclear@0 221 return rgbe_error(rgbe_write_error, NULL);
nuclear@0 222 return RGBE_RETURN_SUCCESS;
nuclear@0 223 }
nuclear@0 224 #endif
nuclear@0 225
nuclear@0 226 /* minimal header reading. modify if you want to parse more information */
nuclear@0 227 static int rgbe_read_header(struct img_io *io, int *width, int *height, rgbe_header_info * info)
nuclear@0 228 {
nuclear@0 229 char buf[128];
nuclear@0 230 float tempf;
nuclear@0 231 int i;
nuclear@0 232
nuclear@0 233 if(info) {
nuclear@0 234 info->valid = 0;
nuclear@0 235 info->programtype[0] = 0;
nuclear@0 236 info->gamma = info->exposure = 1.0;
nuclear@0 237 }
nuclear@0 238 if(iofgets(buf, sizeof(buf) / sizeof(buf[0]), io) == NULL)
nuclear@0 239 return RGBE_RETURN_FAILURE;/*rgbe_error(rgbe_read_error, NULL);*/
nuclear@0 240 if((buf[0] != '#') || (buf[1] != '?')) {
nuclear@0 241 /* if you want to require the magic token then uncomment the next line */
nuclear@0 242 /*return rgbe_error(rgbe_format_error,"bad initial token"); */
nuclear@0 243 } else if(info) {
nuclear@0 244 info->valid |= RGBE_VALID_PROGRAMTYPE;
nuclear@0 245 for(i = 0; i < sizeof(info->programtype) - 1; i++) {
nuclear@0 246 if((buf[i + 2] == 0) || isspace(buf[i + 2]))
nuclear@0 247 break;
nuclear@0 248 info->programtype[i] = buf[i + 2];
nuclear@0 249 }
nuclear@0 250 info->programtype[i] = 0;
nuclear@0 251 if(iofgets(buf, sizeof(buf) / sizeof(buf[0]), io) == 0)
nuclear@0 252 return rgbe_error(rgbe_read_error, NULL);
nuclear@0 253 }
nuclear@0 254 for(;;) {
nuclear@0 255 if((buf[0] == 0) || (buf[0] == '\n'))
nuclear@0 256 return RGBE_RETURN_FAILURE;/*rgbe_error(rgbe_format_error, "no FORMAT specifier found");*/
nuclear@0 257 else if(strcmp(buf, "FORMAT=32-bit_rle_rgbe\n") == 0)
nuclear@0 258 break; /* format found so break out of loop */
nuclear@0 259 else if(info && (sscanf(buf, "GAMMA=%g", &tempf) == 1)) {
nuclear@0 260 info->gamma = tempf;
nuclear@0 261 info->valid |= RGBE_VALID_GAMMA;
nuclear@0 262 } else if(info && (sscanf(buf, "EXPOSURE=%g", &tempf) == 1)) {
nuclear@0 263 info->exposure = tempf;
nuclear@0 264 info->valid |= RGBE_VALID_EXPOSURE;
nuclear@0 265 }
nuclear@0 266 if(iofgets(buf, sizeof(buf) / sizeof(buf[0]), io) == 0)
nuclear@0 267 return RGBE_RETURN_FAILURE;/*rgbe_error(rgbe_read_error, NULL);*/
nuclear@0 268 }
nuclear@0 269 if(iofgets(buf, sizeof(buf) / sizeof(buf[0]), io) == 0)
nuclear@0 270 return RGBE_RETURN_FAILURE;/*rgbe_error(rgbe_read_error, NULL);*/
nuclear@0 271 if(strcmp(buf, "\n") != 0)
nuclear@0 272 return RGBE_RETURN_FAILURE;/*rgbe_error(rgbe_format_error, "missing blank line after FORMAT specifier");*/
nuclear@0 273 if(iofgets(buf, sizeof(buf) / sizeof(buf[0]), io) == 0)
nuclear@0 274 return RGBE_RETURN_FAILURE;/*rgbe_error(rgbe_read_error, NULL);*/
nuclear@0 275 if(sscanf(buf, "-Y %d +X %d", height, width) < 2)
nuclear@0 276 return RGBE_RETURN_FAILURE;/*rgbe_error(rgbe_format_error, "missing image size specifier");*/
nuclear@0 277 return RGBE_RETURN_SUCCESS;
nuclear@0 278 }
nuclear@0 279
nuclear@0 280 #if 0
nuclear@0 281 /* simple write routine that does not use run length encoding */
nuclear@0 282
nuclear@0 283 /* These routines can be made faster by allocating a larger buffer and
nuclear@0 284 fread-ing and fwrite-ing the data in larger chunks */
nuclear@0 285 static int rgbe_write_pixels(FILE * fp, float *data, int numpixels)
nuclear@0 286 {
nuclear@0 287 unsigned char rgbe[4];
nuclear@0 288
nuclear@0 289 while(numpixels-- > 0) {
nuclear@0 290 float2rgbe(rgbe, data[RGBE_DATA_RED], data[RGBE_DATA_GREEN], data[RGBE_DATA_BLUE]);
nuclear@0 291 data += RGBE_DATA_SIZE;
nuclear@0 292 if(fwrite(rgbe, sizeof(rgbe), 1, fp) < 1)
nuclear@0 293 return rgbe_error(rgbe_write_error, NULL);
nuclear@0 294 }
nuclear@0 295 return RGBE_RETURN_SUCCESS;
nuclear@0 296 }
nuclear@0 297 #endif
nuclear@0 298
nuclear@0 299 /* simple read routine. will not correctly handle run length encoding */
nuclear@0 300 static int rgbe_read_pixels(struct img_io *io, float *data, int numpixels)
nuclear@0 301 {
nuclear@0 302 unsigned char rgbe[4];
nuclear@0 303
nuclear@0 304 while(numpixels-- > 0) {
nuclear@0 305 if(io->read(rgbe, sizeof(rgbe), io->uptr) < 1)
nuclear@0 306 return rgbe_error(rgbe_read_error, NULL);
nuclear@0 307 rgbe2float(&data[RGBE_DATA_RED], &data[RGBE_DATA_GREEN], &data[RGBE_DATA_BLUE], rgbe);
nuclear@0 308 data += RGBE_DATA_SIZE;
nuclear@0 309 }
nuclear@0 310 return RGBE_RETURN_SUCCESS;
nuclear@0 311 }
nuclear@0 312
nuclear@0 313 #if 0
nuclear@0 314 /* The code below is only needed for the run-length encoded files. */
nuclear@0 315
nuclear@0 316 /* Run length encoding adds considerable complexity but does */
nuclear@0 317
nuclear@0 318 /* save some space. For each scanline, each channel (r,g,b,e) is */
nuclear@0 319
nuclear@0 320 /* encoded separately for better compression. */
nuclear@0 321
nuclear@0 322 static int rgbe_write_bytes_rle(struct img_io *io, unsigned char *data, int numbytes)
nuclear@0 323 {
nuclear@0 324 #define MINRUNLENGTH 4
nuclear@0 325 int cur, beg_run, run_count, old_run_count, nonrun_count;
nuclear@0 326 unsigned char buf[2];
nuclear@0 327
nuclear@0 328 cur = 0;
nuclear@0 329 while(cur < numbytes) {
nuclear@0 330 beg_run = cur;
nuclear@0 331 /* find next run of length at least 4 if one exists */
nuclear@0 332 run_count = old_run_count = 0;
nuclear@0 333 while((run_count < MINRUNLENGTH) && (beg_run < numbytes)) {
nuclear@0 334 beg_run += run_count;
nuclear@0 335 old_run_count = run_count;
nuclear@0 336 run_count = 1;
nuclear@0 337 while((beg_run + run_count < numbytes) && (run_count < 127)
nuclear@0 338 && (data[beg_run] == data[beg_run + run_count]))
nuclear@0 339 run_count++;
nuclear@0 340 }
nuclear@0 341 /* if data before next big run is a short run then write it as such */
nuclear@0 342 if((old_run_count > 1) && (old_run_count == beg_run - cur)) {
nuclear@0 343 buf[0] = 128 + old_run_count; /*write short run */
nuclear@0 344 buf[1] = data[cur];
nuclear@0 345 if(fwrite(buf, sizeof(buf[0]) * 2, 1, fp) < 1)
nuclear@0 346 return rgbe_error(rgbe_write_error, NULL);
nuclear@0 347 cur = beg_run;
nuclear@0 348 }
nuclear@0 349 /* write out bytes until we reach the start of the next run */
nuclear@0 350 while(cur < beg_run) {
nuclear@0 351 nonrun_count = beg_run - cur;
nuclear@0 352 if(nonrun_count > 128)
nuclear@0 353 nonrun_count = 128;
nuclear@0 354 buf[0] = nonrun_count;
nuclear@0 355 if(fwrite(buf, sizeof(buf[0]), 1, fp) < 1)
nuclear@0 356 return rgbe_error(rgbe_write_error, NULL);
nuclear@0 357 if(fwrite(&data[cur], sizeof(data[0]) * nonrun_count, 1, fp) < 1)
nuclear@0 358 return rgbe_error(rgbe_write_error, NULL);
nuclear@0 359 cur += nonrun_count;
nuclear@0 360 }
nuclear@0 361 /* write out next run if one was found */
nuclear@0 362 if(run_count >= MINRUNLENGTH) {
nuclear@0 363 buf[0] = 128 + run_count;
nuclear@0 364 buf[1] = data[beg_run];
nuclear@0 365 if(fwrite(buf, sizeof(buf[0]) * 2, 1, fp) < 1)
nuclear@0 366 return rgbe_error(rgbe_write_error, NULL);
nuclear@0 367 cur += run_count;
nuclear@0 368 }
nuclear@0 369 }
nuclear@0 370 return RGBE_RETURN_SUCCESS;
nuclear@0 371 #undef MINRUNLENGTH
nuclear@0 372 }
nuclear@0 373
nuclear@0 374 static int rgbe_write_pixels_rle(struct img_io *io, float *data, int scanline_width, int num_scanlines)
nuclear@0 375 {
nuclear@0 376 unsigned char rgbe[4];
nuclear@0 377 unsigned char *buffer;
nuclear@0 378 int i, err;
nuclear@0 379
nuclear@0 380 if((scanline_width < 8) || (scanline_width > 0x7fff))
nuclear@0 381 /* run length encoding is not allowed so write flat */
nuclear@0 382 return rgbe_write_pixels(io, data, scanline_width * num_scanlines);
nuclear@0 383 buffer = (unsigned char *)malloc(sizeof(unsigned char) * 4 * scanline_width);
nuclear@0 384 if(buffer == NULL)
nuclear@0 385 /* no buffer space so write flat */
nuclear@0 386 return rgbe_write_pixels(fp, data, scanline_width * num_scanlines);
nuclear@0 387 while(num_scanlines-- > 0) {
nuclear@0 388 rgbe[0] = 2;
nuclear@0 389 rgbe[1] = 2;
nuclear@0 390 rgbe[2] = scanline_width >> 8;
nuclear@0 391 rgbe[3] = scanline_width & 0xFF;
nuclear@0 392 if(fwrite(rgbe, sizeof(rgbe), 1, fp) < 1) {
nuclear@0 393 free(buffer);
nuclear@0 394 return rgbe_error(rgbe_write_error, NULL);
nuclear@0 395 }
nuclear@0 396 for(i = 0; i < scanline_width; i++) {
nuclear@0 397 float2rgbe(rgbe, data[RGBE_DATA_RED], data[RGBE_DATA_GREEN], data[RGBE_DATA_BLUE]);
nuclear@0 398 buffer[i] = rgbe[0];
nuclear@0 399 buffer[i + scanline_width] = rgbe[1];
nuclear@0 400 buffer[i + 2 * scanline_width] = rgbe[2];
nuclear@0 401 buffer[i + 3 * scanline_width] = rgbe[3];
nuclear@0 402 data += RGBE_DATA_SIZE;
nuclear@0 403 }
nuclear@0 404 /* write out each of the four channels separately run length encoded */
nuclear@0 405 /* first red, then green, then blue, then exponent */
nuclear@0 406 for(i = 0; i < 4; i++) {
nuclear@0 407 if((err = rgbe_write_bytes_rle(fp, &buffer[i * scanline_width],
nuclear@0 408 scanline_width)) != RGBE_RETURN_SUCCESS) {
nuclear@0 409 free(buffer);
nuclear@0 410 return err;
nuclear@0 411 }
nuclear@0 412 }
nuclear@0 413 }
nuclear@0 414 free(buffer);
nuclear@0 415 return RGBE_RETURN_SUCCESS;
nuclear@0 416 }
nuclear@0 417 #endif
nuclear@0 418
nuclear@0 419 static int rgbe_read_pixels_rle(struct img_io *io, float *data, int scanline_width, int num_scanlines)
nuclear@0 420 {
nuclear@0 421 unsigned char rgbe[4], *scanline_buffer, *ptr, *ptr_end;
nuclear@0 422 int i, count;
nuclear@0 423 unsigned char buf[2];
nuclear@0 424
nuclear@0 425 if((scanline_width < 8) || (scanline_width > 0x7fff))
nuclear@0 426 /* run length encoding is not allowed so read flat */
nuclear@0 427 return rgbe_read_pixels(io, data, scanline_width * num_scanlines);
nuclear@0 428 scanline_buffer = NULL;
nuclear@0 429 /* read in each successive scanline */
nuclear@0 430 while(num_scanlines > 0) {
nuclear@0 431 if(io->read(rgbe, sizeof(rgbe), io->uptr) < 1) {
nuclear@0 432 free(scanline_buffer);
nuclear@0 433 return rgbe_error(rgbe_read_error, NULL);
nuclear@0 434 }
nuclear@0 435 if((rgbe[0] != 2) || (rgbe[1] != 2) || (rgbe[2] & 0x80)) {
nuclear@0 436 /* this file is not run length encoded */
nuclear@0 437 rgbe2float(&data[0], &data[1], &data[2], rgbe);
nuclear@0 438 data += RGBE_DATA_SIZE;
nuclear@0 439 free(scanline_buffer);
nuclear@0 440 return rgbe_read_pixels(io, data, scanline_width * num_scanlines - 1);
nuclear@0 441 }
nuclear@0 442 if((((int)rgbe[2]) << 8 | rgbe[3]) != scanline_width) {
nuclear@0 443 free(scanline_buffer);
nuclear@0 444 return rgbe_error(rgbe_format_error, "wrong scanline width");
nuclear@0 445 }
nuclear@0 446 if(scanline_buffer == NULL)
nuclear@0 447 scanline_buffer = (unsigned char *)
nuclear@0 448 malloc(sizeof(unsigned char) * 4 * scanline_width);
nuclear@0 449 if(scanline_buffer == NULL)
nuclear@0 450 return rgbe_error(rgbe_memory_error, "unable to allocate buffer space");
nuclear@0 451
nuclear@0 452 ptr = &scanline_buffer[0];
nuclear@0 453 /* read each of the four channels for the scanline into the buffer */
nuclear@0 454 for(i = 0; i < 4; i++) {
nuclear@0 455 ptr_end = &scanline_buffer[(i + 1) * scanline_width];
nuclear@0 456 while(ptr < ptr_end) {
nuclear@0 457 if(io->read(buf, sizeof(buf[0]) * 2, io->uptr) < 1) {
nuclear@0 458 free(scanline_buffer);
nuclear@0 459 return rgbe_error(rgbe_read_error, NULL);
nuclear@0 460 }
nuclear@0 461 if(buf[0] > 128) {
nuclear@0 462 /* a run of the same value */
nuclear@0 463 count = buf[0] - 128;
nuclear@0 464 if((count == 0) || (count > ptr_end - ptr)) {
nuclear@0 465 free(scanline_buffer);
nuclear@0 466 return rgbe_error(rgbe_format_error, "bad scanline data");
nuclear@0 467 }
nuclear@0 468 while(count-- > 0)
nuclear@0 469 *ptr++ = buf[1];
nuclear@0 470 } else {
nuclear@0 471 /* a non-run */
nuclear@0 472 count = buf[0];
nuclear@0 473 if((count == 0) || (count > ptr_end - ptr)) {
nuclear@0 474 free(scanline_buffer);
nuclear@0 475 return rgbe_error(rgbe_format_error, "bad scanline data");
nuclear@0 476 }
nuclear@0 477 *ptr++ = buf[1];
nuclear@0 478 if(--count > 0) {
nuclear@0 479 if(io->read(ptr, sizeof(*ptr) * count, io->uptr) < 1) {
nuclear@0 480 free(scanline_buffer);
nuclear@0 481 return rgbe_error(rgbe_read_error, NULL);
nuclear@0 482 }
nuclear@0 483 ptr += count;
nuclear@0 484 }
nuclear@0 485 }
nuclear@0 486 }
nuclear@0 487 }
nuclear@0 488 /* now convert data from buffer into floats */
nuclear@0 489 for(i = 0; i < scanline_width; i++) {
nuclear@0 490 rgbe[0] = scanline_buffer[i];
nuclear@0 491 rgbe[1] = scanline_buffer[i + scanline_width];
nuclear@0 492 rgbe[2] = scanline_buffer[i + 2 * scanline_width];
nuclear@0 493 rgbe[3] = scanline_buffer[i + 3 * scanline_width];
nuclear@0 494 rgbe2float(&data[RGBE_DATA_RED], &data[RGBE_DATA_GREEN], &data[RGBE_DATA_BLUE], rgbe);
nuclear@0 495 data += RGBE_DATA_SIZE;
nuclear@0 496 }
nuclear@0 497 num_scanlines--;
nuclear@0 498 }
nuclear@0 499 free(scanline_buffer);
nuclear@0 500 return RGBE_RETURN_SUCCESS;
nuclear@0 501 }