Subversion Repositories Kolibri OS

Rev

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

  1. /*
  2.  * Copyright 2008 Vincent Sanders <vince@simtec.co.uk>
  3.  *
  4.  * This file is part of NetSurf, http://www.netsurf-browser.org/
  5.  *
  6.  * NetSurf is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; version 2 of the License.
  9.  *
  10.  * NetSurf is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  * GNU General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17.  */
  18.  
  19. #include <inttypes.h>
  20. #include <sys/types.h>
  21. #include <stdbool.h>
  22. #include <assert.h>
  23.  
  24. #include <libnsfb.h>
  25.  
  26. #include "image/bitmap.h"
  27. #include "utils/log.h"
  28.  
  29. /**
  30.  * Create a bitmap.
  31.  *
  32.  * \param  width   width of image in pixels
  33.  * \param  height  width of image in pixels
  34.  * \param  state   a flag word indicating the initial state
  35.  * \return an opaque struct bitmap, or NULL on memory exhaustion
  36.  */
  37.  
  38. void *bitmap_create(int width, int height, unsigned int state)
  39. {
  40.         nsfb_t *bm;
  41.  
  42.         LOG(("width %d, height %d, state %u",width,height,state));
  43.  
  44.         bm = nsfb_new(NSFB_SURFACE_RAM);
  45.         if (bm == NULL) {
  46.                 return NULL;
  47.         }
  48.  
  49.         if ((state & BITMAP_OPAQUE) == 0) {
  50.                 nsfb_set_geometry(bm, width, height, NSFB_FMT_ABGR8888);
  51.         } else {
  52.                 nsfb_set_geometry(bm, width, height, NSFB_FMT_XBGR8888);
  53.         }
  54.  
  55.         if (nsfb_init(bm) == -1) {
  56.                 nsfb_free(bm);
  57.                 return NULL;           
  58.         }
  59.  
  60.         LOG(("bitmap %p", bm));
  61.  
  62.         return bm;
  63. }
  64.  
  65.  
  66. /**
  67.  * Return a pointer to the pixel data in a bitmap.
  68.  *
  69.  * \param  bitmap  a bitmap, as returned by bitmap_create()
  70.  * \return pointer to the pixel buffer
  71.  *
  72.  * The pixel data is packed as BITMAP_FORMAT, possibly with padding at the end
  73.  * of rows. The width of a row in bytes is given by bitmap_get_rowstride().
  74.  */
  75.  
  76. unsigned char *bitmap_get_buffer(void *bitmap)
  77. {
  78.         nsfb_t *bm = bitmap;
  79.         unsigned char *bmpptr;
  80.  
  81.         assert(bm != NULL);
  82.  
  83.         nsfb_get_buffer(bm, &bmpptr, NULL);
  84.  
  85.         return bmpptr;
  86. }
  87.  
  88.  
  89. /**
  90.  * Find the width of a pixel row in bytes.
  91.  *
  92.  * \param  bitmap  a bitmap, as returned by bitmap_create()
  93.  * \return width of a pixel row in the bitmap
  94.  */
  95.  
  96. size_t bitmap_get_rowstride(void *bitmap)
  97. {
  98.         nsfb_t *bm = bitmap;
  99.         int bmpstride;
  100.  
  101.         assert(bm != NULL);
  102.  
  103.         nsfb_get_buffer(bm, NULL, &bmpstride);
  104.  
  105.         return bmpstride;
  106. }
  107.  
  108.  
  109. /**
  110.  * Free a bitmap.
  111.  *
  112.  * \param  bitmap  a bitmap, as returned by bitmap_create()
  113.  */
  114.  
  115. void bitmap_destroy(void *bitmap)
  116. {
  117.         nsfb_t *bm = bitmap;
  118.  
  119.         assert(bm != NULL);
  120.  
  121.         nsfb_free(bm);
  122. }
  123.  
  124.  
  125. /**
  126.  * Save a bitmap in the platform's native format.
  127.  *
  128.  * \param  bitmap  a bitmap, as returned by bitmap_create()
  129.  * \param  path    pathname for file
  130.  * \return true on success, false on error and error reported
  131.  */
  132.  
  133. bool bitmap_save(void *bitmap, const char *path, unsigned flags)
  134. {
  135.         return true;
  136. }
  137.  
  138.  
  139. /**
  140.  * The bitmap image has changed, so flush any persistant cache.
  141.  *
  142.  * \param  bitmap  a bitmap, as returned by bitmap_create()
  143.  */
  144. void bitmap_modified(void *bitmap) {
  145. }
  146.  
  147. /**
  148.  * Sets wether a bitmap should be plotted opaque
  149.  *
  150.  * \param  bitmap  a bitmap, as returned by bitmap_create()
  151.  * \param  opaque  whether the bitmap should be plotted opaque
  152.  */
  153. void bitmap_set_opaque(void *bitmap, bool opaque)
  154. {      
  155.         nsfb_t *bm = bitmap;
  156.  
  157.         assert(bm != NULL);
  158.  
  159.         LOG(("setting bitmap %p to %s", bm, opaque?"opaque":"transparent"));
  160.  
  161.         if (opaque) {
  162.                 nsfb_set_geometry(bm, 0, 0, NSFB_FMT_XBGR8888);
  163.         } else {
  164.                 nsfb_set_geometry(bm, 0, 0, NSFB_FMT_ABGR8888);
  165.         }
  166. }
  167.  
  168.  
  169. /**
  170.  * Tests whether a bitmap has an opaque alpha channel
  171.  *
  172.  * \param  bitmap  a bitmap, as returned by bitmap_create()
  173.  * \return whether the bitmap is opaque
  174.  */
  175. bool bitmap_test_opaque(void *bitmap)
  176. {
  177.         int tst;
  178.         nsfb_t *bm = bitmap;
  179.         unsigned char *bmpptr;
  180.         int width;
  181.         int height;
  182.  
  183.         assert(bm != NULL);
  184.  
  185.         nsfb_get_buffer(bm, &bmpptr, NULL);
  186.  
  187.         nsfb_get_geometry(bm, &width, &height, NULL);
  188.  
  189.         tst = width * height;
  190.  
  191.         while (tst-- > 0) {
  192.                 if (bmpptr[(tst << 2) + 3] != 0xff) {
  193.                         LOG(("bitmap %p has transparency",bm));
  194.                         return false;                    
  195.                 }  
  196.         }
  197.         LOG(("bitmap %p is opaque", bm));
  198.         return true;
  199. }
  200.  
  201.  
  202. /**
  203.  * Gets weather a bitmap should be plotted opaque
  204.  *
  205.  * \param  bitmap  a bitmap, as returned by bitmap_create()
  206.  */
  207. bool bitmap_get_opaque(void *bitmap)
  208. {
  209.         nsfb_t *bm = bitmap;
  210.         enum nsfb_format_e format;
  211.  
  212.         assert(bm != NULL);
  213.  
  214.         nsfb_get_geometry(bm, NULL, NULL, &format);
  215.  
  216.         if (format == NSFB_FMT_ABGR8888)
  217.                 return false;
  218.  
  219.         return true;
  220. }
  221.  
  222. int bitmap_get_width(void *bitmap)
  223. {
  224.         nsfb_t *bm = bitmap;
  225.         int width;
  226.  
  227.         assert(bm != NULL);
  228.  
  229.         nsfb_get_geometry(bm, &width, NULL, NULL);
  230.  
  231.         return(width);
  232. }
  233.  
  234. int bitmap_get_height(void *bitmap)
  235. {
  236.         nsfb_t *bm = bitmap;
  237.         int height;
  238.  
  239.         assert(bm != NULL);
  240.  
  241.         nsfb_get_geometry(bm, NULL, &height, NULL);
  242.  
  243.         return(height);
  244. }
  245.  
  246. /* get bytes per pixel */
  247. size_t bitmap_get_bpp(void *bitmap)
  248. {
  249.         return 4;
  250. }
  251.  
  252. /*
  253.  * Local Variables:
  254.  * c-basic-offset:8
  255.  * End:
  256.  */
  257.