Subversion Repositories Kolibri OS

Rev

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

  1. /*
  2.  * Copyright 2006 Richard Wilson <richard.wilson@netsurf-browser.org>
  3.  * Copyright 2008 Sean Fox <dyntryx@gmail.com>
  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. /** \file
  11.  * BMP file decoding (interface).
  12.  */
  13.  
  14. #ifndef libnsbmp_h_
  15. #define libnsbmp_h_
  16.  
  17. #include <stdbool.h>
  18. #include <stdint.h>
  19. #include <stddef.h>
  20.  
  21. /* bmp flags */
  22. #define BMP_NEW                 0
  23. #define BMP_OPAQUE              (1 << 0)        /** image is opaque (as opposed to having an alpha mask) */
  24. #define BMP_CLEAR_MEMORY        (1 << 1)        /** memory should be wiped */
  25.  
  26. /* error return values */
  27. typedef enum {
  28.         BMP_OK = 0,
  29.         BMP_INSUFFICIENT_MEMORY = 1,
  30.         BMP_INSUFFICIENT_DATA = 2,
  31.         BMP_DATA_ERROR = 3
  32. } bmp_result;
  33.  
  34. /* encoding types */
  35. typedef enum {
  36.         BMP_ENCODING_RGB = 0,
  37.         BMP_ENCODING_RLE8 = 1,
  38.         BMP_ENCODING_RLE4 = 2,
  39.         BMP_ENCODING_BITFIELDS = 3
  40. } bmp_encoding;
  41.  
  42. /*      API for Bitmap callbacks
  43. */
  44. typedef void* (*bmp_bitmap_cb_create)(int width, int height, unsigned int state);
  45. typedef void (*bmp_bitmap_cb_destroy)(void *bitmap);
  46. typedef unsigned char* (*bmp_bitmap_cb_get_buffer)(void *bitmap);
  47. typedef size_t (*bmp_bitmap_cb_get_bpp)(void *bitmap);
  48.  
  49. /*      The Bitmap callbacks function table
  50. */
  51. typedef struct bmp_bitmap_callback_vt_s {
  52.         bmp_bitmap_cb_create bitmap_create;                     /**< Create a bitmap. */
  53.         bmp_bitmap_cb_destroy bitmap_destroy;                   /**< Free a bitmap. */
  54.         bmp_bitmap_cb_get_buffer bitmap_get_buffer;             /**< Return a pointer to the pixel data in a bitmap. */
  55.         bmp_bitmap_cb_get_bpp bitmap_get_bpp;                   /**< Find the width of a pixel row in bytes. */
  56. } bmp_bitmap_callback_vt;
  57.  
  58. typedef struct bmp_image {
  59.         bmp_bitmap_callback_vt bitmap_callbacks;        /**< callbacks for bitmap functions */
  60.         uint8_t *bmp_data;                              /** pointer to BMP data */
  61.         uint32_t width;                                 /** width of BMP (valid after _analyse) */
  62.         uint32_t height;                                /** heigth of BMP (valid after _analyse) */
  63.         bool decoded;                                   /** whether the image has been decoded */
  64.         void *bitmap;                                   /** decoded image */
  65.         /**     Internal members are listed below
  66.         */
  67.         uint32_t buffer_size;                           /** total number of bytes of BMP data available */
  68.         bmp_encoding encoding;                          /** pixel encoding type */
  69.         uint32_t bitmap_offset;                         /** offset of bitmap data */
  70.         uint16_t bpp;                                   /** bits per pixel */
  71.         uint32_t colours;                               /** number of colours */
  72.         uint32_t *colour_table;                         /** colour table */
  73.         bool limited_trans;                             /** whether to use bmp's limited transparency */
  74.         uint32_t trans_colour;                          /** colour to display for "transparent" pixels when
  75.                                                           * using limited transparency */
  76.         bool reversed;                                  /** scanlines are top to bottom */
  77.         bool ico;                                       /** image is part of an ICO, mask follows */
  78.         bool opaque;                                    /** true if the bitmap does not contain an alpha channel */
  79.         uint32_t mask[4];                               /** four bitwise mask */
  80.         int32_t shift[4];                               /** four bitwise shifts */
  81.         uint32_t transparent_index;                     /** colour representing "transparency" in the bitmap */
  82. } bmp_image;
  83.  
  84. typedef struct ico_image {
  85.         bmp_image bmp;
  86.         struct ico_image *next;
  87. } ico_image;
  88.  
  89. typedef struct ico_collection {
  90.         bmp_bitmap_callback_vt bitmap_callbacks;        /**< callbacks for bitmap functions */
  91.         uint16_t width;                                 /** width of largest BMP */
  92.         uint16_t height;                                /** heigth of largest BMP */
  93.         /**     Internal members are listed below
  94.         */
  95.         uint8_t *ico_data;                              /** pointer to ICO data */
  96.         uint32_t buffer_size;                           /** total number of bytes of ICO data available */
  97.         ico_image *first;
  98. } ico_collection;
  99.  
  100. void bmp_create(bmp_image *bmp, bmp_bitmap_callback_vt *bitmap_callbacks);
  101. void ico_collection_create(ico_collection *ico,
  102.                 bmp_bitmap_callback_vt *bitmap_callbacks);
  103. bmp_result bmp_analyse(bmp_image *bmp, size_t size, uint8_t *data);
  104. bmp_result bmp_decode(bmp_image *bmp);
  105. bmp_result bmp_decode_trans(bmp_image *bmp, uint32_t transparent_colour);
  106. void bmp_finalise(bmp_image *bmp);
  107.  
  108. bmp_result ico_analyse(ico_collection *ico, size_t size, uint8_t *data);
  109. bmp_image *ico_find(ico_collection *ico, uint16_t width, uint16_t height);
  110. void ico_finalise(ico_collection *ico);
  111.  
  112. #endif
  113.