Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright 2008 Sean Fox <dyntryx@gmail.com>
  3.  * Copyright 2008 James Bursa <james@netsurf-browser.org>
  4.  *
  5.  * This file is part of NetSurf's libnsbmp, http://www.netsurf-browser.org/
  6.  * Licenced under the MIT License,
  7.  *                http://www.opensource.org/licenses/mit-license.php
  8.  */
  9.  
  10. #include <assert.h>
  11. #include <errno.h>
  12. #include <stdbool.h>
  13. #include <stdlib.h>
  14. #include <stdint.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <sys/stat.h>
  18. #include "../include/libnsbmp.h"
  19.  
  20. #define BYTES_PER_PIXEL 4
  21. #define TRANSPARENT_COLOR 0xffffffff
  22.  
  23. unsigned char *load_file(const char *path, size_t *data_size);
  24. void warning(const char *context, bmp_result code);
  25. void *bitmap_create(int width, int height, unsigned int state);
  26. unsigned char *bitmap_get_buffer(void *bitmap);
  27. size_t bitmap_get_bpp(void *bitmap);
  28. void bitmap_destroy(void *bitmap);
  29.  
  30.  
  31. int main(int argc, char *argv[])
  32. {
  33.         bmp_bitmap_callback_vt bitmap_callbacks = {
  34.                 bitmap_create,
  35.                 bitmap_destroy,
  36.                 bitmap_get_buffer,
  37.                 bitmap_get_bpp
  38.         };
  39.         bmp_result code;
  40.         bmp_image bmp;
  41.         size_t size;
  42.         unsigned short res = 0;
  43.  
  44.         if (argc != 2) {
  45.                 fprintf(stderr, "Usage: %s image.bmp\n", argv[0]);
  46.                 return 1;
  47.         }
  48.  
  49.         /* create our bmp image */
  50.         bmp_create(&bmp, &bitmap_callbacks);
  51.  
  52.         /* load file into memory */
  53.         unsigned char *data = load_file(argv[1], &size);
  54.  
  55.         /* analyse the BMP */
  56.         code = bmp_analyse(&bmp, size, data);
  57.         if (code != BMP_OK) {
  58.                 warning("bmp_analyse", code);
  59.                 res = 1;
  60.                 goto cleanup;
  61.         }
  62.  
  63.         /* decode the image */
  64.         code = bmp_decode(&bmp);
  65.         /* code = bmp_decode_trans(&bmp, TRANSPARENT_COLOR); */
  66.         if (code != BMP_OK) {
  67.                 warning("bmp_decode", code);
  68.                 /* allow partially decoded images */
  69.                 if (code != BMP_INSUFFICIENT_DATA) {
  70.                         res = 1;
  71.                         goto cleanup;
  72.                 }
  73.         }
  74.  
  75.         printf("P3\n");
  76.         printf("# %s\n", argv[1]);
  77.         printf("# width                %u \n", bmp.width);
  78.         printf("# height               %u \n", bmp.height);
  79.         printf("%u %u 256\n", bmp.width, bmp.height);
  80.  
  81.         {
  82.                 uint16_t row, col;
  83.                 uint8_t *image;
  84.                 image = (uint8_t *) bmp.bitmap;
  85.                 for (row = 0; row != bmp.height; row++) {
  86.                         for (col = 0; col != bmp.width; col++) {
  87.                                 size_t z = (row * bmp.width + col) * BYTES_PER_PIXEL;
  88.                                 printf("%u %u %u ",     image[z],
  89.                                                         image[z + 1],
  90.                                                         image[z + 2]);
  91.                         }
  92.                         printf("\n");
  93.                 }
  94.         }
  95.  
  96. cleanup:
  97.         /* clean up */
  98.         bmp_finalise(&bmp);
  99.         free(data);
  100.  
  101.         return res;
  102. }
  103.  
  104.  
  105. unsigned char *load_file(const char *path, size_t *data_size)
  106. {
  107.         FILE *fd;
  108.         struct stat sb;
  109.         unsigned char *buffer;
  110.         size_t size;
  111.         size_t n;
  112.  
  113.         fd = fopen(path, "rb");
  114.         if (!fd) {
  115.                 perror(path);
  116.                 exit(EXIT_FAILURE);
  117.         }
  118.  
  119.         if (stat(path, &sb)) {
  120.                 perror(path);
  121.                 exit(EXIT_FAILURE);
  122.         }
  123.         size = sb.st_size;
  124.  
  125.         buffer = malloc(size);
  126.         if (!buffer) {
  127.                 fprintf(stderr, "Unable to allocate %lld bytes\n",
  128.                                 (long long) size);
  129.                 exit(EXIT_FAILURE);
  130.         }
  131.  
  132.         n = fread(buffer, 1, size, fd);
  133.         if (n != size) {
  134.                 perror(path);
  135.                 exit(EXIT_FAILURE);
  136.         }
  137.  
  138.         fclose(fd);
  139.  
  140.         *data_size = size;
  141.         return buffer;
  142. }
  143.  
  144.  
  145. void warning(const char *context, bmp_result code)
  146. {
  147.         fprintf(stderr, "%s failed: ", context);
  148.         switch (code) {
  149.                 case BMP_INSUFFICIENT_MEMORY:
  150.                         fprintf(stderr, "BMP_INSUFFICIENT_MEMORY");
  151.                         break;
  152.                 case BMP_INSUFFICIENT_DATA:
  153.                         fprintf(stderr, "BMP_INSUFFICIENT_DATA");
  154.                         break;
  155.                 case BMP_DATA_ERROR:
  156.                         fprintf(stderr, "BMP_DATA_ERROR");
  157.                         break;
  158.                 default:
  159.                         fprintf(stderr, "unknown code %i", code);
  160.                         break;
  161.         }
  162.         fprintf(stderr, "\n");
  163. }
  164.  
  165.  
  166. void *bitmap_create(int width, int height, unsigned int state)
  167. {
  168.         (void) state;  /* unused */
  169.         return calloc(width * height, BYTES_PER_PIXEL);
  170. }
  171.  
  172.  
  173. unsigned char *bitmap_get_buffer(void *bitmap)
  174. {
  175.         assert(bitmap);
  176.         return bitmap;
  177. }
  178.  
  179.  
  180. size_t bitmap_get_bpp(void *bitmap)
  181. {
  182.         (void) bitmap;  /* unused */
  183.         return BYTES_PER_PIXEL;
  184. }
  185.  
  186.  
  187. void bitmap_destroy(void *bitmap)
  188. {
  189.         assert(bitmap);
  190.         free(bitmap);
  191. }
  192.  
  193.