diff options
-rw-r--r-- | source/blender/imbuf/IMB_imbuf_types.h | 27 | ||||
-rw-r--r-- | source/blender/imbuf/intern/IMB_jpeg.h | 2 | ||||
-rw-r--r-- | source/blender/imbuf/intern/IMB_png.h | 6 | ||||
-rw-r--r-- | source/blender/imbuf/intern/amiga.c | 5 | ||||
-rw-r--r-- | source/blender/imbuf/intern/antialias.c | 6 | ||||
-rw-r--r-- | source/blender/imbuf/intern/bitplanes.c | 6 | ||||
-rw-r--r-- | source/blender/imbuf/intern/ham.c | 2 | ||||
-rw-r--r-- | source/blender/imbuf/intern/iris.c | 16 | ||||
-rw-r--r-- | source/blender/imbuf/intern/jpeg.c | 12 | ||||
-rw-r--r-- | source/blender/imbuf/intern/matrix.h | 56 | ||||
-rw-r--r-- | source/blender/imbuf/intern/png.c (renamed from source/blender/imbuf/intern/png_decode.c) | 220 | ||||
-rw-r--r-- | source/blender/imbuf/intern/png_encode.c | 240 | ||||
-rw-r--r-- | source/blender/imbuf/intern/readimage.c | 2 | ||||
-rw-r--r-- | source/blender/imbuf/intern/writeimage.c | 11 | ||||
-rw-r--r-- | source/blender/imbuf/readme.txt | 27 |
15 files changed, 292 insertions, 346 deletions
diff --git a/source/blender/imbuf/IMB_imbuf_types.h b/source/blender/imbuf/IMB_imbuf_types.h index bff0665aa71..8abb00d722d 100644 --- a/source/blender/imbuf/IMB_imbuf_types.h +++ b/source/blender/imbuf/IMB_imbuf_types.h @@ -136,10 +136,10 @@ typedef enum { * * The bit flag is stored in the ImBuf.ftype variable. */ -#define AMI (1 << 31) -#define PNG (1 << 30) -#define Anim (1 << 29) -#define TGA (1 << 28) +#define AMI (1 << 31) +#define PNG (1 << 30) +#define Anim (1 << 29) +#define TGA (1 << 28) #define JPG (1 << 27) #define BMP (1 << 26) #ifdef WITH_QUICKTIME @@ -152,16 +152,16 @@ typedef enum { #define IMAGEMAGICK (1 << 23) #endif -#define RAWTGA (TGA | 1) +#define RAWTGA (TGA | 1) -#define JPG_STD (JPG | (0 << 8)) -#define JPG_VID (JPG | (1 << 8)) -#define JPG_JST (JPG | (2 << 8)) -#define JPG_MAX (JPG | (3 << 8)) -#define JPG_MSK (0xffffff00) +#define JPG_STD (JPG | (0 << 8)) +#define JPG_VID (JPG | (1 << 8)) +#define JPG_JST (JPG | (2 << 8)) +#define JPG_MAX (JPG | (3 << 8)) +#define JPG_MSK (0xffffff00) -#define AM_ham (0x0800 | AMI) -#define AM_hbrite (0x0080 | AMI) +#define AM_ham (0x0800 | AMI) +#define AM_hbrite (0x0080 | AMI) #define C233 1 #define YUVX 2 @@ -201,6 +201,3 @@ typedef enum { #endif - - - diff --git a/source/blender/imbuf/intern/IMB_jpeg.h b/source/blender/imbuf/intern/IMB_jpeg.h index db23ebccb87..8edf42881ae 100644 --- a/source/blender/imbuf/intern/IMB_jpeg.h +++ b/source/blender/imbuf/intern/IMB_jpeg.h @@ -43,7 +43,7 @@ struct ImBuf; struct jpeg_compress_struct; -int imb_save_jpeg(char * name, struct ImBuf * ibuf, int flags); +int imb_savejpeg(struct ImBuf * ibuf, char * name, int flags); struct ImBuf * imb_ibJpegImageFromFilename (char * filename, int flags); struct ImBuf * imb_ibJpegImageFromMemory (unsigned char * buffer, int size, int flags); diff --git a/source/blender/imbuf/intern/IMB_png.h b/source/blender/imbuf/intern/IMB_png.h index 4623189c636..63638deff00 100644 --- a/source/blender/imbuf/intern/IMB_png.h +++ b/source/blender/imbuf/intern/IMB_png.h @@ -34,7 +34,7 @@ /** * \file IMB_png.h * \ingroup imbuf - * \brief Function declarations for png_decode.c + * \brief Function declarations for png.c */ #ifndef IMB_PNG_H @@ -43,7 +43,9 @@ struct ImBuf; int imb_is_a_png(void *buf); -struct ImBuf *imb_png_decode(unsigned char *mem, int size, int flags); +struct ImBuf *imb_loadpng(unsigned char *mem, int size, int flags); + +short imb_savepng(struct ImBuf *ibuf, int file, int flags); #endif diff --git a/source/blender/imbuf/intern/amiga.c b/source/blender/imbuf/intern/amiga.c index 613b6ab04f1..6feb1e29166 100644 --- a/source/blender/imbuf/intern/amiga.c +++ b/source/blender/imbuf/intern/amiga.c @@ -455,10 +455,7 @@ struct ImBuf *imb_loadamiga(int *iffmem,int flags) ibuf->mincol = 0; ibuf->maxcol = cmaplen; imb_addcmapImBuf(ibuf); - /* this functions needs a 3rd arg: the number of - * columns.... why did this work before? */ -/* imb_makecolarray(ibuf, cmap); */ - imb_makecolarray(ibuf, cmap, 0); + imb_makecolarray(ibuf, (uchar *)cmap, 0); } if (flags & IB_test){ diff --git a/source/blender/imbuf/intern/antialias.c b/source/blender/imbuf/intern/antialias.c index 48384c51915..6c261eb7750 100644 --- a/source/blender/imbuf/intern/antialias.c +++ b/source/blender/imbuf/intern/antialias.c @@ -98,7 +98,8 @@ static ListBase * scanimage(struct ImBuf * ibuf, int dir) step = 1; nextline = ibuf->x; pixels = ibuf->x; lines = ibuf->y; break; - case 'v': +/* case 'v': changed so assured values for step etc.. */ + default: step = ibuf->x; nextline = 1; pixels = ibuf->y; lines = ibuf->x; } @@ -233,7 +234,8 @@ static void filterimage(struct ImBuf * ibuf, struct ImBuf * cbuf, ListBase * lis step = 1; nextline = ibuf->x; pixels = ibuf->x; lines = ibuf->y; break; - case 'v': +/* case 'v': changed so have values */ + default: step = ibuf->x; nextline = 1; pixels = ibuf->y; lines = ibuf->x; } diff --git a/source/blender/imbuf/intern/bitplanes.c b/source/blender/imbuf/intern/bitplanes.c index 578ab579510..56f3dc01eb1 100644 --- a/source/blender/imbuf/intern/bitplanes.c +++ b/source/blender/imbuf/intern/bitplanes.c @@ -75,12 +75,12 @@ static void bptolscanl(unsigned int *buf, int size, unsigned int **list, int nob for (;nobp>0;) { int todo,i; - register int bp1, bp2, bp3, data; + register int bp1, bp2, bp3, bp4, data; register unsigned int *point; - int bp4, loffset; + int loffset; /*register unsigned int bp1, bp2, bp3, bp4;*/ - todo = 0; + bp1 = bp2 = bp3 = bp4 = todo = 0; point = buf; loffset = offset; diff --git a/source/blender/imbuf/intern/ham.c b/source/blender/imbuf/intern/ham.c index afc899d86ff..3673b954c0e 100644 --- a/source/blender/imbuf/intern/ham.c +++ b/source/blender/imbuf/intern/ham.c @@ -274,7 +274,7 @@ short imb_converttoham(struct ImBuf *ibuf) imb_convhamx(ibuf, coltab, deltab); } else { for(;y > 0; y--){ - convhamscanl(x, y, rect, coltab, deltab, ibuf->cbits); + convhamscanl(x, y, (uchar *)rect, coltab, deltab, ibuf->cbits); rect += x; } } diff --git a/source/blender/imbuf/intern/iris.c b/source/blender/imbuf/intern/iris.c index 1619616cc67..469bbc414e5 100644 --- a/source/blender/imbuf/intern/iris.c +++ b/source/blender/imbuf/intern/iris.c @@ -321,7 +321,7 @@ struct ImBuf *imb_loadiris(unsigned char *mem, int flags) rledat = file_data + file_offset; file_offset += lengthtab[y+z*ysize]; - expandrow(lptr, rledat, 3-z); + expandrow((uchar *)lptr, rledat, 3-z); lptr += xsize; } } @@ -338,8 +338,8 @@ struct ImBuf *imb_loadiris(unsigned char *mem, int flags) rledat = file_data + file_offset; file_offset += lengthtab[y+z*ysize]; - if(z<4) expandrow(lptr, rledat, 3-z); - else if(z<8) expandrow(zptr, rledat, 7-z); + if(z<4) expandrow((uchar *)lptr, rledat, 3-z); + else if(z<8) expandrow((uchar *)zptr, rledat, 7-z); } lptr += xsize; zptr += xsize; @@ -367,7 +367,7 @@ struct ImBuf *imb_loadiris(unsigned char *mem, int flags) for(y = 0; y < ysize; y++) { - interleaverow(lptr, rledat, 3-z, xsize); + interleaverow((uchar *)lptr, rledat, 3-z, xsize); rledat += xsize; lptr += xsize; @@ -571,15 +571,15 @@ static int output_iris(unsigned int *lptr, int xsize, int ysize, int zsize, int for (z = 0; z < zsize; z++) { if (zsize == 1) { - lumrow(lptr,lumbuf,xsize); - len = compressrow(lumbuf,rlebuf,CHANOFFSET(z),xsize); + lumrow((uchar *)lptr,(uchar *)lumbuf,xsize); + len = compressrow((uchar *)lumbuf,rlebuf,CHANOFFSET(z),xsize); } else { if(z<4) { - len = compressrow(lptr, rlebuf,CHANOFFSET(z),xsize); + len = compressrow((uchar *)lptr, rlebuf,CHANOFFSET(z),xsize); } else if(z<8 && zptr) { - len = compressrow(zptr, rlebuf,CHANOFFSET(z-4),xsize); + len = compressrow((uchar *)zptr, rlebuf,CHANOFFSET(z-4),xsize); } } if(len>rlebuflen) { diff --git a/source/blender/imbuf/intern/jpeg.c b/source/blender/imbuf/intern/jpeg.c index 10cf082dd0a..378c43bc2ba 100644 --- a/source/blender/imbuf/intern/jpeg.c +++ b/source/blender/imbuf/intern/jpeg.c @@ -613,15 +613,13 @@ static int save_maxjpeg(char * name, struct ImBuf * ibuf) } -int imb_save_jpeg(char * name, struct ImBuf * ibuf, int flags) +int imb_savejpeg(struct ImBuf * ibuf, char * name, int flags) { - int ret; ibuf->flags = flags; - if (IS_stdjpg(ibuf)) ret = save_stdjpeg(name, ibuf); - else if (IS_jstjpg(ibuf)) ret = save_jstjpeg(name, ibuf); - else if (IS_maxjpg(ibuf)) ret = save_maxjpeg(name, ibuf); - else ret = save_vidjpeg(name, ibuf); - return(ret); + if (IS_stdjpg(ibuf)) return save_stdjpeg(name, ibuf); + if (IS_jstjpg(ibuf)) return save_jstjpeg(name, ibuf); + if (IS_maxjpg(ibuf)) return save_maxjpeg(name, ibuf); + return save_vidjpeg(name, ibuf); } diff --git a/source/blender/imbuf/intern/matrix.h b/source/blender/imbuf/intern/matrix.h index 0036ff7b570..f2db8057b32 100644 --- a/source/blender/imbuf/intern/matrix.h +++ b/source/blender/imbuf/intern/matrix.h @@ -36,52 +36,52 @@ float rgbyuv[4][4]={ /* afgeleid uit videoframer = Y Cr Cb in kopieen van Francois*/ /* is identriek aan matrix van jpeg */ - .50000, .11400, -.08131, 0.0, /* b */ - -.33126, .58700, -.41869, 0.0, /* g */ - -.16874, .29900, .50000, 0.0, /* r */ - 128.0, 0.0, 128.0, 1.0}; + { .50000, .11400, -.08131, 0.0,}, /* b */ + {-.33126, .58700, -.41869, 0.0,}, /* g */ + {-.16874, .29900, .50000, 0.0,}, /* r */ + { 128.0, 0.0, 128.0, 1.0}}; /* b-y (u) y r-y (v) */ float rgbbeta[4][4]={ /* afgeleid uit videoframer = Y Cr Cb in kopieen van Francois*/ /* is identriek aan matrix van jpeg */ - .50000, .11400, -.08131, 0.0, /* b-y -> b */ - -.33126, .58700, -.41869, 0.0, /* y -> g */ - -.16874, .29900, .50000, 0.0, /* r-y -> r */ - 128.0, 0.0, 128.0, 1.0}; + {.50000, .11400, -.08131, 0.0,}, /* b-y -> b */ + {-.33126, .58700, -.41869, 0.0,}, /* y -> g */ + {-.16874, .29900, .50000, 0.0,}, /* r-y -> r */ + { 128.0, 0.0, 128.0, 1.0}}; /* b-y y r-y */ float yuvrgb[4][4]={ - 1.77200, -0.34414, 0.0, 0.0, - 1.0, 1.0, 1.0, 0.0, - 0.0, -0.71414, 1.40200, 0.0, - -226.816, 135.460, -179.456, 1.0}; + {1.77200, -0.34414, 0.0, 0.0, }, + {1.0, 1.0, 1.0, 0.0, }, + {0.0, -0.71414, 1.40200, 0.0, }, + {-226.816, 135.460, -179.456, 1.0}}; float rgb_to_bw[4][4]={ - .299, .299, .299, 0.0, - .587, .587, .587, 0.0, - .114, .114, .114, 0.0, - 0.5, 0.5, 0.5, 1.0}; + {.299, .299, .299, 0.0,}, + {.587, .587, .587, 0.0,}, + {.114, .114, .114, 0.0,}, + { 0.5, 0.5, 0.5, 1.0}}; float dyuvrgb_oud[4][4]={ - 1.0 , 1.0 , 1.0, 0.0, - 1.733, -0.337, 0.0, 0.0, - 0.0, -.698, 1.371, 0.0, - -221.8, 132.47, -175.5, 1.0}; + {1.0 , 1.0 , 1.0, 0.0,}, + {1.733, -0.337, 0.0, 0.0,}, + {0.0, -.698, 1.371, 0.0,}, + {-221.8, 132.47, -175.5, 1.0}}; float dyuvrgb[4][4]={ - 1.164 , 1.164 , 1.164, 0.0, - 2.018, -0.391, 0.0, 0.0, - 0.0, -0.813, 1.596, 0.0, - -276.7, 135.6, -222.7, 1.0}; + {1.164 , 1.164 , 1.164, 0.0,}, + {2.018, -0.391, 0.0, 0.0,}, + {0.0, -0.813, 1.596, 0.0,}, + {-276.7, 135.6, -222.7, 1.0}}; float rgbdyuv[4][4]={ - 0.439, 0.098, -0.071, 0.0, - -0.291, 0.504, -0.368, 0.0, - -0.148, 0.257, 0.439, 0.0, - 128.0, 16.0, 128.0, 1.0}; + {0.439, 0.098, -0.071, 0.0,}, + {-0.291, 0.504, -0.368, 0.0,}, + {-0.148, 0.257, 0.439, 0.0,}, + {128.0, 16.0, 128.0, 1.0}}; diff --git a/source/blender/imbuf/intern/png_decode.c b/source/blender/imbuf/intern/png.c index 3f9f3936270..d8ec1a293dc 100644 --- a/source/blender/imbuf/intern/png_decode.c +++ b/source/blender/imbuf/intern/png.c @@ -30,13 +30,14 @@ * $Id$ */ + #include "png.h" #ifdef HAVE_CONFIG_H #include <config.h> #endif -#ifdef _WIN32 +#ifdef WIN32 #include "BLI_winstuff.h" #endif #include "BLI_blenlib.h" @@ -51,41 +52,45 @@ #include "IMB_cmap.h" #include "IMB_png.h" +typedef struct PNGReadStruct { + unsigned char *data; + unsigned int size; + unsigned int seek; +}PNGReadStruct; +static void ReadData( png_structp png_ptr, png_bytep data, png_size_t length); +static void WriteData( png_structp png_ptr, png_bytep data, png_size_t length); +static void Flush( png_structp png_ptr); -static int checkpng(unsigned char *mem) +int imb_is_a_png(void *mem) { - int ret_val = 0; + int ret_val = 0; - if (mem) { - ret_val = !png_sig_cmp(mem, 0, 8); - } + if (mem) { + ret_val = !png_sig_cmp(mem, 0, 8); + } - return(ret_val); + return(ret_val); } -int imb_is_a_png(void *buf) { - - return checkpng(buf); +static void Flush(png_structp png_ptr) +{ } -typedef struct PNGReadStruct { - unsigned char *data; - unsigned int size; - unsigned int seek; -}PNGReadStruct; +static void WriteData( png_structp png_ptr, png_bytep data, png_size_t length) +{ + ImBuf *ibuf = (ImBuf *) png_get_io_ptr(png_ptr); + + // if buffer is to small increase it. + while (ibuf->encodedsize + length > ibuf->encodedbuffersize) { + imb_enlargeencodedbufferImBuf(ibuf); + } + + memcpy(ibuf->encodedbuffer + ibuf->encodedsize, data, length); + ibuf->encodedsize += length; +} -static void -ReadData( - png_structp png_ptr, - png_bytep data, - png_size_t length); - -static void -ReadData( - png_structp png_ptr, - png_bytep data, - png_size_t length) +static void ReadData( png_structp png_ptr, png_bytep data, png_size_t length) { PNGReadStruct *rs= (PNGReadStruct *) png_get_io_ptr(png_ptr); @@ -101,8 +106,161 @@ ReadData( longjmp(png_jmpbuf(png_ptr), 1); } +short imb_savepng(struct ImBuf *ibuf, int file, int flags) +{ + png_structp png_ptr; + png_infop info_ptr; + unsigned char *pixels = 0; + unsigned char *from, *to; + png_bytepp row_pointers = 0; + int i, bytesperpixel, color_type = PNG_COLOR_TYPE_GRAY; + FILE *fp = 0; + + bytesperpixel = (ibuf->depth + 7) >> 3; + if ((bytesperpixel > 4) || (bytesperpixel == 2)) { + printf("imb_savepng: unsupported bytes per pixel: %d\n", bytesperpixel); + return (0); + } + + png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, + NULL, NULL, NULL); + if (png_ptr == NULL) { + printf("Cannot png_create_write_struct\n"); + return 0; + } + + info_ptr = png_create_info_struct(png_ptr); + if (info_ptr == NULL) { + png_destroy_write_struct(&png_ptr, (png_infopp)NULL); + printf("Cannot png_create_info_struct\n"); + return 0; + } + + if (setjmp(png_jmpbuf(png_ptr))) { + png_destroy_write_struct(&png_ptr, &info_ptr); + if (pixels) MEM_freeN(pixels); + if (row_pointers) MEM_freeN(row_pointers); + // printf("Aborting\n"); + if (fp) { + fflush(fp); + fclose(fp); + } + return 0; + } + + // copy image data + + pixels = MEM_mallocN(ibuf->x * ibuf->y * bytesperpixel * sizeof(unsigned char), "pixels"); + if (pixels == NULL) { + printf("Cannot allocate pixels array\n"); + return 0; + } + + from = (unsigned char *) ibuf->rect; + to = pixels; + + switch (bytesperpixel) { + case 4: + color_type = PNG_COLOR_TYPE_RGBA; + for (i = ibuf->x * ibuf->y; i > 0; i--) { + to[0] = from[0]; + to[1] = from[1]; + to[2] = from[2]; + to[3] = from[3]; + to += 4; from += 4; + } + break; + case 3: + color_type = PNG_COLOR_TYPE_RGB; + for (i = ibuf->x * ibuf->y; i > 0; i--) { + to[0] = from[0]; + to[1] = from[1]; + to[2] = from[2]; + to += 3; from += 4; + } + break; + case 1: + color_type = PNG_COLOR_TYPE_GRAY; + for (i = ibuf->x * ibuf->y; i > 0; i--) { + to[0] = from[0]; + to++; from += 4; + } + break; + } + + if (flags & IB_mem) { + // create image in memory + imb_addencodedbufferImBuf(ibuf); + ibuf->encodedsize = 0; + + png_set_write_fn(png_ptr, + (png_voidp) ibuf, + WriteData, + Flush); + } else { + fp = fdopen(file, "wb"); + png_init_io(png_ptr, fp); + } + + /* + png_set_filter(png_ptr, 0, + PNG_FILTER_NONE | PNG_FILTER_VALUE_NONE | + PNG_FILTER_SUB | PNG_FILTER_VALUE_SUB | + PNG_FILTER_UP | PNG_FILTER_VALUE_UP | + PNG_FILTER_AVG | PNG_FILTER_VALUE_AVG | + PNG_FILTER_PAETH | PNG_FILTER_VALUE_PAETH| + PNG_ALL_FILTERS); + + png_set_compression_level(png_ptr, Z_BEST_COMPRESSION); + */ + + // png image settings + png_set_IHDR(png_ptr, + info_ptr, + ibuf->x, + ibuf->y, + 8, + color_type, + PNG_INTERLACE_NONE, + PNG_COMPRESSION_TYPE_DEFAULT, + PNG_FILTER_TYPE_DEFAULT); + + // write the file header information + png_write_info(png_ptr, info_ptr); + + // allocate memory for an array of row-pointers + row_pointers = (png_bytepp) MEM_mallocN(ibuf->y * sizeof(png_bytep), "row_pointers"); + if (row_pointers == NULL) { + printf("Cannot allocate row-pointers array\n"); + return 0; + } + + // set the individual row-pointers to point at the correct offsets + for (i = 0; i < ibuf->y; i++) { + row_pointers[ibuf->y-1-i] = (png_bytep) + ((unsigned char *)pixels + (i * ibuf->x) * bytesperpixel * sizeof(unsigned char)); + } + + // write out the entire image data in one call + png_write_image(png_ptr, row_pointers); + + // write the additional chunks to the PNG file (not really needed) + png_write_end(png_ptr, info_ptr); + + // clean up + MEM_freeN(pixels); + MEM_freeN(row_pointers); + png_destroy_write_struct(&png_ptr, &info_ptr); + + if (fp) { + fflush(fp); + fclose(fp); + } + + return(1); +} -struct ImBuf *imb_png_decode(unsigned char *mem, int size, int flags) +struct ImBuf *imb_loadpng(unsigned char *mem, int size, int flags) { struct ImBuf *ibuf = 0; png_structp png_ptr; @@ -116,7 +274,7 @@ struct ImBuf *imb_png_decode(unsigned char *mem, int size, int flags) unsigned char *from, *to; int i, bytesperpixel; - if (checkpng(mem) == 0) return(0); + if (imb_is_a_png(mem) == 0) return(0); png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); @@ -127,7 +285,8 @@ struct ImBuf *imb_png_decode(unsigned char *mem, int size, int flags) info_ptr = png_create_info_struct(png_ptr); if (info_ptr == NULL) { - png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL); + png_destroy_read_struct(&png_ptr, (png_infopp)NULL, + (png_infopp)NULL); printf("Cannot png_create_info_struct\n"); return 0; } @@ -149,7 +308,8 @@ struct ImBuf *imb_png_decode(unsigned char *mem, int size, int flags) // png_set_sig_bytes(png_ptr, 8); png_read_info(png_ptr, info_ptr); - png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, NULL, NULL, NULL); + png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, + &color_type, NULL, NULL, NULL); if (bit_depth == 16) { png_set_strip_16(png_ptr); diff --git a/source/blender/imbuf/intern/png_encode.c b/source/blender/imbuf/intern/png_encode.c deleted file mode 100644 index fbc9afd7b2c..00000000000 --- a/source/blender/imbuf/intern/png_encode.c +++ /dev/null @@ -1,240 +0,0 @@ -/** - * - * ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. The Blender - * Foundation also sells licenses for use in proprietary software under - * the Blender License. See http://www.blender.org/BL/ for information - * about this. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * - * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. - * All rights reserved. - * - * The Original Code is: all of this file. - * - * Contributor(s): none yet. - * - * ***** END GPL/BL DUAL LICENSE BLOCK ***** - * $Id$ - */ - - -#include "png.h" - -#ifdef HAVE_CONFIG_H -#include <config.h> -#endif - -#ifdef WIN32 -#include "BLI_winstuff.h" -#endif -#include "BLI_blenlib.h" - -#include "imbuf.h" -#include "imbuf_patch.h" - -#include "IMB_imbuf_types.h" -#include "IMB_imbuf.h" - -#include "IMB_allocimbuf.h" -#include "IMB_cmap.h" - -static void -WriteData( - png_structp png_ptr, - png_bytep data, - png_size_t length); - -static void -Flush( - png_structp png_ptr); - -static void -WriteData( - png_structp png_ptr, - png_bytep data, - png_size_t length) -{ - ImBuf *ibuf = (ImBuf *) png_get_io_ptr(png_ptr); - - // if buffer is to small increase it. - while (ibuf->encodedsize + length > ibuf->encodedbuffersize) { - imb_enlargeencodedbufferImBuf(ibuf); - } - - memcpy(ibuf->encodedbuffer + ibuf->encodedsize, data, length); - ibuf->encodedsize += length; -} - -static void -Flush( - png_structp png_ptr) -{ -} - -short IMB_png_encode(struct ImBuf *ibuf, int file, int flags) -{ - png_structp png_ptr; - png_infop info_ptr; - unsigned char *pixels = 0; - unsigned char *from, *to; - png_bytepp row_pointers = 0; - int i, bytesperpixel, color_type = PNG_COLOR_TYPE_GRAY; - FILE *fp = 0; - - bytesperpixel = (ibuf->depth + 7) >> 3; - if ((bytesperpixel > 4) || (bytesperpixel == 2)) { - printf("imb_png_encode: unsupported bytes per pixel: %d\n", bytesperpixel); - return (0); - } - - png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, - NULL, NULL, NULL); - if (png_ptr == NULL) { - printf("Cannot png_create_write_struct\n"); - return 0; - } - - info_ptr = png_create_info_struct(png_ptr); - if (info_ptr == NULL) { - png_destroy_write_struct(&png_ptr, (png_infopp)NULL); - printf("Cannot png_create_info_struct\n"); - return 0; - } - - if (setjmp(png_jmpbuf(png_ptr))) { - png_destroy_write_struct(&png_ptr, &info_ptr); - if (pixels) MEM_freeN(pixels); - if (row_pointers) MEM_freeN(row_pointers); - // printf("Aborting\n"); - if (fp) { - fflush(fp); - fclose(fp); - } - return 0; - } - - // copy image data - - pixels = MEM_mallocN(ibuf->x * ibuf->y * bytesperpixel * sizeof(unsigned char), "pixels"); - if (pixels == NULL) { - printf("Cannot allocate pixels array\n"); - return 0; - } - - from = (unsigned char *) ibuf->rect; - to = pixels; - - switch (bytesperpixel) { - case 4: - color_type = PNG_COLOR_TYPE_RGBA; - for (i = ibuf->x * ibuf->y; i > 0; i--) { - to[0] = from[0]; - to[1] = from[1]; - to[2] = from[2]; - to[3] = from[3]; - to += 4; from += 4; - } - break; - case 3: - color_type = PNG_COLOR_TYPE_RGB; - for (i = ibuf->x * ibuf->y; i > 0; i--) { - to[0] = from[0]; - to[1] = from[1]; - to[2] = from[2]; - to += 3; from += 4; - } - break; - case 1: - color_type = PNG_COLOR_TYPE_GRAY; - for (i = ibuf->x * ibuf->y; i > 0; i--) { - to[0] = from[0]; - to++; from += 4; - } - break; - } - - if (flags & IB_mem) { - // create image in memory - imb_addencodedbufferImBuf(ibuf); - ibuf->encodedsize = 0; - - png_set_write_fn(png_ptr, - (png_voidp) ibuf, - WriteData, - Flush); - } else { - fp = fdopen(file, "wb"); - png_init_io(png_ptr, fp); - } - - /* - png_set_filter(png_ptr, 0, - PNG_FILTER_NONE | PNG_FILTER_VALUE_NONE | - PNG_FILTER_SUB | PNG_FILTER_VALUE_SUB | - PNG_FILTER_UP | PNG_FILTER_VALUE_UP | - PNG_FILTER_AVG | PNG_FILTER_VALUE_AVG | - PNG_FILTER_PAETH | PNG_FILTER_VALUE_PAETH| - PNG_ALL_FILTERS); - - png_set_compression_level(png_ptr, Z_BEST_COMPRESSION); - */ - - // png image settings - png_set_IHDR(png_ptr, - info_ptr, - ibuf->x, - ibuf->y, - 8, - color_type, - PNG_INTERLACE_NONE, - PNG_COMPRESSION_TYPE_DEFAULT, - PNG_FILTER_TYPE_DEFAULT); - - // write the file header information - png_write_info(png_ptr, info_ptr); - - // allocate memory for an array of row-pointers - row_pointers = (png_bytepp) MEM_mallocN(ibuf->y * sizeof(png_bytep), "row_pointers"); - if (row_pointers == NULL) { - printf("Cannot allocate row-pointers array\n"); - return 0; - } - - // set the individual row-pointers to point at the correct offsets - for (i = 0; i < ibuf->y; i++) { - row_pointers[ibuf->y-1-i] = (png_bytep) - ((unsigned char *)pixels + (i * ibuf->x) * bytesperpixel * sizeof(unsigned char)); - } - - // write out the entire image data in one call - png_write_image(png_ptr, row_pointers); - - // write the additional chunks to the PNG file (not really needed) - png_write_end(png_ptr, info_ptr); - - // clean up - MEM_freeN(pixels); - MEM_freeN(row_pointers); - png_destroy_write_struct(&png_ptr, &info_ptr); - - if (fp) { - fflush(fp); - fclose(fp); - } - - return(1); -} - diff --git a/source/blender/imbuf/intern/readimage.c b/source/blender/imbuf/intern/readimage.c index fb574bf7da8..f15d3505374 100644 --- a/source/blender/imbuf/intern/readimage.c +++ b/source/blender/imbuf/intern/readimage.c @@ -127,7 +127,7 @@ ImBuf *IMB_ibImageFromMemory(int *mem, int size, int flags) { #else if(1) { #endif - ibuf = imb_png_decode((uchar *)mem, size, flags); + ibuf = imb_loadpng((uchar *)mem, size, flags); if (ibuf) return(ibuf); } diff --git a/source/blender/imbuf/intern/writeimage.c b/source/blender/imbuf/intern/writeimage.c index 49d827fcce5..3bd0e8bafc6 100644 --- a/source/blender/imbuf/intern/writeimage.c +++ b/source/blender/imbuf/intern/writeimage.c @@ -48,6 +48,7 @@ #include "IMB_ham.h" #include "IMB_hamx.h" #include "IMB_amiga.h" +#include "IMB_png.h" #include "IMB_iff.h" #include "IMB_bitplanes.h" @@ -66,10 +67,11 @@ short IMB_saveiff(struct ImBuf *ibuf,char *naam,int flags) if (ibuf==0) return (FALSE); ibuf->flags = flags; + /* Put formats that take a filename here */ if (IS_jpg(ibuf)) { - if (imb_save_jpeg(naam, ibuf, flags)) return (0); - else return (TRUE); + return imb_savejpeg(ibuf, naam, flags); } + file = open(naam, O_BINARY | O_RDWR | O_CREAT | O_TRUNC, 0666); if (file < 0) return (FALSE); @@ -79,15 +81,16 @@ short IMB_saveiff(struct ImBuf *ibuf,char *naam,int flags) } } + /* Put formats that take a filehandle here */ if (IS_png(ibuf)) { - ok = IMB_png_encode(ibuf,file,flags); + ok = imb_savepng(ibuf,file,flags); if (ok) { close (file); return (ok); } } - if (IS_tga(ibuf) || IS_png(ibuf)) { + if (IS_tga(ibuf)) { ok = imb_savetarga(ibuf,file,flags); if (ok) { close (file); diff --git a/source/blender/imbuf/readme.txt b/source/blender/imbuf/readme.txt new file mode 100644 index 00000000000..d50cf82af89 --- /dev/null +++ b/source/blender/imbuf/readme.txt @@ -0,0 +1,27 @@ +The following 4 steps to adding a new image format to blender, its +probably easiest to look at the png code for a clean clear example, +animation formats are a bit more complicated but very similar: + +Step 1: +create a new file named after the format for example lets say we were +creating an openexr read/writer use openexr.c +It should contain functions to match the following prototypes: + +struct ImBuf *imb_loadopenexr(unsigned char *mem,int size,int flags); +/* Use one of the following depending on whats easyer for your file format */ +short imb_saveopenexr(struct ImBuf *ibuf, FILE myfile, int flags); +short imb_saveopenexr(struct ImBuf *ibuf, char *myfile, int flags); + +/* Used to test if its the correct format +int IMB_is_openexr(void *buf); + +Step 2: +Add your hooks to read and write the image format these go in + writeimage.c and readimage.c just look at how the others are done + +Step 3: +Add in IS_openexr to blender/source/blender/imbuf/IMB_imbuf_types.h + +Step 4: +Add any external library info to the build process. + |