agssim: stop execution on invalid jump
[rofl0r-agsutils.git] / BitmapFuncs.h
blob5efc1a66e4896fd2a00b88002916f88cca9394d2
1 #ifndef BITMAPFUNCS_H
2 #define BITMAPFUNCS_H
4 #include "Bitmap.h"
5 #include "ImageData.h"
6 #include "endianness.h"
7 #include <stdlib.h>
8 #include <stdio.h>
10 static int pad_bmp(ImageData *d) {
11 unsigned stride = ((((d->width * d->bytesperpixel*8) + 31) & ~31) >> 3);
12 if(stride == d->width * d->bytesperpixel) return -1;
13 unsigned x,y;
14 unsigned outsz = d->height * stride;
15 unsigned char *out = malloc(outsz), *p = d->data, *q = out;
16 if(!out) return 0;
17 for(y = 0; y < d->height; ++y) {
18 for(x = 0; x < d->width*d->bytesperpixel; ++x)
19 *(q++) = *(p++);
20 for(; x < stride; ++x)
21 *(q++) = 0;
23 free(d->data);
24 d->data = out;
25 d->data_size = outsz;
26 return 1;
28 static void write_bmp(char *name, ImageData *d) {
29 FILE *f = fopen(name, "w");
30 if(f) {
31 struct BITMAPINFOHEADER_X hdr = {
32 .bfType = le16(0x4D42),
33 .bfSize = le32(sizeof(hdr) + d->data_size),
34 .bfOffsetBits = le32(sizeof(hdr)),
35 .biSize = le32(sizeof(hdr)-14),
36 .biWidth = le32(d->width),
37 /* negative height means bmp is stored from top to bottom */
38 .biHeight = le32( - d->height),
39 .biPlanes = le32(1),
40 .biBitCount = le32(d->bytesperpixel * 8),
41 .biCompression = 0,
42 .biSizeImage = 0,
43 .biXPelsPerMeter = le32(0xb11),
44 .biYPelsPerMeter = le32(0xb11),
46 fwrite(&hdr, 1, sizeof hdr, f);
47 pad_bmp(d);
48 fwrite(d->data, 1, d->data_size, f);
49 fclose(f);
50 } else {
51 fprintf(stderr, "error opening %s\n", name);
55 #endif