From ae3ebe2e5c0afff99308414d40fbedc1568d1a41 Mon Sep 17 00:00:00 2001 From: T_chan Date: Mon, 31 Jan 2022 10:30:26 +0000 Subject: [PATCH] regroup the BMP file loading into 1 file, and correct some types that give... --- CMakeLists.txt | 10 ++--- samples/lerabot01/main.c | 92 +------------------------------------ samples/lights/main.c | 97 +--------------------------------------- samples/loadbmp.c | 90 +++++++++++++++++++++++++++++++++++++ samples/loadbmp.h | 16 +++++++ samples/mipmap/main.c | 92 +------------------------------------ samples/nehe06/main.c | 92 +------------------------------------ samples/nehe20/main.c | 92 +------------------------------------ 8 files changed, 121 insertions(+), 460 deletions(-) create mode 100644 samples/loadbmp.c create mode 100644 samples/loadbmp.h diff --git a/CMakeLists.txt b/CMakeLists.txt index d25f484..7162f9e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -123,9 +123,9 @@ gen_sample(blend_test samples/blend_test/main.c) gen_sample(depth_funcs samples/depth_funcs/main.c) gen_sample(depth_funcs_alpha_testing samples/depth_funcs_alpha_testing/main.c samples/depth_funcs_alpha_testing/gl_png.c) gen_sample(depth_funcs_ortho samples/depth_funcs_ortho/main.c) -gen_sample(lerabot01 samples/lerabot01/main.c) -gen_sample(lights samples/lights/main.c) -gen_sample(mipmap samples/mipmap/main.c) +gen_sample(lerabot01 samples/lerabot01/main.c samples/loadbmp.c) +gen_sample(lights samples/lights/main.c samples/loadbmp.c) +gen_sample(mipmap samples/mipmap/main.c samples/loadbmp.c) gen_sample(multitexture_arrays samples/multitexture_arrays/main.c samples/multitexture_arrays/pvr-texture.c) gen_sample(nehe02 samples/nehe02/main.c) gen_sample(nehe02de samples/nehe02de/main.c) @@ -133,11 +133,11 @@ gen_sample(nehe02va samples/nehe02va/main.c) gen_sample(nehe03 samples/nehe03/main.c) gen_sample(nehe04 samples/nehe04/main.c) gen_sample(nehe05 samples/nehe05/main.c) -gen_sample(nehe06 samples/nehe06/main.c) +gen_sample(nehe06 samples/nehe06/main.c samples/loadbmp.c) gen_sample(nehe06_vq samples/nehe06_vq/main.c) gen_sample(nehe06_4444twid samples/nehe06_4444twid/main.c) gen_sample(nehe08 samples/nehe08/main.c samples/nehe08/pvr-texture.c) -gen_sample(nehe20 samples/nehe20/main.c) +gen_sample(nehe20 samples/nehe20/main.c samples/loadbmp.c) gen_sample(ortho2d samples/ortho2d/main.c) gen_sample(paletted samples/paletted/main.c) gen_sample(paletted_pcx samples/paletted_pcx/main.c) diff --git a/samples/lerabot01/main.c b/samples/lerabot01/main.c index 9495dd6..f3336c1 100644 --- a/samples/lerabot01/main.c +++ b/samples/lerabot01/main.c @@ -16,102 +16,14 @@ extern uint8 romdisk[]; KOS_INIT_ROMDISK(romdisk); #endif +#include "../loadbmp.h" + /* floats for x rotation, y rotation, z rotation */ float xrot, yrot, zrot; /* storage for one texture */ int texture[1]; -/* Image type - contains height, width, and data */ -struct Image { - unsigned long sizeX; - unsigned long sizeY; - char *data; -}; -typedef struct Image Image; - -// quick and dirty bitmap loader...for 24 bit bitmaps with 1 plane only. -// See http://www.dcs.ed.ac.uk/~mxr/gfx/2d/BMP.txt for more info. -int ImageLoad(char *filename, Image *image) { - FILE *file; - unsigned long size; // size of the image in bytes. - unsigned long i; // standard counter. - unsigned short int planes; // number of planes in image (must be 1) - unsigned short int bpp; // number of bits per pixel (must be 24) - char temp; // temporary color storage for bgr-rgb conversion. - - // make sure the file is there. - if ((file = fopen(filename, "rb"))==NULL) - { - printf("File Not Found : %s\n",filename); - return 0; - } - - // seek through the bmp header, up to the width/height: - fseek(file, 18, SEEK_CUR); - - // read the width - if ((i = fread(&image->sizeX, 4, 1, file)) != 1) { - printf("Error reading width from %s.\n", filename); - return 0; - } - printf("Width of %s: %lu\n", filename, image->sizeX); - - // read the height - if ((i = fread(&image->sizeY, 4, 1, file)) != 1) { - printf("Error reading height from %s.\n", filename); - return 0; - } - printf("Height of %s: %lu\n", filename, image->sizeY); - - // calculate the size (assuming 24 bits or 3 bytes per pixel). - size = image->sizeX * image->sizeY * 3; - - // read the planes - if ((fread(&planes, 2, 1, file)) != 1) { - printf("Error reading planes from %s.\n", filename); - return 0; - } - if (planes != 1) { - printf("Planes from %s is not 1: %u\n", filename, planes); - return 0; - } - - // read the bpp - if ((i = fread(&bpp, 2, 1, file)) != 1) { - printf("Error reading bpp from %s.\n", filename); - return 0; - } - if (bpp != 24) { - printf("Bpp from %s is not 24: %u\n", filename, bpp); - return 0; - } - - // seek past the rest of the bitmap header. - fseek(file, 24, SEEK_CUR); - - // read the data. - image->data = (char *) malloc(size); - if (image->data == NULL) { - printf("Error allocating memory for color-corrected image data"); - return 0; - } - - if ((i = fread(image->data, size, 1, file)) != 1) { - printf("Error reading image data from %s.\n", filename); - return 0; - } - - for (i=0;i rgb) - temp = image->data[i]; - image->data[i] = image->data[i+2]; - image->data[i+2] = temp; - } - - // we're done. - return 1; -} - // Load Bitmaps And Convert To Textures void LoadGLTextures() { // Load Texture diff --git a/samples/lights/main.c b/samples/lights/main.c index 72b56dd..9cc8cb8 100644 --- a/samples/lights/main.c +++ b/samples/lights/main.c @@ -12,113 +12,20 @@ #include "GL/glkos.h" #ifdef __DREAMCAST__ - #include extern uint8_t romdisk[]; KOS_INIT_ROMDISK(romdisk); - #define IMAGE_FILENAME "/rd/NeHe.bmp" - #else #define IMAGE_FILENAME "samples/lights/romdisk/NeHe.bmp" #endif +#include "../loadbmp.h" + float xrot, yrot, zrot; - int texture[1]; - -struct Image { - unsigned long sizeX; - unsigned long sizeY; - char *data; -}; -typedef struct Image Image; - - - -int ImageLoad(char *filename, Image *image) { - FILE *file; - unsigned long size; - unsigned long i; - unsigned short int planes; - unsigned short int bpp; - char temp; - - - if ((file = fopen(filename, "rb"))==NULL) - { - printf("File Not Found : %s\n",filename); - return 0; - } - - - fseek(file, 18, SEEK_CUR); - - - if ((i = fread(&image->sizeX, 4, 1, file)) != 1) { - printf("Error reading width from %s.\n", filename); - return 0; - } - printf("Width of %s: %lu\n", filename, image->sizeX); - - - if ((i = fread(&image->sizeY, 4, 1, file)) != 1) { - printf("Error reading height from %s.\n", filename); - return 0; - } - printf("Height of %s: %lu\n", filename, image->sizeY); - - - size = image->sizeX * image->sizeY * 3; - - - if ((fread(&planes, 2, 1, file)) != 1) { - printf("Error reading planes from %s.\n", filename); - return 0; - } - if (planes != 1) { - printf("Planes from %s is not 1: %u\n", filename, planes); - return 0; - } - - - if ((i = fread(&bpp, 2, 1, file)) != 1) { - printf("Error reading bpp from %s.\n", filename); - return 0; - } - if (bpp != 24) { - printf("Bpp from %s is not 24: %u\n", filename, bpp); - return 0; - } - - - fseek(file, 24, SEEK_CUR); - - - image->data = (char *) malloc(size); - if (image->data == NULL) { - printf("Error allocating memory for color-corrected image data"); - return 0; - } - - if ((i = fread(image->data, size, 1, file)) != 1) { - fprintf(stderr, "Error reading image data from %s.\n", filename); - return 0; - } - - for (i=0;idata[i]; - image->data[i] = image->data[i+2]; - image->data[i+2] = temp; - } - - - return 1; -} - - void LoadGLTextures() { Image *image1; diff --git a/samples/loadbmp.c b/samples/loadbmp.c new file mode 100644 index 0000000..cc8d7b7 --- /dev/null +++ b/samples/loadbmp.c @@ -0,0 +1,90 @@ +// quick and dirty bitmap loader...for 24 bit bitmaps with 1 plane only. +// See http://www.dcs.ed.ac.uk/~mxr/gfx/2d/BMP.txt for more info. + +#include +#include +#include + +#include "loadbmp.h" + +int ImageLoad(char *filename, Image *image) { + FILE *file; + size_t size; // size of the image in bytes. + size_t i; // standard counter. + int32_t sizeX, sizeY; // width/height of the image - must be 4 bytes to match the file format + int16_t planes; // number of planes in image (must be 1) + int16_t bpp; // number of bits per pixel (must be 24) + char temp; // temporary color storage for bgr-rgb conversion. + + // make sure the file is there. + if ((file = fopen(filename, "rb"))==NULL) { + printf("File Not Found : %s\n",filename); + return 0; + } + + // seek through the bmp header, up to the width/height: + fseek(file, 18, SEEK_CUR); + + // read the width + if ((i = fread(&sizeX, 4, 1, file)) != 1) { + printf("Error reading width from %s.\n", filename); + return 0; + } + image->sizeX = sizeX; + printf("Width of %s: %d\n", filename, sizeX); + + // read the height + if ((i = fread(&sizeY, 4, 1, file)) != 1) { + printf("Error reading height from %s.\n", filename); + return 0; + } + image->sizeY = sizeY; + printf("Height of %s: %d\n", filename, sizeY); + + // calculate the size (assuming 24 bits or 3 bytes per pixel). + size = image->sizeX * image->sizeY * 3; + + // read the planes + if ((fread(&planes, 2, 1, file)) != 1) { + printf("Error reading planes from %s.\n", filename); + return 0; + } + if (planes != 1) { + printf("Planes from %s is not 1: %u\n", filename, planes); + return 0; + } + + // read the bpp + if ((i = fread(&bpp, 2, 1, file)) != 1) { + printf("Error reading bpp from %s.\n", filename); + return 0; + } + if (bpp != 24) { + printf("Bpp from %s is not 24: %u\n", filename, bpp); + return 0; + } + + // seek past the rest of the bitmap header. + fseek(file, 24, SEEK_CUR); + + // read the data. + image->data = (char *) malloc(size); + if (image->data == NULL) { + printf("Error allocating memory for color-corrected image data"); + return 0; + } + + if ((i = fread(image->data, size, 1, file)) != 1) { + printf("Error reading image data from %s.\n", filename); + return 0; + } + + for (i=0;i rgb) + temp = image->data[i]; + image->data[i] = image->data[i+2]; + image->data[i+2] = temp; + } + + // we're done. + return 1; +} diff --git a/samples/loadbmp.h b/samples/loadbmp.h new file mode 100644 index 0000000..2febd53 --- /dev/null +++ b/samples/loadbmp.h @@ -0,0 +1,16 @@ +// quick and dirty bitmap loader...for 24 bit bitmaps with 1 plane only. +// See http://www.dcs.ed.ac.uk/~mxr/gfx/2d/BMP.txt for more info. +#ifndef __LOADBMP_H +#define __LOADBMP_H + +/* Image type - contains height, width, and data */ +struct Image { + unsigned int sizeX; + unsigned int sizeY; + char *data; +}; +typedef struct Image Image; + +int ImageLoad(char *, Image *); + +#endif diff --git a/samples/mipmap/main.c b/samples/mipmap/main.c index 1aa0b6d..86a1a54 100644 --- a/samples/mipmap/main.c +++ b/samples/mipmap/main.c @@ -19,99 +19,11 @@ KOS_INIT_ROMDISK(romdisk); #define IMAGE_FILENAME "samples/mipmap/romdisk/NeHe.bmp" #endif +#include "../loadbmp.h" + /* storage for one texture */ int texture[1]; -/* Image type - contains height, width, and data */ -struct Image { - unsigned long sizeX; - unsigned long sizeY; - char *data; -}; -typedef struct Image Image; - -// quick and dirty bitmap loader...for 24 bit bitmaps with 1 plane only. -// See http://www.dcs.ed.ac.uk/~mxr/gfx/2d/BMP.txt for more info. -int ImageLoad(char *filename, Image *image) { - FILE *file; - unsigned long size; // size of the image in bytes. - unsigned long i; // standard counter. - unsigned short int planes; // number of planes in image (must be 1) - unsigned short int bpp; // number of bits per pixel (must be 24) - char temp; // temporary color storage for bgr-rgb conversion. - - // make sure the file is there. - if ((file = fopen(filename, "rb"))==NULL) - { - printf("File Not Found : %s\n",filename); - return 0; - } - - // seek through the bmp header, up to the width/height: - fseek(file, 18, SEEK_CUR); - - // read the width - if ((i = fread(&image->sizeX, 4, 1, file)) != 1) { - printf("Error reading width from %s.\n", filename); - return 0; - } - printf("Width of %s: %lu\n", filename, image->sizeX); - - // read the height - if ((i = fread(&image->sizeY, 4, 1, file)) != 1) { - printf("Error reading height from %s.\n", filename); - return 0; - } - printf("Height of %s: %lu\n", filename, image->sizeY); - - // calculate the size (assuming 24 bits or 3 bytes per pixel). - size = image->sizeX * image->sizeY * 3; - - // read the planes - if ((fread(&planes, 2, 1, file)) != 1) { - printf("Error reading planes from %s.\n", filename); - return 0; - } - if (planes != 1) { - printf("Planes from %s is not 1: %u\n", filename, planes); - return 0; - } - - // read the bpp - if ((i = fread(&bpp, 2, 1, file)) != 1) { - printf("Error reading bpp from %s.\n", filename); - return 0; - } - if (bpp != 24) { - printf("Bpp from %s is not 24: %u\n", filename, bpp); - return 0; - } - - // seek past the rest of the bitmap header. - fseek(file, 24, SEEK_CUR); - - // read the data. - image->data = (char *) malloc(size); - if (image->data == NULL) { - printf("Error allocating memory for color-corrected image data"); - return 0; - } - - if ((i = fread(image->data, size, 1, file)) != 1) { - fprintf(stderr, "Error reading image data from %s.\n", filename); - return 0; - } - - for (i=0;i rgb) - temp = image->data[i]; - image->data[i] = image->data[i+2]; - image->data[i+2] = temp; - } - - // we're done. - return 1; -} - // Load Bitmaps And Convert To Textures void LoadGLTextures() { // Load Texture diff --git a/samples/nehe06/main.c b/samples/nehe06/main.c index 27d3ee4..fe98b82 100644 --- a/samples/nehe06/main.c +++ b/samples/nehe06/main.c @@ -20,102 +20,14 @@ KOS_INIT_ROMDISK(romdisk); #define IMAGE_FILENAME "samples/nehe06/romdisk/NeHe.bmp" #endif +#include "../loadbmp.h" + /* floats for x rotation, y rotation, z rotation */ float xrot, yrot, zrot; /* storage for one texture */ GLuint texture[1]; -/* Image type - contains height, width, and data */ -struct Image { - unsigned long sizeX; - unsigned long sizeY; - char *data; -}; -typedef struct Image Image; - -// quick and dirty bitmap loader...for 24 bit bitmaps with 1 plane only. -// See http://www.dcs.ed.ac.uk/~mxr/gfx/2d/BMP.txt for more info. -int ImageLoad(char *filename, Image *image) { - FILE *file; - unsigned long size; // size of the image in bytes. - unsigned long i; // standard counter. - unsigned short int planes; // number of planes in image (must be 1) - unsigned short int bpp; // number of bits per pixel (must be 24) - char temp; // temporary color storage for bgr-rgb conversion. - - // make sure the file is there. - if ((file = fopen(filename, "rb"))==NULL) - { - printf("File Not Found : %s\n",filename); - return 0; - } - - // seek through the bmp header, up to the width/height: - fseek(file, 18, SEEK_CUR); - - // read the width - if ((i = fread(&image->sizeX, 4, 1, file)) != 1) { - printf("Error reading width from %s.\n", filename); - return 0; - } - printf("Width of %s: %lu\n", filename, image->sizeX); - - // read the height - if ((i = fread(&image->sizeY, 4, 1, file)) != 1) { - printf("Error reading height from %s.\n", filename); - return 0; - } - printf("Height of %s: %lu\n", filename, image->sizeY); - - // calculate the size (assuming 24 bits or 3 bytes per pixel). - size = image->sizeX * image->sizeY * 3; - - // read the planes - if ((fread(&planes, 2, 1, file)) != 1) { - printf("Error reading planes from %s.\n", filename); - return 0; - } - if (planes != 1) { - printf("Planes from %s is not 1: %u\n", filename, planes); - return 0; - } - - // read the bpp - if ((i = fread(&bpp, 2, 1, file)) != 1) { - printf("Error reading bpp from %s.\n", filename); - return 0; - } - if (bpp != 24) { - printf("Bpp from %s is not 24: %u\n", filename, bpp); - return 0; - } - - // seek past the rest of the bitmap header. - fseek(file, 24, SEEK_CUR); - - // read the data. - image->data = (char *) malloc(size); - if (image->data == NULL) { - printf("Error allocating memory for color-corrected image data"); - return 0; - } - - if ((i = fread(image->data, size, 1, file)) != 1) { - fprintf(stderr, "Error reading image data from %s.\n", filename); - return 0; - } - - for (i=0;i rgb) - temp = image->data[i]; - image->data[i] = image->data[i+2]; - image->data[i+2] = temp; - } - - // we're done. - return 1; -} - // Load Bitmaps And Convert To Textures void LoadGLTextures() { // Load Texture diff --git a/samples/nehe20/main.c b/samples/nehe20/main.c index 30a032b..93b01a5 100644 --- a/samples/nehe20/main.c +++ b/samples/nehe20/main.c @@ -21,6 +21,8 @@ extern uint8_t romdisk[]; KOS_INIT_ROMDISK(romdisk); #endif +#include "../loadbmp.h" + /* * This Code Was Created By Jeff Molofee 2000 * And Modified By Giuseppe D'Agata (waveform@tiscalinet.it) @@ -67,96 +69,6 @@ GLuint loop; // Generic Loop Variable GLfloat roll; // Rolling Texture -/* Image type - contains height, width, and data */ -struct Image { - unsigned long sizeX; - unsigned long sizeY; - char *data; -}; - -typedef struct Image Image; - - -int ImageLoad(char *filename, Image *image) { - FILE *file; - unsigned long size; // size of the image in bytes. - unsigned long i; // standard counter. - unsigned short int planes; // number of planes in image (must be 1) - unsigned short int bpp; // number of bits per pixel (must be 24) - char temp; // temporary color storage for bgr-rgb conversion. - - // make sure the file is there. - if ((file = fopen(filename, "rb"))==NULL) - { - printf("File Not Found : %s\n",filename); - return 0; - } - - // seek through the bmp header, up to the width/height: - fseek(file, 18, SEEK_CUR); - - // read the width - if ((i = fread(&image->sizeX, 4, 1, file)) != 1) { - printf("Error reading width from %s.\n", filename); - return 0; - } - printf("Width of %s: %lu\n", filename, image->sizeX); - - // read the height - if ((i = fread(&image->sizeY, 4, 1, file)) != 1) { - printf("Error reading height from %s.\n", filename); - return 0; - } - printf("Height of %s: %lu\n", filename, image->sizeY); - - // calculate the size (assuming 24 bits or 3 bytes per pixel). - size = image->sizeX * image->sizeY * 3; - - // read the planes - if ((fread(&planes, 2, 1, file)) != 1) { - printf("Error reading planes from %s.\n", filename); - return 0; - } - if (planes != 1) { - printf("Planes from %s is not 1: %u\n", filename, planes); - return 0; - } - - // read the bpp - if ((i = fread(&bpp, 2, 1, file)) != 1) { - printf("Error reading bpp from %s.\n", filename); - return 0; - } - if (bpp != 24) { - printf("Bpp from %s is not 24: %u\n", filename, bpp); - return 0; - } - - // seek past the rest of the bitmap header. - fseek(file, 24, SEEK_CUR); - - // read the data. - image->data = (char *) malloc(size); - if (image->data == NULL) { - printf("Error allocating memory for color-corrected image data"); - return 0; - } - - if ((i = fread(image->data, size, 1, file)) != 1) { - fprintf(stderr, "Error reading image data from %s.\n", filename); - return 0; - } - - for (i=0;i rgb) - temp = image->data[i]; - image->data[i] = image->data[i+2]; - image->data[i+2] = temp; - } - - // we're done. - return 1; -} - int LoadGLTextures() // Load Bitmaps And Convert To Textures { int Status=FALSE; // Status Indicator