summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorSean Fox <dyntryx@gmail.com>2008-07-01 09:10:20 +0000
committerSean Fox <dyntryx@gmail.com>2008-07-01 09:10:20 +0000
commit46ace4776ee4bdb421810c492641aea987289310 (patch)
treed801105afa6ed0ee5edcc13f1319ea22ae7f7d6f /examples
parent099d5a5fbb93e3995b32245ccfb2d99e7996a045 (diff)
downloadlibnsbmp-46ace4776ee4bdb421810c492641aea987289310.tar.gz
libnsbmp-46ace4776ee4bdb421810c492641aea987289310.tar.bz2
Changed several char/int types to proper types from <stdint.h>; corrected decoding errors for 32-bit and 16-bit bitmaps; more corrections to ico handling; added support for 'limited transparency'; added documentation to assist with future editing; cleaned up functions that read in data
svn path=/branches/dynis/libnsbmp/; revision=4484
Diffstat (limited to 'examples')
-rw-r--r--examples/decode_bmp.c24
-rw-r--r--examples/decode_ico.c57
2 files changed, 48 insertions, 33 deletions
diff --git a/examples/decode_bmp.c b/examples/decode_bmp.c
index d838f79..e09f187 100644
--- a/examples/decode_bmp.c
+++ b/examples/decode_bmp.c
@@ -21,12 +21,14 @@
#include <errno.h>
#include <stdbool.h>
#include <stdlib.h>
+#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <libnsbmp.h>
-#define BITMAP_BYTES_PER_PIXEL 4
+#define BYTES_PER_PIXEL 4
+#define TRANSPARENT_COLOR 0xffffffff
unsigned char *load_file(const char *path, size_t *data_size);
void warning(const char *context, bmp_result code);
@@ -78,21 +80,21 @@ int main(int argc, char *argv[])
/* decode the image */
code = bmp_decode(&bmp);
+ /* code = bmp_decode_trans(&bmp, TRANSPARENT_COLOR); */
if (code != BMP_OK) {
warning("bmp_decode", code);
exit(1);
}
{
- unsigned int row, col;
- unsigned char *image;
- image = (unsigned char *) bmp.bitmap;
+ uint16_t row, col;
+ uint8_t *image;
+ image = (uint8_t *) bmp.bitmap;
for (row = 0; row != bmp.height; row++) {
for (col = 0; col != bmp.width; col++) {
- size_t z = (row * bmp.width + col) * BITMAP_BYTES_PER_PIXEL;
- printf("%u %u %u ",
- (unsigned char) image[z],
- (unsigned char) image[z + 1],
- (unsigned char) image[z + 2]);
+ size_t z = (row * bmp.width + col) * BYTES_PER_PIXEL;
+ printf("%u %u %u ", image[z],
+ image[z + 1],
+ image[z + 2]);
}
printf("\n");
}
@@ -170,7 +172,7 @@ void warning(const char *context, bmp_result code)
void *bitmap_create(int width, int height, unsigned int state)
{
(void) state; /* unused */
- return calloc(width * height, BITMAP_BYTES_PER_PIXEL);
+ return calloc(width * height, BYTES_PER_PIXEL);
}
@@ -200,7 +202,7 @@ unsigned char *bitmap_get_buffer(void *bitmap)
size_t bitmap_get_bpp(void *bitmap)
{
(void) bitmap; /* unused */
- return BITMAP_BYTES_PER_PIXEL;
+ return BYTES_PER_PIXEL;
}
diff --git a/examples/decode_ico.c b/examples/decode_ico.c
index 124aab3..a1cc181 100644
--- a/examples/decode_ico.c
+++ b/examples/decode_ico.c
@@ -21,12 +21,18 @@
#include <errno.h>
#include <stdbool.h>
#include <stdlib.h>
+#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <libnsbmp.h>
-#define BITMAP_BYTES_PER_PIXEL 4
+/* Currently the library returns the data in RGBA format,
+ * so there are 4 bytes per pixel */
+#define BYTES_PER_PIXEL 4
+
+/* White with alpha masking. */
+#define TRANSPARENT_COLOR 0xffffffff
unsigned char *load_file(const char *path, size_t *data_size);
void warning(const char *context, bmp_result code);
@@ -48,15 +54,18 @@ int main(int argc, char *argv[])
bitmap_get_buffer,
bitmap_get_bpp
};
- bmp_result code;
+ uint16_t width, height;
ico_collection ico;
+ bmp_result code;
struct bmp_image *bmp;
size_t size;
- if (argc != 2) {
- fprintf(stderr, "Usage: %s collection.ico\n", argv[0]);
+ if ((argc < 2) || (argc > 4)) {
+ fprintf(stderr, "Usage: %s collection.ico [width=255] [height=255]\n", argv[0]);
return 1;
}
+ width = (argc >= 3) ? atoi(argv[2]) : 255;
+ height = (argc == 4) ? atoi(argv[3]) : 255;
/* create our bmp image */
ico_collection_create(&ico, &bitmap_callbacks);
@@ -71,34 +80,38 @@ int main(int argc, char *argv[])
exit(1);
}
+ /* decode the image */
+ bmp = ico_find(&ico, width, height);
+ assert(bmp);
+
printf("P3\n");
printf("# %s\n", argv[1]);
- printf("# width %u \n", ico.width);
- printf("# height %u \n", ico.height);
- printf("%u %u 256\n", ico.width, ico.height);
+ printf("# width %u \n", bmp->width);
+ printf("# height %u \n", bmp->height);
+ printf("%u %u 256\n", bmp->width, bmp->height);
- /* decode the image */
- bmp = ico_find(&ico, 255, 255);
- assert(bmp);
- bmp_decode(bmp);
+ code = bmp_decode(bmp);
+ /* code = bmp_decode_trans(bmp, TRANSPARENT_COLOR); */
+ if (code != BMP_OK) {
+ warning("bmp_decode", code);
+ exit(1);
+ }
{
- unsigned int row, col;
- unsigned char *image;
- image = (unsigned char *) bmp->bitmap;
+ uint16_t row, col;
+ uint8_t *image;
+ image = (uint8_t *) bmp->bitmap;
for (row = 0; row != bmp->height; row++) {
for (col = 0; col != bmp->width; col++) {
- size_t z = (row * bmp->width + col) * BITMAP_BYTES_PER_PIXEL;
- printf("%u %u %u ",
- (unsigned char) image[z],
- (unsigned char) image[z + 1],
- (unsigned char) image[z + 2]);
+ size_t z = (row * bmp->width + col) * BYTES_PER_PIXEL;
+ printf("%u %u %u ", image[z],
+ image[z + 1],
+ image[z + 2]);
}
printf("\n");
}
}
/* clean up */
- bmp_finalise(bmp);
ico_finalise(&ico);
free(data);
@@ -170,7 +183,7 @@ void warning(const char *context, bmp_result code)
void *bitmap_create(int width, int height, unsigned int state)
{
(void) state; /* unused */
- return calloc(width * height, BITMAP_BYTES_PER_PIXEL);
+ return calloc(width * height, BYTES_PER_PIXEL);
}
@@ -200,7 +213,7 @@ unsigned char *bitmap_get_buffer(void *bitmap)
size_t bitmap_get_bpp(void *bitmap)
{
(void) bitmap; /* unused */
- return BITMAP_BYTES_PER_PIXEL;
+ return BYTES_PER_PIXEL;
}