Skip to content

Commit

Permalink
pp2d: remove unnecessary stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
BernardoGiordano committed Dec 19, 2017
1 parent aae1e9d commit 4245dfa
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 89 deletions.
88 changes: 1 addition & 87 deletions source/pp2d/loadbmp.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,7 @@
#define LOADBMP_RGB 3
#define LOADBMP_RGBA 4

#ifdef LOADBMP_IMPLEMENTATION
# define LOADBMP_API
#else
# define LOADBMP_API extern
#endif
# define LOADBMP_API

// LoadBMP uses raw buffers and support RGB and RGBA formats.
// The order is RGBRGBRGB... or RGBARGBARGBA..., from top left
Expand All @@ -54,9 +50,6 @@
LOADBMP_API unsigned int loadbmp_decode_file(
const char *filename, unsigned char **imageData, unsigned int *width, unsigned int *height, unsigned int components);

LOADBMP_API unsigned int loadbmp_encode_file(
const char *filename, const unsigned char *imageData, unsigned int width, unsigned int height, unsigned int components);

// Disable Microsoft Visual C++ compiler security warnings for fopen, strcpy, etc being unsafe
#if defined(_MSC_VER) && (_MSC_VER >= 1310)
# pragma warning(disable: 4996)
Expand Down Expand Up @@ -132,7 +125,6 @@ LOADBMP_API unsigned int loadbmp_decode_file(
if (fread(data + i, 3, 1, f) == 0)
{
free(data);

fclose(f);
return LOADBMP_INVALID_FILE_FORMAT;
}
Expand All @@ -148,7 +140,6 @@ LOADBMP_API unsigned int loadbmp_decode_file(
if (fread(bmp_pad, 1, padding, f) != padding)
{
free(data);

fclose(f);
return LOADBMP_INVALID_FILE_FORMAT;
}
Expand All @@ -160,82 +151,5 @@ LOADBMP_API unsigned int loadbmp_decode_file(
(*imageData) = data;

fclose(f);

return LOADBMP_NO_ERROR;
}

LOADBMP_API unsigned int loadbmp_encode_file(
const char *filename, const unsigned char *imageData, unsigned int width, unsigned int height, unsigned int components)
{
FILE *f = fopen(filename, "wb");

if (!f)
return LOADBMP_FILE_OPERATION;

unsigned char bmp_file_header[14] = { 'B', 'M', 0, 0, 0, 0, 0, 0, 0, 0, 54, 0, 0, 0 };
unsigned char bmp_info_header[40] = { 40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 24, 0 };
const unsigned char bmp_pad[3] = { 0, 0, 0 };

const unsigned int size = 54 + width * height * 3; // 3 as the BMP format uses 3 channels (red, green, blue and NO alpha)

unsigned int x, y, i, padding;

unsigned char pixel[3];

bmp_file_header[2] = (unsigned char)(size);
bmp_file_header[3] = (unsigned char)(size >> 8);
bmp_file_header[4] = (unsigned char)(size >> 16);
bmp_file_header[5] = (unsigned char)(size >> 24);

bmp_info_header[4] = (unsigned char)(width);
bmp_info_header[5] = (unsigned char)(width >> 8);
bmp_info_header[6] = (unsigned char)(width >> 16);
bmp_info_header[7] = (unsigned char)(width >> 24);

bmp_info_header[8] = (unsigned char)(height);
bmp_info_header[9] = (unsigned char)(height >> 8);
bmp_info_header[10] = (unsigned char)(height >> 16);
bmp_info_header[11] = (unsigned char)(height >> 24);

if (fwrite(bmp_file_header, 14, 1, f) == 0)
{
fclose(f);
return LOADBMP_FILE_OPERATION;
}

if (fwrite(bmp_info_header, 40, 1, f) == 0)
{
fclose(f);
return LOADBMP_FILE_OPERATION;
}

for (y = (height - 1); y != -1; y--)
{
for (x = 0; x < width; x++)
{
i = (x + y * width) * components;

memcpy(pixel, imageData + i, sizeof(pixel));

pixel[0] ^= pixel[2] ^= pixel[0] ^= pixel[2]; // RGB -> BGR

if (fwrite(pixel, sizeof(pixel), 1, f) == 0)
{
fclose(f);
return LOADBMP_FILE_OPERATION;
}
}

padding = ((4 - (width * 3) % 4) % 4);

if (fwrite(bmp_pad, 1, padding, f) != padding)
{
fclose(f);
return LOADBMP_FILE_OPERATION;
}
}

fclose(f);

return LOADBMP_NO_ERROR;
}
2 changes: 0 additions & 2 deletions source/pp2d/pp2d.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@
*/

#include "pp2d.h"

#define LOADBMP_IMPLEMENTATION
#include "loadbmp.h"

static DVLB_s* vshader_dvlb;
Expand Down

0 comments on commit 4245dfa

Please sign in to comment.