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. /* Currently the library returns the data in RGBA format,
  21.  * so there are 4 bytes per pixel */
  22. #define BYTES_PER_PIXEL 4
  23.  
  24. /* White with alpha masking. */
  25. #define TRANSPARENT_COLOR 0xffffffff
  26.  
  27. unsigned char *load_file(const char *path, size_t *data_size);
  28. void warning(const char *context, bmp_result code);
  29. void *bitmap_create(int width, int height, unsigned int state);
  30. unsigned char *bitmap_get_buffer(void *bitmap);
  31. size_t bitmap_get_bpp(void *bitmap);
  32. void bitmap_destroy(void *bitmap);
  33.  
  34.  
  35. int main(int argc, char *argv[])
  36. {
  37.         bmp_bitmap_callback_vt bitmap_callbacks = {
  38.                 bitmap_create,
  39.                 bitmap_destroy,
  40.                 bitmap_get_buffer,
  41.                 bitmap_get_bpp
  42.         };
  43.         uint16_t width, height;
  44.         ico_collection ico;
  45.         bmp_result code;
  46.         struct bmp_image *bmp;
  47.         size_t size;
  48.         unsigned short res = 0;
  49.  
  50.         if ((argc < 2) || (argc > 4)) {
  51.                 fprintf(stderr, "Usage: %s collection.ico [width=255] [height=255]\n", argv[0]);
  52.                 return 1;
  53.         }
  54.         width = (argc >= 3) ?  atoi(argv[2]) : 255;
  55.         height = (argc == 4) ? atoi(argv[3]) : 255;
  56.  
  57.         /* create our bmp image */
  58.         ico_collection_create(&ico, &bitmap_callbacks);
  59.  
  60.         /* load file into memory */
  61.         unsigned char *data = load_file(argv[1], &size);
  62.  
  63.         /* analyse the BMP */
  64.         code = ico_analyse(&ico, size, data);
  65.         if (code != BMP_OK) {
  66.                 warning("ico_analyse", code);
  67.                 res = 1;
  68.                 goto cleanup;
  69.         }
  70.  
  71.         /* decode the image */
  72.         bmp = ico_find(&ico, width, height);
  73.         assert(bmp);
  74.  
  75.         code = bmp_decode(bmp);
  76.         /* code = bmp_decode_trans(bmp, TRANSPARENT_COLOR); */
  77.         if (code != BMP_OK) {
  78.                 warning("bmp_decode", code);
  79.                 /* allow partially decoded images */
  80.                 if (code != BMP_INSUFFICIENT_DATA) {
  81.                         res = 1;
  82.                         goto cleanup;
  83.                 }
  84.         }
  85.  
  86.         printf("P3\n");
  87.         printf("# %s\n", argv[1]);
  88.         printf("# width                %u \n", bmp->width);
  89.         printf("# height               %u \n", bmp->height);
  90.         printf("%u %u 256\n", bmp->width, bmp->height);
  91.  
  92.         {
  93.                 uint16_t row, col;
  94.                 uint8_t *image;
  95.                 image = (uint8_t *) bmp->bitmap;
  96.                 for (row = 0; row != bmp->height; row++) {
  97.                         for (col = 0; col != bmp->width; col++) {
  98.                                 size_t z = (row * bmp->width + col) * BYTES_PER_PIXEL;
  99.                                 printf("%u %u %u ",     image[z],
  100.                                                         image[z + 1],
  101.                                                         image[z + 2]);
  102.                         }
  103.                         printf("\n");
  104.                 }
  105.         }
  106.  
  107. cleanup:
  108.         /* clean up */
  109.         ico_finalise(&ico);
  110.         free(data);
  111.  
  112.         return res;
  113. }
  114.  
  115.  
  116. unsigned char *load_file(const char *path, size_t *data_size)
  117. {
  118.         FILE *fd;
  119.         struct stat sb;
  120.         unsigned char *buffer;
  121.         size_t size;
  122.         size_t n;
  123.  
  124.         fd = fopen(path, "rb");
  125.         if (!fd) {
  126.                 perror(path);
  127.                 exit(EXIT_FAILURE);
  128.         }
  129.  
  130.         if (stat(path, &sb)) {
  131.                 perror(path);
  132.                 exit(EXIT_FAILURE);
  133.         }
  134.         size = sb.st_size;
  135.  
  136.         buffer = malloc(size);
  137.         if (!buffer) {
  138.                 fprintf(stderr, "Unable to allocate %lld bytes\n",
  139.                                 (long long) size);
  140.                 exit(EXIT_FAILURE);
  141.         }
  142.  
  143.         n = fread(buffer, 1, size, fd);
  144.         if (n != size) {
  145.                 perror(path);
  146.                 exit(EXIT_FAILURE);
  147.         }
  148.  
  149.         fclose(fd);
  150.  
  151.         *data_size = size;
  152.         return buffer;
  153. }
  154.  
  155.  
  156. void warning(const char *context, bmp_result code)
  157. {
  158.         fprintf(stderr, "%s failed: ", context);
  159.         switch (code) {
  160.                 case BMP_INSUFFICIENT_MEMORY:
  161.                         fprintf(stderr, "BMP_INSUFFICIENT_MEMORY");
  162.                         break;
  163.                 case BMP_INSUFFICIENT_DATA:
  164.                         fprintf(stderr, "BMP_INSUFFICIENT_DATA");
  165.                         break;
  166.                 case BMP_DATA_ERROR:
  167.                         fprintf(stderr, "BMP_DATA_ERROR");
  168.                         break;
  169.                 default:
  170.                         fprintf(stderr, "unknown code %i", code);
  171.                         break;
  172.         }
  173.         fprintf(stderr, "\n");
  174. }
  175.  
  176.  
  177. void *bitmap_create(int width, int height, unsigned int state)
  178. {
  179.         (void) state;  /* unused */
  180.         return calloc(width * height, BYTES_PER_PIXEL);
  181. }
  182.  
  183.  
  184. unsigned char *bitmap_get_buffer(void *bitmap)
  185. {
  186.         assert(bitmap);
  187.         return bitmap;
  188. }
  189.  
  190.  
  191. size_t bitmap_get_bpp(void *bitmap)
  192. {
  193.         (void) bitmap;  /* unused */
  194.         return BYTES_PER_PIXEL;
  195. }
  196.  
  197.  
  198. void bitmap_destroy(void *bitmap)
  199. {
  200.         assert(bitmap);
  201.         free(bitmap);
  202. }
  203.  
  204.