Thu Jan 21 17:16:48 2016 UTC ()
remove malloc casts.


(christos)
diff -r1.6 -r1.7 src/sys/dev/stbi/stb_image.c

cvs diff -r1.6 -r1.7 src/sys/dev/stbi/stb_image.c (expand / switch to unified diff)

--- src/sys/dev/stbi/stb_image.c 2013/09/15 14:06:10 1.6
+++ src/sys/dev/stbi/stb_image.c 2016/01/21 17:16:48 1.7
@@ -420,27 +420,27 @@ extern int stbi_gif_info_from_file  @@ -420,27 +420,27 @@ extern int stbi_gif_info_from_file
420 420
421#ifndef STBI_HEADER_FILE_ONLY 421#ifndef STBI_HEADER_FILE_ONLY
422 422
423#ifndef STBI_NO_HDR 423#ifndef STBI_NO_HDR
424#include <math.h> // ldexp 424#include <math.h> // ldexp
425#include <string.h> // strcmp 425#include <string.h> // strcmp
426#endif 426#endif
427 427
428#ifndef STBI_NO_STDIO 428#ifndef STBI_NO_STDIO
429#include <stdio.h> 429#include <stdio.h>
430#endif 430#endif
431#ifdef _KERNEL 431#ifdef _KERNEL
432#include <sys/cdefs.h> 432#include <sys/cdefs.h>
433__KERNEL_RCSID(0, "$NetBSD: stb_image.c,v 1.6 2013/09/15 14:06:10 martin Exp $"); 433__KERNEL_RCSID(0, "$NetBSD: stb_image.c,v 1.7 2016/01/21 17:16:48 christos Exp $");
434#include <sys/param.h> 434#include <sys/param.h>
435#include <sys/systm.h> 435#include <sys/systm.h>
436#include <sys/kernel.h> 436#include <sys/kernel.h>
437#include <sys/types.h> 437#include <sys/types.h>
438#include <sys/malloc.h> 438#include <sys/malloc.h>
439#else 439#else
440#include <stdlib.h> 440#include <stdlib.h>
441#include <memory.h> 441#include <memory.h>
442#include <assert.h> 442#include <assert.h>
443#include <stdarg.h> 443#include <stdarg.h>
444#endif 444#endif
445 445
446#ifdef _KERNEL 446#ifdef _KERNEL
@@ -975,27 +975,27 @@ static uint32 get32le(stbi *s) @@ -975,27 +975,27 @@ static uint32 get32le(stbi *s)
975static uint8 compute_y(int r, int g, int b) 975static uint8 compute_y(int r, int g, int b)
976{ 976{
977 return (uint8) (((r*77) + (g*150) + (29*b)) >> 8); 977 return (uint8) (((r*77) + (g*150) + (29*b)) >> 8);
978} 978}
979 979
980static unsigned char *convert_format(unsigned char *data, int img_n, int req_comp, uint x, uint y) 980static unsigned char *convert_format(unsigned char *data, int img_n, int req_comp, uint x, uint y)
981{ 981{
982 int i,j; 982 int i,j;
983 unsigned char *good; 983 unsigned char *good;
984 984
985 if (req_comp == img_n) return data; 985 if (req_comp == img_n) return data;
986 assert(req_comp >= 1 && req_comp <= 4); 986 assert(req_comp >= 1 && req_comp <= 4);
987 987
988 good = (unsigned char *) MALLOC(req_comp * x * y); 988 good = MALLOC(req_comp * x * y);
989 if (good == NULL) { 989 if (good == NULL) {
990 FREE(data); 990 FREE(data);
991 return epuc("outofmem", "Out of memory"); 991 return epuc("outofmem", "Out of memory");
992 } 992 }
993 993
994 for (j=0; j < (int) y; ++j) { 994 for (j=0; j < (int) y; ++j) {
995 unsigned char *src = data + j * x * img_n ; 995 unsigned char *src = data + j * x * img_n ;
996 unsigned char *dest = good + j * x * req_comp; 996 unsigned char *dest = good + j * x * req_comp;
997 997
998 #define COMBO(a,b) ((a)*8+(b)) 998 #define COMBO(a,b) ((a)*8+(b))
999 #define CASE(a,b) case COMBO(a,b): for(i=x-1; i >= 0; --i, src += a, dest += b) 999 #define CASE(a,b) case COMBO(a,b): for(i=x-1; i >= 0; --i, src += a, dest += b)
1000 // convert source image with img_n components to one with req_comp components; 1000 // convert source image with img_n components to one with req_comp components;
1001 // avoid switch per pixel, so use switch per scanline and massive macros 1001 // avoid switch per pixel, so use switch per scanline and massive macros
@@ -1015,45 +1015,45 @@ static unsigned char *convert_format(uns @@ -1015,45 +1015,45 @@ static unsigned char *convert_format(uns
1015 default: assert(0); 1015 default: assert(0);
1016 } 1016 }
1017 #undef CASE 1017 #undef CASE
1018 } 1018 }
1019 1019
1020 FREE(data); 1020 FREE(data);
1021 return good; 1021 return good;
1022} 1022}
1023 1023
1024#ifndef STBI_NO_HDR 1024#ifndef STBI_NO_HDR
1025static float *ldr_to_hdr(stbi_uc *data, int x, int y, int comp) 1025static float *ldr_to_hdr(stbi_uc *data, int x, int y, int comp)
1026{ 1026{
1027 int i,k,n; 1027 int i,k,n;
1028 float *output = (float *) MALLOC(x * y * comp * sizeof(float)); 1028 float *output = MALLOC(x * y * comp * sizeof(float));
1029 if (output == NULL) { FREE(data); return epf("outofmem", "Out of memory"); } 1029 if (output == NULL) { FREE(data); return epf("outofmem", "Out of memory"); }
1030 // compute number of non-alpha components 1030 // compute number of non-alpha components
1031 if (comp & 1) n = comp; else n = comp-1; 1031 if (comp & 1) n = comp; else n = comp-1;
1032 for (i=0; i < x*y; ++i) { 1032 for (i=0; i < x*y; ++i) {
1033 for (k=0; k < n; ++k) { 1033 for (k=0; k < n; ++k) {
1034 output[i*comp + k] = (float) pow(data[i*comp+k]/255.0f, l2h_gamma) * l2h_scale; 1034 output[i*comp + k] = (float) pow(data[i*comp+k]/255.0f, l2h_gamma) * l2h_scale;
1035 } 1035 }
1036 if (k < comp) output[i*comp + k] = data[i*comp+k]/255.0f; 1036 if (k < comp) output[i*comp + k] = data[i*comp+k]/255.0f;
1037 } 1037 }
1038 FREE(data); 1038 FREE(data);
1039 return output; 1039 return output;
1040} 1040}
1041 1041
1042#define float2int(x) ((int) (x)) 1042#define float2int(x) ((int) (x))
1043static stbi_uc *hdr_to_ldr(float *data, int x, int y, int comp) 1043static stbi_uc *hdr_to_ldr(float *data, int x, int y, int comp)
1044{ 1044{
1045 int i,k,n; 1045 int i,k,n;
1046 stbi_uc *output = (stbi_uc *) MALLOC(x * y * comp); 1046 stbi_uc *output = MALLOC(x * y * comp);
1047 if (output == NULL) { FREE(data); return epuc("outofmem", "Out of memory"); } 1047 if (output == NULL) { FREE(data); return epuc("outofmem", "Out of memory"); }
1048 // compute number of non-alpha components 1048 // compute number of non-alpha components
1049 if (comp & 1) n = comp; else n = comp-1; 1049 if (comp & 1) n = comp; else n = comp-1;
1050 for (i=0; i < x*y; ++i) { 1050 for (i=0; i < x*y; ++i) {
1051 for (k=0; k < n; ++k) { 1051 for (k=0; k < n; ++k) {
1052 float z = (float) pow(data[i*comp+k]*h2l_scale_i, h2l_gamma_i) * 255 + 0.5f; 1052 float z = (float) pow(data[i*comp+k]*h2l_scale_i, h2l_gamma_i) * 255 + 0.5f;
1053 if (z < 0) z = 0; 1053 if (z < 0) z = 0;
1054 if (z > 255) z = 255; 1054 if (z > 255) z = 255;
1055 output[i*comp + k] = (uint8) float2int(z); 1055 output[i*comp + k] = (uint8) float2int(z);
1056 } 1056 }
1057 if (k < comp) { 1057 if (k < comp) {
1058 float z = data[i*comp+k] * 255 + 0.5f; 1058 float z = data[i*comp+k] * 255 + 0.5f;
1059 if (z < 0) z = 0; 1059 if (z < 0) z = 0;
@@ -1973,45 +1973,45 @@ static uint8 *load_jpeg_image(jpeg *z, i @@ -1973,45 +1973,45 @@ static uint8 *load_jpeg_image(jpeg *z, i
1973 { 1973 {
1974 int k; 1974 int k;
1975 uint i,j; 1975 uint i,j;
1976 uint8 *output; 1976 uint8 *output;
1977 uint8 *coutput[4]; 1977 uint8 *coutput[4];
1978 1978
1979 stbi_resample res_comp[4]; 1979 stbi_resample res_comp[4];
1980 1980
1981 for (k=0; k < decode_n; ++k) { 1981 for (k=0; k < decode_n; ++k) {
1982 stbi_resample *r = &res_comp[k]; 1982 stbi_resample *r = &res_comp[k];
1983 1983
1984 // allocate line buffer big enough for upsampling off the edges 1984 // allocate line buffer big enough for upsampling off the edges
1985 // with upsample factor of 4 1985 // with upsample factor of 4
1986 z->img_comp[k].linebuf = (uint8 *) MALLOC(z->s.img_x + 3); 1986 z->img_comp[k].linebuf = MALLOC(z->s.img_x + 3);
1987 if (!z->img_comp[k].linebuf) { cleanup_jpeg(z); return epuc("outofmem", "Out of memory"); } 1987 if (!z->img_comp[k].linebuf) { cleanup_jpeg(z); return epuc("outofmem", "Out of memory"); }
1988 1988
1989 r->hs = z->img_h_max / z->img_comp[k].h; 1989 r->hs = z->img_h_max / z->img_comp[k].h;
1990 r->vs = z->img_v_max / z->img_comp[k].v; 1990 r->vs = z->img_v_max / z->img_comp[k].v;
1991 r->ystep = r->vs >> 1; 1991 r->ystep = r->vs >> 1;
1992 r->w_lores = (z->s.img_x + r->hs-1) / r->hs; 1992 r->w_lores = (z->s.img_x + r->hs-1) / r->hs;
1993 r->ypos = 0; 1993 r->ypos = 0;
1994 r->line0 = r->line1 = z->img_comp[k].data; 1994 r->line0 = r->line1 = z->img_comp[k].data;
1995 1995
1996 if (r->hs == 1 && r->vs == 1) r->resample = resample_row_1; 1996 if (r->hs == 1 && r->vs == 1) r->resample = resample_row_1;
1997 else if (r->hs == 1 && r->vs == 2) r->resample = resample_row_v_2; 1997 else if (r->hs == 1 && r->vs == 2) r->resample = resample_row_v_2;
1998 else if (r->hs == 2 && r->vs == 1) r->resample = resample_row_h_2; 1998 else if (r->hs == 2 && r->vs == 1) r->resample = resample_row_h_2;
1999 else if (r->hs == 2 && r->vs == 2) r->resample = resample_row_hv_2; 1999 else if (r->hs == 2 && r->vs == 2) r->resample = resample_row_hv_2;
2000 else r->resample = resample_row_generic; 2000 else r->resample = resample_row_generic;
2001 } 2001 }
2002 2002
2003 // can't error after this so, this is safe 2003 // can't error after this so, this is safe
2004 output = (uint8 *) MALLOC(n * z->s.img_x * z->s.img_y + 1); 2004 output = MALLOC(n * z->s.img_x * z->s.img_y + 1);
2005 if (!output) { cleanup_jpeg(z); return epuc("outofmem", "Out of memory"); } 2005 if (!output) { cleanup_jpeg(z); return epuc("outofmem", "Out of memory"); }
2006 2006
2007 // now go ahead and resample 2007 // now go ahead and resample
2008 for (j=0; j < z->s.img_y; ++j) { 2008 for (j=0; j < z->s.img_y; ++j) {
2009 uint8 *out = output + n * z->s.img_x * j; 2009 uint8 *out = output + n * z->s.img_x * j;
2010 for (k=0; k < decode_n; ++k) { 2010 for (k=0; k < decode_n; ++k) {
2011 stbi_resample *r = &res_comp[k]; 2011 stbi_resample *r = &res_comp[k];
2012 int y_bot = r->ystep >= (r->vs >> 1); 2012 int y_bot = r->ystep >= (r->vs >> 1);
2013 coutput[k] = r->resample(z->img_comp[k].linebuf, 2013 coutput[k] = r->resample(z->img_comp[k].linebuf,
2014 y_bot ? r->line1 : r->line0, 2014 y_bot ? r->line1 : r->line0,
2015 y_bot ? r->line0 : r->line1, 2015 y_bot ? r->line0 : r->line1,
2016 r->w_lores, r->hs); 2016 r->w_lores, r->hs);
2017 if (++r->ystep >= r->vs) { 2017 if (++r->ystep >= r->vs) {
@@ -2064,27 +2064,27 @@ unsigned char *stbi_jpeg_load(char const @@ -2064,27 +2064,27 @@ unsigned char *stbi_jpeg_load(char const
2064 unsigned char *data; 2064 unsigned char *data;
2065 FILE *f = fopen(filename, "rb"); 2065 FILE *f = fopen(filename, "rb");
2066 if (!f) return NULL; 2066 if (!f) return NULL;
2067 data = stbi_jpeg_load_from_file(f,x,y,comp,req_comp); 2067 data = stbi_jpeg_load_from_file(f,x,y,comp,req_comp);
2068 fclose(f); 2068 fclose(f);
2069 return data; 2069 return data;
2070} 2070}
2071#endif 2071#endif
2072 2072
2073unsigned char *stbi_jpeg_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp) 2073unsigned char *stbi_jpeg_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)
2074{ 2074{
2075 #ifdef STBI_SMALL_STACK 2075 #ifdef STBI_SMALL_STACK
2076 unsigned char *result; 2076 unsigned char *result;
2077 jpeg *j = (jpeg *) MALLOC(sizeof(*j)); 2077 jpeg *j = MALLOC(sizeof(*j));
2078 start_mem(&j->s, buffer, len); 2078 start_mem(&j->s, buffer, len);
2079 result = load_jpeg_image(j,x,y,comp,req_comp); 2079 result = load_jpeg_image(j,x,y,comp,req_comp);
2080 FREE(j); 2080 FREE(j);
2081 return result; 2081 return result;
2082 #else 2082 #else
2083 jpeg j; 2083 jpeg j;
2084 start_mem(&j.s, buffer,len); 2084 start_mem(&j.s, buffer,len);
2085 return load_jpeg_image(&j, x,y,comp,req_comp); 2085 return load_jpeg_image(&j, x,y,comp,req_comp);
2086 #endif 2086 #endif
2087} 2087}
2088 2088
2089static int stbi_jpeg_info_raw(jpeg *j, int *x, int *y, int *comp) 2089static int stbi_jpeg_info_raw(jpeg *j, int *x, int *y, int *comp)
2090{ 2090{
@@ -2502,75 +2502,75 @@ static int parse_zlib(zbuf *a, int parse @@ -2502,75 +2502,75 @@ static int parse_zlib(zbuf *a, int parse
2502static int do_zlib(zbuf *a, char *obuf, int olen, int exp, int parse_header) 2502static int do_zlib(zbuf *a, char *obuf, int olen, int exp, int parse_header)
2503{ 2503{
2504 a->zout_start = obuf; 2504 a->zout_start = obuf;
2505 a->zout = obuf; 2505 a->zout = obuf;
2506 a->zout_end = obuf + olen; 2506 a->zout_end = obuf + olen;
2507 a->z_expandable = exp; 2507 a->z_expandable = exp;
2508 2508
2509 return parse_zlib(a, parse_header); 2509 return parse_zlib(a, parse_header);
2510} 2510}
2511 2511
2512char *stbi_zlib_decode_malloc_guesssize(const char * buffer, int len, int initial_size, int *outlen) 2512char *stbi_zlib_decode_malloc_guesssize(const char * buffer, int len, int initial_size, int *outlen)
2513{ 2513{
2514 zbuf a; 2514 zbuf a;
2515 char *p = (char *) MALLOC(initial_size); 2515 char *p = MALLOC(initial_size);
2516 if (p == NULL) return NULL; 2516 if (p == NULL) return NULL;
2517 a.zbuffer = (uint8 const *) buffer; 2517 a.zbuffer = (uint8 const *) buffer;
2518 a.zbuffer_end = (uint8 const *) buffer + len; 2518 a.zbuffer_end = (uint8 const *) buffer + len;
2519 if (do_zlib(&a, p, initial_size, 1, 1)) { 2519 if (do_zlib(&a, p, initial_size, 1, 1)) {
2520 if (outlen) *outlen = (int) (a.zout - a.zout_start); 2520 if (outlen) *outlen = (int) (a.zout - a.zout_start);
2521 return a.zout_start; 2521 return a.zout_start;
2522 } else { 2522 } else {
2523 FREE(a.zout_start); 2523 FREE(a.zout_start);
2524 return NULL; 2524 return NULL;
2525 } 2525 }
2526} 2526}
2527 2527
2528char *stbi_zlib_decode_malloc(char const *buffer, int len, int *outlen) 2528char *stbi_zlib_decode_malloc(char const *buffer, int len, int *outlen)
2529{ 2529{
2530 return stbi_zlib_decode_malloc_guesssize(buffer, len, 16384, outlen); 2530 return stbi_zlib_decode_malloc_guesssize(buffer, len, 16384, outlen);
2531} 2531}
2532 2532
2533char *stbi_zlib_decode_malloc_guesssize_headerflag(const char *buffer, int len, int initial_size, int *outlen, int parse_header) 2533char *stbi_zlib_decode_malloc_guesssize_headerflag(const char *buffer, int len, int initial_size, int *outlen, int parse_header)
2534{ 2534{
2535 zbuf a; 2535 zbuf a;
2536 char *p = (char *) MALLOC(initial_size); 2536 char *p = MALLOC(initial_size);
2537 if (p == NULL) return NULL; 2537 if (p == NULL) return NULL;
2538 a.zbuffer = (uint8 const *) buffer; 2538 a.zbuffer = (uint8 const *) buffer;
2539 a.zbuffer_end = (uint8 const *) buffer + len; 2539 a.zbuffer_end = (uint8 const *) buffer + len;
2540 if (do_zlib(&a, p, initial_size, 1, parse_header)) { 2540 if (do_zlib(&a, p, initial_size, 1, parse_header)) {
2541 if (outlen) *outlen = (int) (a.zout - a.zout_start); 2541 if (outlen) *outlen = (int) (a.zout - a.zout_start);
2542 return a.zout_start; 2542 return a.zout_start;
2543 } else { 2543 } else {
2544 FREE(a.zout_start); 2544 FREE(a.zout_start);
2545 return NULL; 2545 return NULL;
2546 } 2546 }
2547} 2547}
2548 2548
2549int stbi_zlib_decode_buffer(char *obuffer, int olen, char const *ibuffer, int ilen) 2549int stbi_zlib_decode_buffer(char *obuffer, int olen, char const *ibuffer, int ilen)
2550{ 2550{
2551 zbuf a; 2551 zbuf a;
2552 a.zbuffer = (uint8 const *) ibuffer; 2552 a.zbuffer = (uint8 const *) ibuffer;
2553 a.zbuffer_end = (uint8 const *) ibuffer + ilen; 2553 a.zbuffer_end = (uint8 const *) ibuffer + ilen;
2554 if (do_zlib(&a, obuffer, olen, 0, 1)) 2554 if (do_zlib(&a, obuffer, olen, 0, 1))
2555 return (int) (a.zout - a.zout_start); 2555 return (int) (a.zout - a.zout_start);
2556 else 2556 else
2557 return -1; 2557 return -1;
2558} 2558}
2559 2559
2560char *stbi_zlib_decode_noheader_malloc(char const *buffer, int len, int *outlen) 2560char *stbi_zlib_decode_noheader_malloc(char const *buffer, int len, int *outlen)
2561{ 2561{
2562 zbuf a; 2562 zbuf a;
2563 char *p = (char *) MALLOC(16384); 2563 char *p = MALLOC(16384);
2564 if (p == NULL) return NULL; 2564 if (p == NULL) return NULL;
2565 a.zbuffer = (uint8 const *) buffer; 2565 a.zbuffer = (uint8 const *) buffer;
2566 a.zbuffer_end = (uint8 const *) buffer+len; 2566 a.zbuffer_end = (uint8 const *) buffer+len;
2567 if (do_zlib(&a, p, 16384, 1, 0)) { 2567 if (do_zlib(&a, p, 16384, 1, 0)) {
2568 if (outlen) *outlen = (int) (a.zout - a.zout_start); 2568 if (outlen) *outlen = (int) (a.zout - a.zout_start);
2569 return a.zout_start; 2569 return a.zout_start;
2570 } else { 2570 } else {
2571 FREE(a.zout_start); 2571 FREE(a.zout_start);
2572 return NULL; 2572 return NULL;
2573 } 2573 }
2574} 2574}
2575 2575
2576int stbi_zlib_decode_noheader_buffer(char *obuffer, int olen, const char *ibuffer, int ilen) 2576int stbi_zlib_decode_noheader_buffer(char *obuffer, int olen, const char *ibuffer, int ilen)
@@ -2647,27 +2647,27 @@ static int paeth(int a, int b, int c) @@ -2647,27 +2647,27 @@ static int paeth(int a, int b, int c)
2647 if (pb <= pc) return b; 2647 if (pb <= pc) return b;
2648 return c; 2648 return c;
2649} 2649}
2650 2650
2651// create the png data from post-deflated data 2651// create the png data from post-deflated data
2652static int create_png_image_raw(png *a, uint8 *raw, uint32 raw_len, int out_n, uint32 x, uint32 y) 2652static int create_png_image_raw(png *a, uint8 *raw, uint32 raw_len, int out_n, uint32 x, uint32 y)
2653{ 2653{
2654 stbi *s = &a->s; 2654 stbi *s = &a->s;
2655 uint32 i,j,stride = x*out_n; 2655 uint32 i,j,stride = x*out_n;
2656 int k; 2656 int k;
2657 int img_n = s->img_n; // copy it into a local for later 2657 int img_n = s->img_n; // copy it into a local for later
2658 assert(out_n == s->img_n || out_n == s->img_n+1); 2658 assert(out_n == s->img_n || out_n == s->img_n+1);
2659 if (stbi_png_partial) y = 1; 2659 if (stbi_png_partial) y = 1;
2660 a->out = (uint8 *) MALLOC(x * y * out_n); 2660 a->out = MALLOC(x * y * out_n);
2661 if (!a->out) return e("outofmem", "Out of memory"); 2661 if (!a->out) return e("outofmem", "Out of memory");
2662 if (!stbi_png_partial) { 2662 if (!stbi_png_partial) {
2663 if (s->img_x == x && s->img_y == y) { 2663 if (s->img_x == x && s->img_y == y) {
2664 if (raw_len != (img_n * x + 1) * y) return e("not enough pixels","Corrupt PNG"); 2664 if (raw_len != (img_n * x + 1) * y) return e("not enough pixels","Corrupt PNG");
2665 } else { // interlaced: 2665 } else { // interlaced:
2666 if (raw_len < (img_n * x + 1) * y) return e("not enough pixels","Corrupt PNG"); 2666 if (raw_len < (img_n * x + 1) * y) return e("not enough pixels","Corrupt PNG");
2667 } 2667 }
2668 } 2668 }
2669 for (j=0; j < y; ++j) { 2669 for (j=0; j < y; ++j) {
2670 uint8 *cur = a->out + stride*j; 2670 uint8 *cur = a->out + stride*j;
2671 uint8 *prior = cur - stride; 2671 uint8 *prior = cur - stride;
2672 int filter = *raw++; 2672 int filter = *raw++;
2673 if (filter > 4) return e("invalid filter","Corrupt PNG"); 2673 if (filter > 4) return e("invalid filter","Corrupt PNG");
@@ -2727,27 +2727,27 @@ static int create_png_image_raw(png *a,  @@ -2727,27 +2727,27 @@ static int create_png_image_raw(png *a,
2727} 2727}
2728 2728
2729static int create_png_image(png *a, uint8 *raw, uint32 raw_len, int out_n, int interlaced) 2729static int create_png_image(png *a, uint8 *raw, uint32 raw_len, int out_n, int interlaced)
2730{ 2730{
2731 uint8 *final; 2731 uint8 *final;
2732 int p; 2732 int p;
2733 int save; 2733 int save;
2734 if (!interlaced) 2734 if (!interlaced)
2735 return create_png_image_raw(a, raw, raw_len, out_n, a->s.img_x, a->s.img_y); 2735 return create_png_image_raw(a, raw, raw_len, out_n, a->s.img_x, a->s.img_y);
2736 save = stbi_png_partial; 2736 save = stbi_png_partial;
2737 stbi_png_partial = 0; 2737 stbi_png_partial = 0;
2738 2738
2739 // de-interlacing 2739 // de-interlacing
2740 final = (uint8 *) MALLOC(a->s.img_x * a->s.img_y * out_n); 2740 final = MALLOC(a->s.img_x * a->s.img_y * out_n);
2741 for (p=0; p < 7; ++p) { 2741 for (p=0; p < 7; ++p) {
2742 int xorig[] = { 0,4,0,2,0,1,0 }; 2742 int xorig[] = { 0,4,0,2,0,1,0 };
2743 int yorig[] = { 0,0,4,0,2,0,1 }; 2743 int yorig[] = { 0,0,4,0,2,0,1 };
2744 int xspc[] = { 8,8,4,4,2,2,1 }; 2744 int xspc[] = { 8,8,4,4,2,2,1 };
2745 int yspc[] = { 8,8,8,4,4,2,2 }; 2745 int yspc[] = { 8,8,8,4,4,2,2 };
2746 int i,j,x,y; 2746 int i,j,x,y;
2747 // pass1_x[4] = 0, pass1_x[5] = 1, pass1_x[12] = 1 2747 // pass1_x[4] = 0, pass1_x[5] = 1, pass1_x[12] = 1
2748 x = (a->s.img_x - xorig[p] + xspc[p]-1) / xspc[p]; 2748 x = (a->s.img_x - xorig[p] + xspc[p]-1) / xspc[p];
2749 y = (a->s.img_y - yorig[p] + yspc[p]-1) / yspc[p]; 2749 y = (a->s.img_y - yorig[p] + yspc[p]-1) / yspc[p];
2750 if (x && y) { 2750 if (x && y) {
2751 if (!create_png_image_raw(a, raw, raw_len, out_n, x, y)) { 2751 if (!create_png_image_raw(a, raw, raw_len, out_n, x, y)) {
2752 FREE(final); 2752 FREE(final);
2753 return 0; 2753 return 0;
@@ -2787,27 +2787,27 @@ static int compute_transparency(png *z,  @@ -2787,27 +2787,27 @@ static int compute_transparency(png *z,
2787 if (p[0] == tc[0] && p[1] == tc[1] && p[2] == tc[2]) 2787 if (p[0] == tc[0] && p[1] == tc[1] && p[2] == tc[2])
2788 p[3] = 0; 2788 p[3] = 0;
2789 p += 4; 2789 p += 4;
2790 } 2790 }
2791 } 2791 }
2792 return 1; 2792 return 1;
2793} 2793}
2794 2794
2795static int expand_palette(png *a, uint8 *palette, int len, int pal_img_n) 2795static int expand_palette(png *a, uint8 *palette, int len, int pal_img_n)
2796{ 2796{
2797 uint32 i, pixel_count = a->s.img_x * a->s.img_y; 2797 uint32 i, pixel_count = a->s.img_x * a->s.img_y;
2798 uint8 *p, *temp_out, *orig = a->out; 2798 uint8 *p, *temp_out, *orig = a->out;
2799 2799
2800 p = (uint8 *) MALLOC(pixel_count * pal_img_n); 2800 p = MALLOC(pixel_count * pal_img_n);
2801 if (p == NULL) return e("outofmem", "Out of memory"); 2801 if (p == NULL) return e("outofmem", "Out of memory");
2802 2802
2803 // between here and FREE(out) below, exiting would leak 2803 // between here and FREE(out) below, exiting would leak
2804 temp_out = p; 2804 temp_out = p;
2805 2805
2806 if (pal_img_n == 3) { 2806 if (pal_img_n == 3) {
2807 for (i=0; i < pixel_count; ++i) { 2807 for (i=0; i < pixel_count; ++i) {
2808 int n = orig[i]*4; 2808 int n = orig[i]*4;
2809 p[0] = palette[n ]; 2809 p[0] = palette[n ];
2810 p[1] = palette[n+1]; 2810 p[1] = palette[n+1];
2811 p[2] = palette[n+2]; 2811 p[2] = palette[n+2];
2812 p += 3; 2812 p += 3;
2813 } 2813 }
@@ -3295,27 +3295,27 @@ static stbi_uc *bmp_load(stbi *s, int *x @@ -3295,27 +3295,27 @@ static stbi_uc *bmp_load(stbi *s, int *x
3295 ma = get32le(s); 3295 ma = get32le(s);
3296 get32le(s); // discard color space 3296 get32le(s); // discard color space
3297 for (i=0; i < 12; ++i) 3297 for (i=0; i < 12; ++i)
3298 get32le(s); // discard color space parameters 3298 get32le(s); // discard color space parameters
3299 } 3299 }
3300 if (bpp < 16) 3300 if (bpp < 16)
3301 psize = (offset - 14 - hsz) >> 2; 3301 psize = (offset - 14 - hsz) >> 2;
3302 } 3302 }
3303 s->img_n = ma ? 4 : 3; 3303 s->img_n = ma ? 4 : 3;
3304 if (req_comp && req_comp >= 3) // we can directly decode 3 or 4 3304 if (req_comp && req_comp >= 3) // we can directly decode 3 or 4
3305 target = req_comp; 3305 target = req_comp;
3306 else 3306 else
3307 target = s->img_n; // if they want monochrome, we'll post-convert 3307 target = s->img_n; // if they want monochrome, we'll post-convert
3308 out = (stbi_uc *) MALLOC(target * s->img_x * s->img_y); 3308 out = MALLOC(target * s->img_x * s->img_y);
3309 if (!out) return epuc("outofmem", "Out of memory"); 3309 if (!out) return epuc("outofmem", "Out of memory");
3310 if (bpp < 16) { 3310 if (bpp < 16) {
3311 int z=0; 3311 int z=0;
3312 if (psize == 0 || psize > 256) { FREE(out); return epuc("invalid", "Corrupt BMP"); } 3312 if (psize == 0 || psize > 256) { FREE(out); return epuc("invalid", "Corrupt BMP"); }
3313 for (i=0; i < psize; ++i) { 3313 for (i=0; i < psize; ++i) {
3314 pal[i][2] = get8u(s); 3314 pal[i][2] = get8u(s);
3315 pal[i][1] = get8u(s); 3315 pal[i][1] = get8u(s);
3316 pal[i][0] = get8u(s); 3316 pal[i][0] = get8u(s);
3317 if (hsz != 12) get8(s); 3317 if (hsz != 12) get8(s);
3318 pal[i][3] = 255; 3318 pal[i][3] = 255;
3319 } 3319 }
3320 skip(s, offset - 14 - hsz - psize * (hsz == 12 ? 3 : 4)); 3320 skip(s, offset - 14 - hsz - psize * (hsz == 12 ? 3 : 4));
3321 if (bpp == 4) width = (s->img_x + 1) >> 1; 3321 if (bpp == 4) width = (s->img_x + 1) >> 1;
@@ -3584,37 +3584,37 @@ static stbi_uc *tga_load(stbi *s, int *x @@ -3584,37 +3584,37 @@ static stbi_uc *tga_load(stbi *s, int *x
3584 // tga info 3584 // tga info
3585 *x = tga_width; 3585 *x = tga_width;
3586 *y = tga_height; 3586 *y = tga_height;
3587 if ( (req_comp < 1) || (req_comp > 4) ) 3587 if ( (req_comp < 1) || (req_comp > 4) )
3588 { 3588 {
3589 // just use whatever the file was 3589 // just use whatever the file was
3590 req_comp = tga_bits_per_pixel / 8; 3590 req_comp = tga_bits_per_pixel / 8;
3591 *comp = req_comp; 3591 *comp = req_comp;
3592 } else 3592 } else
3593 { 3593 {
3594 // force a new number of components 3594 // force a new number of components
3595 *comp = tga_bits_per_pixel/8; 3595 *comp = tga_bits_per_pixel/8;
3596 } 3596 }
3597 tga_data = (unsigned char*)MALLOC( tga_width * tga_height * req_comp ); 3597 tga_data = MALLOC( tga_width * tga_height * req_comp );
3598 3598
3599 // skip to the data's starting position (offset usually = 0) 3599 // skip to the data's starting position (offset usually = 0)
3600 skip(s, tga_offset ); 3600 skip(s, tga_offset );
3601 // do I need to load a palette? 3601 // do I need to load a palette?
3602 if ( tga_indexed ) 3602 if ( tga_indexed )
3603 { 3603 {
3604 // any data to skip? (offset usually = 0) 3604 // any data to skip? (offset usually = 0)
3605 skip(s, tga_palette_start ); 3605 skip(s, tga_palette_start );
3606 // load the palette 3606 // load the palette
3607 tga_palette = (unsigned char*)MALLOC( tga_palette_len * tga_palette_bits / 8 ); 3607 tga_palette = MALLOC( tga_palette_len * tga_palette_bits / 8 );
3608 if (!getn(s, tga_palette, tga_palette_len * tga_palette_bits / 8 )) 3608 if (!getn(s, tga_palette, tga_palette_len * tga_palette_bits / 8 ))
3609 return NULL; 3609 return NULL;
3610 } 3610 }
3611 // load the data 3611 // load the data
3612 trans_data[0] = trans_data[1] = trans_data[2] = trans_data[3] = 0; 3612 trans_data[0] = trans_data[1] = trans_data[2] = trans_data[3] = 0;
3613 for (i=0; i < tga_width * tga_height; ++i) 3613 for (i=0; i < tga_width * tga_height; ++i)
3614 { 3614 {
3615 // if I'm in RLE mode, do I need to get a RLE chunk? 3615 // if I'm in RLE mode, do I need to get a RLE chunk?
3616 if ( tga_is_RLE ) 3616 if ( tga_is_RLE )
3617 { 3617 {
3618 if ( RLE_count == 0 ) 3618 if ( RLE_count == 0 )
3619 { 3619 {
3620 // yep, get the next byte as a RLE command 3620 // yep, get the next byte as a RLE command
@@ -3858,27 +3858,27 @@ static stbi_uc *psd_load(stbi *s, int *x @@ -3858,27 +3858,27 @@ static stbi_uc *psd_load(stbi *s, int *x
3858 3858
3859 // Skip the reserved data. 3859 // Skip the reserved data.
3860 skip(s, get32(s) ); 3860 skip(s, get32(s) );
3861 3861
3862 // Find out if the data is compressed. 3862 // Find out if the data is compressed.
3863 // Known values: 3863 // Known values:
3864 // 0: no compression 3864 // 0: no compression
3865 // 1: RLE compressed 3865 // 1: RLE compressed
3866 compression = get16(s); 3866 compression = get16(s);
3867 if (compression > 1) 3867 if (compression > 1)
3868 return epuc("bad compression", "PSD has an unknown compression format"); 3868 return epuc("bad compression", "PSD has an unknown compression format");
3869 3869
3870 // Create the destination image. 3870 // Create the destination image.
3871 out = (stbi_uc *) MALLOC(4 * w*h); 3871 out = MALLOC(4 * w*h);
3872 if (!out) return epuc("outofmem", "Out of memory"); 3872 if (!out) return epuc("outofmem", "Out of memory");
3873 pixelCount = w*h; 3873 pixelCount = w*h;
3874 3874
3875 // Initialize the data to zero. 3875 // Initialize the data to zero.
3876 //memset( out, 0, pixelCount * 4 ); 3876 //memset( out, 0, pixelCount * 4 );
3877  3877
3878 // Finally, the image data. 3878 // Finally, the image data.
3879 if (compression) { 3879 if (compression) {
3880 // RLE as used by .PSD and .TIFF 3880 // RLE as used by .PSD and .TIFF
3881 // Loop until you get the number of unpacked bytes you are expecting: 3881 // Loop until you get the number of unpacked bytes you are expecting:
3882 // Read the next source byte into n. 3882 // Read the next source byte into n.
3883 // If n is between 0 and 127 inclusive, copy the next n+1 bytes literally. 3883 // If n is between 0 and 127 inclusive, copy the next n+1 bytes literally.
3884 // Else if n is between -127 and -1 inclusive, copy the next byte -n+1 times. 3884 // Else if n is between -127 and -1 inclusive, copy the next byte -n+1 times.
@@ -4168,27 +4168,27 @@ static stbi_uc *pic_load(stbi *s,int *px @@ -4168,27 +4168,27 @@ static stbi_uc *pic_load(stbi *s,int *px
4168 for (i=0; i<92; ++i) 4168 for (i=0; i<92; ++i)
4169 get8(s); 4169 get8(s);
4170 4170
4171 x = get16(s); 4171 x = get16(s);
4172 y = get16(s); 4172 y = get16(s);
4173 if (at_eof(s)) return epuc("bad file","file too short (pic header)"); 4173 if (at_eof(s)) return epuc("bad file","file too short (pic header)");
4174 if ((1 << 28) / x < y) return epuc("too large", "Image too large to decode"); 4174 if ((1 << 28) / x < y) return epuc("too large", "Image too large to decode");
4175 4175
4176 get32(s); //skip `ratio' 4176 get32(s); //skip `ratio'
4177 get16(s); //skip `fields' 4177 get16(s); //skip `fields'
4178 get16(s); //skip `pad' 4178 get16(s); //skip `pad'
4179 4179
4180 // intermediate buffer is RGBA 4180 // intermediate buffer is RGBA
4181 result = (stbi_uc *) MALLOC(x*y*4); 4181 result = MALLOC(x*y*4);
4182 memset(result, 0xff, x*y*4); 4182 memset(result, 0xff, x*y*4);
4183 4183
4184 if (!pic_load2(s,x,y,comp, result)) { 4184 if (!pic_load2(s,x,y,comp, result)) {
4185 FREE(result); 4185 FREE(result);
4186 result=0; 4186 result=0;
4187 } 4187 }
4188 *px = x; 4188 *px = x;
4189 *py = y; 4189 *py = y;
4190 if (req_comp == 0) req_comp = *comp; 4190 if (req_comp == 0) req_comp = *comp;
4191 result=convert_format(result,4,req_comp,x,y); 4191 result=convert_format(result,4,req_comp,x,y);
4192 4192
4193 return result; 4193 return result;
4194} 4194}
@@ -4464,34 +4464,34 @@ static void stbi_fill_gif_background(stb @@ -4464,34 +4464,34 @@ static void stbi_fill_gif_background(stb
4464 p[2] = c[0]; 4464 p[2] = c[0];
4465 p[3] = c[3]; 4465 p[3] = c[3];
4466 } 4466 }
4467} 4467}
4468 4468
4469// this function is designed to support animated gifs, although stb_image doesn't support it 4469// this function is designed to support animated gifs, although stb_image doesn't support it
4470static uint8 *stbi_gif_load_next(stbi *s, stbi_gif *g, int *comp, int req_comp) 4470static uint8 *stbi_gif_load_next(stbi *s, stbi_gif *g, int *comp, int req_comp)
4471{ 4471{
4472 int i; 4472 int i;
4473 uint8 *old_out = 0; 4473 uint8 *old_out = 0;
4474 4474
4475 if (g->out == 0) { 4475 if (g->out == 0) {
4476 if (!stbi_gif_header(s, g, comp,0)) return 0; // failure_reason set by stbi_gif_header 4476 if (!stbi_gif_header(s, g, comp,0)) return 0; // failure_reason set by stbi_gif_header
4477 g->out = (uint8 *) MALLOC(4 * g->w * g->h); 4477 g->out = MALLOC(4 * g->w * g->h);
4478 if (g->out == 0) return epuc("outofmem", "Out of memory"); 4478 if (g->out == 0) return epuc("outofmem", "Out of memory");
4479 stbi_fill_gif_background(g); 4479 stbi_fill_gif_background(g);
4480 } else { 4480 } else {
4481 // animated-gif-only path 4481 // animated-gif-only path
4482 if (((g->eflags & 0x1C) >> 2) == 3) { 4482 if (((g->eflags & 0x1C) >> 2) == 3) {
4483 old_out = g->out; 4483 old_out = g->out;
4484 g->out = (uint8 *) MALLOC(4 * g->w * g->h); 4484 g->out = MALLOC(4 * g->w * g->h);
4485 if (g->out == 0) return epuc("outofmem", "Out of memory"); 4485 if (g->out == 0) return epuc("outofmem", "Out of memory");
4486 memcpy(g->out, old_out, g->w*g->h*4); 4486 memcpy(g->out, old_out, g->w*g->h*4);
4487 } 4487 }
4488 } 4488 }
4489  4489
4490 for (;;) { 4490 for (;;) {
4491 switch (get8(s)) { 4491 switch (get8(s)) {
4492 case 0x2C: /* Image Descriptor */ 4492 case 0x2C: /* Image Descriptor */
4493 { 4493 {
4494 int32 x, y, w, h; 4494 int32 x, y, w, h;
4495 uint8 *o; 4495 uint8 *o;
4496 4496
4497 x = get16le(s); 4497 x = get16le(s);
@@ -4593,27 +4593,27 @@ stbi_uc *stbi_gif_load_from_file (FILE @@ -4593,27 +4593,27 @@ stbi_uc *stbi_gif_load_from_file (FILE
4593 } 4593 }
4594 4594
4595 return u; 4595 return u;
4596} 4596}
4597#endif 4597#endif
4598 4598
4599stbi_uc *stbi_gif_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp) 4599stbi_uc *stbi_gif_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)
4600{ 4600{
4601 uint8 *u = 0; 4601 uint8 *u = 0;
4602 stbi s; 4602 stbi s;
4603 stbi_gif *pg; 4603 stbi_gif *pg;
4604 4604
4605 #ifdef STBI_SMALL_STACK 4605 #ifdef STBI_SMALL_STACK
4606 pg = (stbi_gif *) MALLOC(sizeof(*pg)); 4606 pg = MALLOC(sizeof(*pg));
4607 if (pg == NULL) 4607 if (pg == NULL)
4608 return NULL; 4608 return NULL;
4609 #else 4609 #else
4610 stbi_gif g; 4610 stbi_gif g;
4611 pg = &g; 4611 pg = &g;
4612 #endif 4612 #endif
4613 4613
4614 memset(pg, 0, sizeof(*pg)); 4614 memset(pg, 0, sizeof(*pg));
4615 start_mem(&s, buffer, len); 4615 start_mem(&s, buffer, len);
4616 u = stbi_gif_load_next(&s, pg, comp, req_comp); 4616 u = stbi_gif_load_next(&s, pg, comp, req_comp);
4617 if (u == (void *) 1) u = 0; // end of animated gif marker 4617 if (u == (void *) 1) u = 0; // end of animated gif marker
4618 if (u) { 4618 if (u) {
4619 *x = pg->w; 4619 *x = pg->w;
@@ -4778,27 +4778,27 @@ static float *hdr_load(stbi *s, int *x,  @@ -4778,27 +4778,27 @@ static float *hdr_load(stbi *s, int *x,
4778 height = strtol(token, &token, 10); 4778 height = strtol(token, &token, 10);
4779 while (*token == ' ') ++token; 4779 while (*token == ' ') ++token;
4780 if (strncmp(token, "+X ", 3)) return epf("unsupported data layout", "Unsupported HDR format"); 4780 if (strncmp(token, "+X ", 3)) return epf("unsupported data layout", "Unsupported HDR format");
4781 token += 3; 4781 token += 3;
4782 width = strtol(token, NULL, 10); 4782 width = strtol(token, NULL, 10);
4783 4783
4784 *x = width; 4784 *x = width;
4785 *y = height; 4785 *y = height;
4786 4786
4787 *comp = 3; 4787 *comp = 3;
4788 if (req_comp == 0) req_comp = 3; 4788 if (req_comp == 0) req_comp = 3;
4789 4789
4790 // Read data 4790 // Read data
4791 hdr_data = (float *) MALLOC(height * width * req_comp * sizeof(float)); 4791 hdr_data = MALLOC(height * width * req_comp * sizeof(float));
4792 4792
4793 // Load image data 4793 // Load image data
4794 // image data is stored as some number of sca 4794 // image data is stored as some number of sca
4795 if ( width < 8 || width >= 32768) { 4795 if ( width < 8 || width >= 32768) {
4796 // Read flat data 4796 // Read flat data
4797 for (j=0; j < height; ++j) { 4797 for (j=0; j < height; ++j) {
4798 for (i=0; i < width; ++i) { 4798 for (i=0; i < width; ++i) {
4799 stbi_uc rgbe[4]; 4799 stbi_uc rgbe[4];
4800 main_decode_loop: 4800 main_decode_loop:
4801 getn(s, rgbe, 4); 4801 getn(s, rgbe, 4);
4802 hdr_convert(hdr_data + j * width * req_comp + i * req_comp, rgbe, req_comp); 4802 hdr_convert(hdr_data + j * width * req_comp + i * req_comp, rgbe, req_comp);
4803 } 4803 }
4804 } 4804 }
@@ -4817,27 +4817,27 @@ static float *hdr_load(stbi *s, int *x,  @@ -4817,27 +4817,27 @@ static float *hdr_load(stbi *s, int *x,
4817 rgbe[0] = (uint8) c1; 4817 rgbe[0] = (uint8) c1;
4818 rgbe[1] = (uint8) c2; 4818 rgbe[1] = (uint8) c2;
4819 rgbe[2] = (uint8) len; 4819 rgbe[2] = (uint8) len;
4820 rgbe[3] = (uint8) get8u(s); 4820 rgbe[3] = (uint8) get8u(s);
4821 hdr_convert(hdr_data, rgbe, req_comp); 4821 hdr_convert(hdr_data, rgbe, req_comp);
4822 i = 1; 4822 i = 1;
4823 j = 0; 4823 j = 0;
4824 FREE(scanline); 4824 FREE(scanline);
4825 goto main_decode_loop; // yes, this makes no sense 4825 goto main_decode_loop; // yes, this makes no sense
4826 } 4826 }
4827 len <<= 8; 4827 len <<= 8;
4828 len |= get8(s); 4828 len |= get8(s);
4829 if (len != width) { FREE(hdr_data); FREE(scanline); return epf("invalid decoded scanline length", "corrupt HDR"); } 4829 if (len != width) { FREE(hdr_data); FREE(scanline); return epf("invalid decoded scanline length", "corrupt HDR"); }
4830 if (scanline == NULL) scanline = (stbi_uc *) MALLOC(width * 4); 4830 if (scanline == NULL) scanline = MALLOC(width * 4);
4831  4831
4832 for (k = 0; k < 4; ++k) { 4832 for (k = 0; k < 4; ++k) {
4833 i = 0; 4833 i = 0;
4834 while (i < width) { 4834 while (i < width) {
4835 count = get8u(s); 4835 count = get8u(s);
4836 if (count > 128) { 4836 if (count > 128) {
4837 // Run 4837 // Run
4838 value = get8u(s); 4838 value = get8u(s);
4839 count -= 128; 4839 count -= 128;
4840 for (z = 0; z < count; ++z) 4840 for (z = 0; z < count; ++z)
4841 scanline[i++ * 4 + k] = value; 4841 scanline[i++ * 4 + k] = value;
4842 } else { 4842 } else {
4843 // Dump 4843 // Dump