From 3bd20517eb94e2d44b68e4cafdf98ffb37936171 Mon Sep 17 00:00:00 2001 From: Sean Fox Date: Fri, 27 Jun 2008 04:06:59 +0000 Subject: Added bmp decoding example svn path=/branches/dynis/libnsbmp/; revision=4462 --- Makefile | 5 +- bin/decode_bmp | Bin 0 -> 36588 bytes bmp_display | 3 + examples/decode_bmp.c | 220 ++++++++++++++++++++++++++++++++++++++++++++++++++ examples/linux.bmp | Bin 0 -> 315054 bytes examples/ro.bmp | Bin 0 -> 307590 bytes 6 files changed, 227 insertions(+), 1 deletion(-) create mode 100755 bin/decode_bmp create mode 100755 bmp_display create mode 100644 examples/decode_bmp.c create mode 100644 examples/linux.bmp create mode 100644 examples/ro.bmp diff --git a/Makefile b/Makefile index 118bf45..175ff93 100644 --- a/Makefile +++ b/Makefile @@ -23,7 +23,7 @@ DESTDIR ?= .PHONY: all clean docs install uninstall -all: libnsbmp.a +all: libnsbmp.a bin/decode_bmp libnsbmp.a: libnsbmp.o libnsbmp.pc ${AR} ${ARFLAGS} libnsbmp.a libnsbmp.o @@ -34,6 +34,9 @@ libnsbmp.pc: libnsbmp.pc.in %.o: %.c ${CC} -c ${CFLAGS} -o $@ $< +bin/decode_bmp: examples/decode_bmp.c libnsbmp.a + ${CC} ${CFLAGS} -o $@ $< libnsbmp.a + docs: ${DOXYGEN} diff --git a/bin/decode_bmp b/bin/decode_bmp new file mode 100755 index 0000000..f0f6c23 Binary files /dev/null and b/bin/decode_bmp differ diff --git a/bmp_display b/bmp_display new file mode 100755 index 0000000..dd0458c --- /dev/null +++ b/bmp_display @@ -0,0 +1,3 @@ +set -e +make +bin/decode_bmp $1 | display diff --git a/examples/decode_bmp.c b/examples/decode_bmp.c new file mode 100644 index 0000000..685d75a --- /dev/null +++ b/examples/decode_bmp.c @@ -0,0 +1,220 @@ +/* + * Copyright 2008 Sean Fox + * Copyright 2008 James Bursa + * + * This file is part of NetSurf, http://www.netsurf-browser.org/ + * + * NetSurf is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * NetSurf is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#define BITMAP_BYTES_PER_PIXEL 4 + +unsigned char *load_file(const char *path, size_t *data_size); +void warning(const char *context, bmp_result code); +void *bitmap_create(int width, int height, unsigned int state); +void bitmap_set_suspendable(void *bitmap, void *private_word, + void (*invalidate)(void *bitmap, void *private_word)); +void invalidate(void *bitmap, void *private_word); +unsigned char *bitmap_get_buffer(void *bitmap); +size_t bitmap_get_bpp(void *bitmap); +void bitmap_destroy(void *bitmap); + +/* The Bitmap callbacks function table; + necessary for interaction with nsgiflib. +*/ +bmp_bitmap_callback_vt bitmap_callbacks = { + bitmap_create, + bitmap_destroy, + bitmap_set_suspendable, + bitmap_get_buffer, + bitmap_get_bpp +}; + + +int main(int argc, char *argv[]) +{ + bmp_result code; + bmp_image bmp; + size_t size; + + if (argc != 2) { + fprintf(stderr, "Usage: %s image.bmp\n", argv[0]); + return 1; + } + + /* create our bmp image */ + bmp_create(&bmp); + + /* load file into memory */ + unsigned char *data = load_file(argv[1], &size); + + /* set our source data */ + bmp.bmp_data = data; + bmp.buffer_size = size; + + /* analyse the BMP */ + code = bmp_analyse(&bmp, &bitmap_callbacks); + if (code != BMP_OK) { + warning("bmp_analyse", code); + exit(1); + } + + printf("P3\n"); + printf("# %s\n", argv[1]); + printf("# width %u \n", bmp.width); + printf("# height %u \n", bmp.height); + printf("%u %u 256\n", bmp.width, bmp.height); + + /* decode the image */ + code = bmp_decode(&bmp, &bitmap_callbacks); + if (code != BMP_OK) { + warning("bmp_decode", code); + exit(1); + } + { + unsigned int row, col; + unsigned char *image; + image = (unsigned char *) 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]); + } + printf("\n"); + } + } + + /* clean up */ + bmp_finalise(&bmp, &bitmap_callbacks); + free(data); + + return 0; +} + + +unsigned char *load_file(const char *path, size_t *data_size) +{ + FILE *fd; + struct stat sb; + unsigned char *buffer; + size_t size; + size_t n; + + fd = fopen(path, "rb"); + if (!fd) { + perror(path); + exit(EXIT_FAILURE); + } + + if (stat(path, &sb)) { + perror(path); + exit(EXIT_FAILURE); + } + size = sb.st_size; + + buffer = malloc(size); + if (!buffer) { + fprintf(stderr, "Unable to allocate %lld bytes\n", + (long long) size); + exit(EXIT_FAILURE); + } + + n = fread(buffer, 1, size, fd); + if (n != size) { + perror(path); + exit(EXIT_FAILURE); + } + + fclose(fd); + + *data_size = size; + return buffer; +} + + +void warning(const char *context, bmp_result code) +{ + fprintf(stderr, "%s failed: ", context); + switch (code) { + case BMP_INSUFFICIENT_MEMORY: + fprintf(stderr, "BMP_INSUFFICIENT_MEMORY"); + break; + case BMP_INSUFFICIENT_DATA: + fprintf(stderr, "BMP_INSUFFICIENT_DATA"); + break; + case BMP_DATA_ERROR: + fprintf(stderr, "BMP_DATA_ERROR"); + break; + default: + fprintf(stderr, "unknown code %i", code); + break; + } + fprintf(stderr, "\n"); +} + + +void *bitmap_create(int width, int height, unsigned int state) +{ + (void) state; /* unused */ + return calloc(width * height, BITMAP_BYTES_PER_PIXEL); +} + + +void bitmap_set_suspendable(void *bitmap, void *private_word, + void (*invalidate)(void *bitmap, void *private_word)) +{ + (void) bitmap; /* unused */ + (void) private_word; /* unused */ + (void) invalidate; /* unused */ +} + + +void invalidate(void *bitmap, void *private_word) +{ + (void) bitmap; /* unused */ + (void) private_word; /* unused */ +} + + +unsigned char *bitmap_get_buffer(void *bitmap) +{ + assert(bitmap); + return bitmap; +} + + +size_t bitmap_get_bpp(void *bitmap) +{ + (void) bitmap; /* unused */ + return BITMAP_BYTES_PER_PIXEL; +} + + +void bitmap_destroy(void *bitmap) +{ + assert(bitmap); + free(bitmap); +} + diff --git a/examples/linux.bmp b/examples/linux.bmp new file mode 100644 index 0000000..ac9b30c Binary files /dev/null and b/examples/linux.bmp differ diff --git a/examples/ro.bmp b/examples/ro.bmp new file mode 100644 index 0000000..ddd5e8f Binary files /dev/null and b/examples/ro.bmp differ -- cgit v1.2.3