7#define RGB_COLOR_LENGTH 3
8#define FILE_HEADER_LENGTH 14
9#define INFO_HEADER_LENGTH 40
49void Bmp_getPixelOfRaster(
struct Bmp *
this,
int horizontal,
int vertical,
unsigned char *red,
unsigned char *green,
unsigned char *blue,
int *error)
51 unsigned long offset = this->
width * vertical + horizontal;
53 *red = this->
reds[offset];
54 *green = this->
greens[offset];
55 *blue = this->
blues[offset];
58void Bmp_getPixelOfLine(
struct Bmp *
this,
int index,
unsigned char *red,
unsigned char *green,
unsigned char *blue,
int *error)
60 *red = this->
reds[index];
61 *green = this->
greens[index];
62 *blue = this->
blues[index];
67 FILE *file = fopen(filename,
"rb");
78 unsigned int width = 0;
81 unsigned char *
reds = 0;
83 unsigned char *
blues = 0;
87 unsigned int padding = 0;
89 memset(fileHeader, 0,
sizeof(fileHeader));
90 memset(infoHeader, 0,
sizeof(infoHeader));
92 if (fread(fileHeader,
sizeof(fileHeader), 1, file) == 0)
101 if (fread(infoHeader,
sizeof(infoHeader), 1, file) == 0)
110 if ((fileHeader[0] !=
'B') || (fileHeader[1] !=
'M'))
119 if ((infoHeader[14] != 24) && (infoHeader[14] != 32))
128 width = (infoHeader[4] + (infoHeader[5] << 8) + (infoHeader[6] << 16) + (infoHeader[7] << 24));
129 height = (infoHeader[8] + (infoHeader[9] << 8) + (infoHeader[10] << 16) + (infoHeader[11] << 24));
162 for (y = (
height - 1); y != -1; y--)
164 for (x = 0; x <
width; x++)
170 if (fread(
blues + i, 1, 1, file) == 0)
182 if (fread(
greens + i, 1, 1, file) == 0)
194 if (fread(
reds + i, 1, 1, file) == 0)
207 padding = ((4 - (
width * 3) % 4) % 4);
209 if (fread(pad, 1, padding, file) != padding)
225 struct Bmp *
this = malloc(
sizeof(
struct Bmp));
244 FILE *file = fopen(filename,
"wb");
252 unsigned char fileHeader[
FILE_HEADER_LENGTH] = {
'B',
'M', 0, 0, 0, 0, 0, 0, 0, 0, 54, 0, 0, 0 };
253 unsigned char infoHeader[
INFO_HEADER_LENGTH] = { 40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 24, 0 };
254 const unsigned char pad[
PAD_LENGTH] = { 0, 0, 0 };
261 unsigned int padding = 0;
263 fileHeader[2] = (
unsigned char)(size);
264 fileHeader[3] = (
unsigned char)(size >> 8);
265 fileHeader[4] = (
unsigned char)(size >> 16);
266 fileHeader[5] = (
unsigned char)(size >> 24);
268 infoHeader[4] = (
unsigned char)(this->
width);
269 infoHeader[5] = (
unsigned char)(this->
width >> 8);
270 infoHeader[6] = (
unsigned char)(this->
width >> 16);
271 infoHeader[7] = (
unsigned char)(this->
width >> 24);
273 infoHeader[8] = (
unsigned char)(this->
height);
274 infoHeader[9] = (
unsigned char)(this->
height >> 8);
275 infoHeader[10] = (
unsigned char)(this->
height >> 16);
276 infoHeader[11] = (
unsigned char)(this->
height >> 24);
294 for (y = (this->
height - 1); y != -1; y--)
296 for (x = 0; x < this->
width; x++)
302 if (fwrite(&this->
blues[i], 1, 1, file) == 0)
309 if (fwrite(&this->
greens[i], 1, 1, file) == 0)
316 if (fwrite(&this->
reds[i], 1, 1, file) == 0)
324 padding = ((4 - (this->width * 3) % 4) % 4);
326 if (fwrite(pad, 1, padding, file) != padding)
struct Bmp * Bmp_readFile(const char *filename, int *error)
It loads an image file with the bmp format into memory.
unsigned long Bmp_getLength(struct Bmp *this)
It supplies the number of pixels.
#define FILE_HEADER_LENGTH
void Bmp_getPixelOfLine(struct Bmp *this, int index, unsigned char *red, unsigned char *green, unsigned char *blue, int *error)
It supplies three colors of a pixel when the horizontal and vertical positions of a raster are provid...
#define INFO_HEADER_LENGTH
unsigned int Bmp_getWidth(struct Bmp *this)
unsigned int Bmp_getHeight(struct Bmp *this)
It supplies the height of an image.
struct Bmp * Bmp_destruct(struct Bmp *this)
It destructs a managing object for one image.
void Bmp_writeFile(struct Bmp *this, const char *filename, int *error)
It writes an image in a file in bmp format.
void Bmp_getPixelOfRaster(struct Bmp *this, int horizontal, int vertical, unsigned char *red, unsigned char *green, unsigned char *blue, int *error)
It supplies three colors of a pixel when the horizontal and vertical positions of a raster are provid...
It stores an image in memory.
unsigned long length
the length of each color channel
unsigned char * blues
the blue channel as an array of bytes
unsigned char * reds
the red channel as an array of bytes
unsigned char * greens
the green channel as an array of bytes
unsigned int height
the height of an image
unsigned int width
the width of an image