From e3080c9580cc47c0329a62618a7b178d9da42ccb Mon Sep 17 00:00:00 2001 From: Kent Mein Date: Fri, 9 Jan 2004 22:04:08 +0000 Subject: Added support for outputting bmp's The padding is slightly messed up, so it produces somewhat trunkcated images however it works. I'll try and fix it later but I have to go home now. Its atleast usable at this stage. I moved bmp_decode.c to bmp.c and cleaned it up a little bit. Kent --- projectfiles/blender/imbuf/BL_imbuf.dsp | 2 +- source/blender/imbuf/SConscript | 2 +- source/blender/imbuf/intern/IMB_bmp.h | 1 + source/blender/imbuf/intern/bmp.c | 249 ++++++++++++++++++++++++++++++ source/blender/imbuf/intern/bmp_decode.c | 193 ----------------------- source/blender/imbuf/intern/writeimage.c | 8 + source/blender/imbuf/readme.txt | 17 +- source/blender/makesdna/DNA_scene_types.h | 3 +- source/blender/src/buttons_scene.c | 3 + source/blender/src/toets.c | 5 + source/blender/src/writeimage.c | 3 + 11 files changed, 288 insertions(+), 198 deletions(-) create mode 100644 source/blender/imbuf/intern/bmp.c delete mode 100644 source/blender/imbuf/intern/bmp_decode.c diff --git a/projectfiles/blender/imbuf/BL_imbuf.dsp b/projectfiles/blender/imbuf/BL_imbuf.dsp index 34ec90e4a8a..510d1fdc924 100644 --- a/projectfiles/blender/imbuf/BL_imbuf.dsp +++ b/projectfiles/blender/imbuf/BL_imbuf.dsp @@ -163,7 +163,7 @@ SOURCE=..\..\..\source\blender\imbuf\intern\bitplanes.c # End Source File # Begin Source File -SOURCE=..\..\..\source\blender\imbuf\intern\bmp_decode.c +SOURCE=..\..\..\source\blender\imbuf\intern\bmp.c # End Source File # Begin Source File diff --git a/source/blender/imbuf/SConscript b/source/blender/imbuf/SConscript index 7461a89b709..52a1795d666 100644 --- a/source/blender/imbuf/SConscript +++ b/source/blender/imbuf/SConscript @@ -15,7 +15,7 @@ source_files = ['intern/allocimbuf.c', 'intern/anim5.c', 'intern/antialias.c', 'intern/bitplanes.c', - 'intern/bmp_decode.c', + 'intern/bmp.c', 'intern/cmap.c', 'intern/cspace.c', 'intern/data.c', diff --git a/source/blender/imbuf/intern/IMB_bmp.h b/source/blender/imbuf/intern/IMB_bmp.h index fe279849bd0..b0fde577b00 100644 --- a/source/blender/imbuf/intern/IMB_bmp.h +++ b/source/blender/imbuf/intern/IMB_bmp.h @@ -44,6 +44,7 @@ struct ImBuf; int imb_is_a_bmp(void *buf); struct ImBuf *imb_bmp_decode(unsigned char *mem, int size, int flags); +int bmp_savebmp(struct ImBuf *ibuf, int file, int flags); #endif diff --git a/source/blender/imbuf/intern/bmp.c b/source/blender/imbuf/intern/bmp.c new file mode 100644 index 00000000000..cb8a1e22711 --- /dev/null +++ b/source/blender/imbuf/intern/bmp.c @@ -0,0 +1,249 @@ +/** + * $Id$ + * + * ***** 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 ***** + */ + +#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" +#include "IMB_bmp.h" + +#ifdef HAVE_CONFIG_H +#include +#endif + +/* some code copied from article on microsoft.com, copied + here for enhanced BMP support in the future + http://www.microsoft.com/msj/defaultframe.asp?page=/msj/0197/mfcp1/mfcp1.htm&nav=/msj/0197/newnav.htm +*/ + +typedef struct BMPINFOHEADER{ + unsigned int biSize; + unsigned int biWidth; + unsigned int biHeight; + unsigned short biPlanes; + unsigned short biBitCount; + unsigned int biCompression; + unsigned int biSizeImage; + unsigned int biXPelsPerMeter; + unsigned int biYPelsPerMeter; + unsigned int biClrUsed; + unsigned int biClrImportant; +} BMPINFOHEADER; + +typedef struct BMPHEADER { + unsigned short biType; + unsigned int biSize; + unsigned short biRes1; + unsigned short biRes2; + unsigned int biOffBits; +} BMPHEADER; + +#define BMP_FILEHEADER_SIZE 14 + +static int checkbmp(unsigned char *mem) +{ + int ret_val = 0; + BMPINFOHEADER bmi; + unsigned int u; + + if (mem) { + if ((mem[0] == 'B') && (mem[1] == 'M')) { + /* skip fileheader */ + mem += BMP_FILEHEADER_SIZE; + } else { + } + + /* for systems where an int needs to be 4 bytes aligned */ + memcpy(&bmi, mem, sizeof(bmi)); + + u = LITTLE_LONG(bmi.biSize); + /* we only support uncompressed 24 or 32 bits images for now */ + if (u >= sizeof(BMPINFOHEADER)) { + if ((bmi.biCompression == 0) && (bmi.biClrUsed == 0)) { + u = LITTLE_SHORT(bmi.biBitCount); + if (u >= 16) { + ret_val = 1; + } + } + } + } + + return(ret_val); +} + +int imb_is_a_bmp(void *buf) { + + return checkbmp(buf); +} + +struct ImBuf *imb_bmp_decode(unsigned char *mem, int size, int flags) +{ + struct ImBuf *ibuf = 0; + BMPINFOHEADER bmi; + int x, y, depth, skip, i; + unsigned char *bmp, *rect; + unsigned short col; + + if (checkbmp(mem) == 0) return(0); + + if ((mem[0] == 'B') && (mem[1] == 'M')) { + /* skip fileheader */ + mem += BMP_FILEHEADER_SIZE; + } + + /* for systems where an int needs to be 4 bytes aligned */ + memcpy(&bmi, mem, sizeof(bmi)); + + skip = LITTLE_LONG(bmi.biSize); + x = LITTLE_LONG(bmi.biWidth); + y = LITTLE_LONG(bmi.biHeight); + depth = LITTLE_SHORT(bmi.biBitCount); + + /* printf("skip: %d, x: %d y: %d, depth: %d (%x)\n", skip, x, y, + depth, bmi.biBitCount); */ + printf("skip: %d, x: %d y: %d, depth: %d (%x)\n", skip, x, y, + depth, bmi.biBitCount); + if (flags & IB_test) { + ibuf = IMB_allocImBuf(x, y, depth, 0, 0); + } else { + ibuf = IMB_allocImBuf(x, y, depth, IB_rect, 0); + bmp = mem + skip; + rect = (unsigned char *) ibuf->rect; + + if (depth == 16) { + for (i = x * y; i > 0; i--) { + col = bmp[0] + (bmp[1] << 8); + rect[0] = ((col >> 10) & 0x1f) << 3; + rect[1] = ((col >> 5) & 0x1f) << 3; + rect[2] = ((col >> 0) & 0x1f) << 3; + + rect[3] = 255; + rect += 4; bmp += 2; + } + + } else if (depth == 24) { + for (i = x * y; i > 0; i--) { + rect[0] = bmp[2]; + rect[1] = bmp[1]; + rect[2] = bmp[0]; + + rect[3] = 255; + rect += 4; bmp += 3; + } + } else if (depth == 32) { + for (i = x * y; i > 0; i--) { + rect[0] = bmp[0]; + rect[1] = bmp[1]; + rect[2] = bmp[2]; + rect[3] = bmp[3]; + rect += 4; bmp += 4; + } + } + } + + if (ibuf) { + ibuf->ftype = BMP; + } + + return(ibuf); +} + +/* Couple of helper functions for writing our data */ +int putIntLSB(unsigned int ui,FILE *ofile) { + putc((ui>>0)&0xFF,ofile); + putc((ui>>8)&0xFF,ofile); + putc((ui>>16)&0xFF,ofile); + return putc((ui>>24)&0xFF,ofile); +} + +int putShortLSB(unsigned short us,FILE *ofile) { + putc((us>>0)&0xFF,ofile); + return putc((us>>8)&0xFF,ofile); +} + +/* Found write info at http://users.ece.gatech.edu/~slabaugh/personal/c/bitmapUnix.c */ +short imb_savebmp(struct ImBuf *ibuf, int outfile, int flags) { + + BMPINFOHEADER infoheader; + int bytesize, extrabytes, x, y, t, ptr; + uchar *data; + FILE *ofile; + + extrabytes = (4 - ibuf->x % 4) % 4; +printf("extrabytes = %d\n",extrabytes); + bytesize = (ibuf->x + extrabytes) * ibuf->y; + + data = (uchar *) ibuf->rect; + ofile = fdopen(outfile,"ab"); + + putShortLSB(19778,ofile); /* "BM" */ + putIntLSB(0,ofile); /* This can be 0 for BI_RGB bitmaps */ + putShortLSB(0,ofile); /* Res1 */ + putShortLSB(0,ofile); /* Res2 */ + putIntLSB(BMP_FILEHEADER_SIZE + sizeof(infoheader),ofile); + + putIntLSB(sizeof(infoheader),ofile); + putIntLSB(ibuf->x,ofile); + putIntLSB(ibuf->y,ofile); + putShortLSB(1,ofile); + putShortLSB(24,ofile); + putIntLSB(0,ofile); + putIntLSB(bytesize,ofile); + putIntLSB(0,ofile); + putIntLSB(0,ofile); + putIntLSB(0,ofile); + putIntLSB(0,ofile); + + /* Need to write out padded image data in bgr format */ + for (y=0;yy;y++) { + for (x=0;xx;x++) { + + ptr=(x + y * ibuf->x) * 4; + if (putc(data[ptr+2],ofile) == EOF) return 0; + if (putc(data[ptr+1],ofile) == EOF) return 0; + if (putc(data[ptr],ofile) == EOF) return 0; + + } + /* add padding here */ + for (t=0;t -#endif - -// some code copied from article on microsoft.com, copied -// here for enhanced BMP support in the future -// http://www.microsoft.com/msj/defaultframe.asp?page=/msj/0197/mfcp1/mfcp1.htm&nav=/msj/0197/newnav.htm - -/* -LPBYTE CDib::GetBits() - { - return (LPBYTE)m_pbmih + // start of bitmap + - m_pbmih->biSize + // size of header + - GetNumPaletteColors() // (num colors * - *sizeof(RGBQUAD); // size each entry) - } - -UINT CDib::GetNumPaletteColors() - { - UINT nColors=m_pbmih->biClrUsed; - if (nColors==0 && m_pbmih->biBitCount<=8) - nColors = 1<biBitCount; - return nColors; - } - -*/ - -typedef struct BMPINFOHEADER{ - unsigned int biSize; - int biWidth; - int biHeight; - unsigned short biPlanes; - unsigned short biBitCount; - unsigned int biCompression; - unsigned int biSizeImage; - int biXPelsPerMeter; - int biYPelsPerMeter; - unsigned int biClrUsed; - unsigned int biClrImportant; -} BMPINFOHEADER; - -#define BMP_FILEHEADER_SIZE 14 - -static int checkbmp(unsigned char *mem) -{ - int ret_val = 0; - BMPINFOHEADER bmi; - unsigned int u; - - if (mem) { - if ((mem[0] == 'B') && (mem[1] == 'M')) { - // skip fileheader - mem += BMP_FILEHEADER_SIZE; - } - - // for systems where an int needs to be 4 bytes aligned - memcpy(&bmi, mem, sizeof(bmi)); - - u = LITTLE_LONG(bmi.biSize); - // we only support uncompressed 24 or 32 bits images for now - if (u >= sizeof(BMPINFOHEADER)) { - if ((bmi.biCompression == 0) && (bmi.biClrUsed == 0)) { - u = LITTLE_SHORT(bmi.biBitCount); - if (u >= 16) { - ret_val = 1; - } - } - } - } - - return(ret_val); -} - -int imb_is_a_bmp(void *buf) { - - return checkbmp(buf); -} - -struct ImBuf *imb_bmp_decode(unsigned char *mem, int size, int flags) -{ - struct ImBuf *ibuf = 0; - BMPINFOHEADER bmi; - int x, y, depth, skip, i; - unsigned char *bmp, *rect; - unsigned short col; - - if (checkbmp(mem) == 0) return(0); - - if ((mem[0] == 'B') && (mem[1] == 'M')) { - // skip fileheader - mem += BMP_FILEHEADER_SIZE; - } - - // for systems where an int needs to be 4 bytes aligned - memcpy(&bmi, mem, sizeof(bmi)); - - skip = LITTLE_LONG(bmi.biSize); - x = LITTLE_LONG(bmi.biWidth); - y = LITTLE_LONG(bmi.biHeight); - depth = LITTLE_SHORT(bmi.biBitCount); - - // printf("skip: %d, x: %d y: %d, depth: %d (%x)\n", skip, x, y, depth, bmi.biBitCount); - if (flags & IB_test) { - ibuf = IMB_allocImBuf(x, y, depth, 0, 0); - } else { - ibuf = IMB_allocImBuf(x, y, depth, IB_rect, 0); - bmp = mem + skip; - rect = (unsigned char *) ibuf->rect; - - if (depth == 16) { - for (i = x * y; i > 0; i--) { - col = bmp[0] + (bmp[1] << 8); - rect[0] = ((col >> 10) & 0x1f) << 3; - rect[1] = ((col >> 5) & 0x1f) << 3; - rect[2] = ((col >> 0) & 0x1f) << 3; - - rect[3] = 255; - rect += 4; bmp += 2; - } - - } else if (depth == 24) { - for (i = x * y; i > 0; i--) { - rect[0] = bmp[2]; - rect[1] = bmp[1]; - rect[2] = bmp[0]; - - rect[3] = 255; - rect += 4; bmp += 3; - } - } else if (depth == 32) { - for (i = x * y; i > 0; i--) { - rect[0] = bmp[0]; - rect[1] = bmp[1]; - rect[2] = bmp[2]; - rect[3] = bmp[3]; - rect += 4; bmp += 4; - } - } - } - - if (ibuf) { - ibuf->ftype = BMP; - } - - return(ibuf); -} - diff --git a/source/blender/imbuf/intern/writeimage.c b/source/blender/imbuf/intern/writeimage.c index 64df437679a..d6133a19193 100644 --- a/source/blender/imbuf/intern/writeimage.c +++ b/source/blender/imbuf/intern/writeimage.c @@ -91,6 +91,14 @@ short IMB_saveiff(struct ImBuf *ibuf,char *naam,int flags) } } + if (IS_bmp(ibuf)) { + ok = imb_savebmp(ibuf,file,flags); + if (ok) { + close (file); + return (ok); + } + } + if (IS_tga(ibuf)) { ok = imb_savetarga(ibuf,file,flags); if (ok) { diff --git a/source/blender/imbuf/readme.txt b/source/blender/imbuf/readme.txt index d50cf82af89..4edc625013f 100644 --- a/source/blender/imbuf/readme.txt +++ b/source/blender/imbuf/readme.txt @@ -21,7 +21,20 @@ Add your hooks to read and write the image format these go in Step 3: Add in IS_openexr to blender/source/blender/imbuf/IMB_imbuf_types.h +Add in R_openexr to source/blender/makesdna/DNA_scene_types.h -Step 4: -Add any external library info to the build process. +Step 4: +Add your hooks to the gui. +source/blender/src/buttons_scene.c +source/blender/src/toets.c +source/blender/src/writeimage.c + +Step 5: +Alter the build process: +For autoconf you need to edit blender/source/blender/imbuf/Makefile.am +and add in your additional files. +For msvp you need to edit blender/projectfiles/blender/imbuf/BL_imbuf.dsp +and add in your additional files. +If you have any external library info you will also need to add that +to the various build processes. diff --git a/source/blender/makesdna/DNA_scene_types.h b/source/blender/makesdna/DNA_scene_types.h index c8219bdc1d1..5d03de842e5 100644 --- a/source/blender/makesdna/DNA_scene_types.h +++ b/source/blender/makesdna/DNA_scene_types.h @@ -323,7 +323,8 @@ typedef struct Scene { #define R_AVIJPEG 16 #define R_PNG 17 #define R_AVICODEC 18 -#define R_QUICKTIME 19 +#define R_QUICKTIME 19 +#define R_BMP 20 /* **************** RENDER ********************* */ diff --git a/source/blender/src/buttons_scene.c b/source/blender/src/buttons_scene.c index c3697b1f40c..dbb1734dc2b 100644 --- a/source/blender/src/buttons_scene.c +++ b/source/blender/src/buttons_scene.c @@ -877,6 +877,7 @@ static char *imagetype_pup(void) #endif strcat(formatstring, "|%s %%x%d"); // add space for PNG + strcat(formatstring, "|%s %%x%d"); // add space for BMP #ifdef _WIN32 strcat(formatstring, "|%s %%x%d"); // add space for AVI Codec @@ -900,6 +901,7 @@ static char *imagetype_pup(void) "Targa", R_TARGA, "Targa Raw", R_RAWTGA, "PNG", R_PNG, + "BMP", R_BMP, "Jpeg", R_JPEG90, "HamX", R_HAMX, "Iris", R_IRIS, @@ -917,6 +919,7 @@ static char *imagetype_pup(void) "Targa", R_TARGA, "Targa Raw", R_RAWTGA, "PNG", R_PNG, + "BMP", R_BMP, "Jpeg", R_JPEG90, "HamX", R_HAMX, "Iris", R_IRIS, diff --git a/source/blender/src/toets.c b/source/blender/src/toets.c index 87f2654e8c6..fa220f3da2b 100644 --- a/source/blender/src/toets.c +++ b/source/blender/src/toets.c @@ -191,6 +191,9 @@ void schrijfplaatje(char *name) else if(R.r.imtype==R_PNG) { ibuf->ftype= PNG; } + else if(R.r.imtype==R_BMP) { + ibuf->ftype= BMP; + } else if((R.r.imtype==R_TARGA) || (R.r.imtype==R_PNG)) { ibuf->ftype= TGA; } @@ -460,6 +463,8 @@ int save_image_filesel_str(char *str) switch(G.scene->r.imtype) { case R_PNG: strcpy(str, "SAVE PNG"); return 1; + case R_BMP: + strcpy(str, "SAVE BMP"); return 1; case R_TARGA: strcpy(str, "SAVE TARGA"); return 1; case R_RAWTGA: diff --git a/source/blender/src/writeimage.c b/source/blender/src/writeimage.c index 2b49a0f6fda..23ead94c22d 100644 --- a/source/blender/src/writeimage.c +++ b/source/blender/src/writeimage.c @@ -53,6 +53,9 @@ int BIF_write_ibuf(ImBuf *ibuf, char *name) else if ((R.r.imtype==R_PNG)) { ibuf->ftype= PNG; } + else if ((R.r.imtype==R_BMP)) { + ibuf->ftype= BMP; + } else if ((R.r.imtype==R_TARGA) || (R.r.imtype==R_PNG)) { // fall back to Targa if PNG writing is not supported ibuf->ftype= TGA; -- cgit v1.2.3