/src/canvas/skia/ftk_bitmap_skia.cpp

http://ftk.googlecode.com/ · C++ · 157 lines · 99 code · 30 blank · 28 comment · 15 complexity · 7daff10f25422bf8f3abd66dbe104adf MD5 · raw file

  1. /*
  2. * File: ftk_bitmap_skia.c
  3. * Author: Li XianJing <xianjimli@hotmail.com>
  4. * Brief: skia bitmap implemntation.
  5. *
  6. * Copyright (c) 2009 - 2011 Li XianJing <xianjimli@hotmail.com>
  7. *
  8. * Licensed under the Academic Free License version 2.1
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. /*
  25. * History:
  26. * ================================================================
  27. * 2011-10-03 Li XianJing <xianjimli@hotmail.com> created
  28. */
  29. #include "ftk_bitmap.h"
  30. #include "SkColor.h"
  31. #include "SkCanvas.h"
  32. #include "ftk_globals.h"
  33. #include "SkImageEncoder.h"
  34. #include "effects/SkPorterDuff.h"
  35. using namespace android;
  36. FtkBitmap* ftk_bitmap_create(int w, int h, FtkColor color)
  37. {
  38. FtkBitmap* thiz = (FtkBitmap*)FTK_ALLOC(sizeof(FtkBitmap));
  39. if(thiz != NULL)
  40. {
  41. SkBitmap* bitmap = new SkBitmap();
  42. if(bitmap != NULL)
  43. {
  44. thiz->ref = 1;
  45. thiz->data = bitmap;
  46. bitmap->setConfig(SkBitmap::kARGB_8888_Config, w, h, 0);
  47. bitmap->allocPixels();
  48. bitmap->eraseARGB(color.a, color.r, color.g, color.b);
  49. }
  50. else
  51. {
  52. FTK_FREE(thiz);
  53. }
  54. }
  55. return thiz;
  56. }
  57. FtkBitmap* ftk_bitmap_create_with_native(void* bitmap)
  58. {
  59. FtkBitmap* thiz = (FtkBitmap*)FTK_ALLOC(sizeof(FtkBitmap));
  60. if(thiz != NULL)
  61. {
  62. thiz->ref = 1;
  63. thiz->data = bitmap;
  64. }
  65. return thiz;
  66. }
  67. void* ftk_bitmap_get_native(FtkBitmap* thiz)
  68. {
  69. return_val_if_fail(thiz != NULL, NULL);
  70. return thiz->data;
  71. }
  72. int ftk_bitmap_width(FtkBitmap* thiz)
  73. {
  74. SkBitmap* bitmap = NULL;
  75. return_val_if_fail(thiz != NULL, 0);
  76. bitmap = (SkBitmap*)thiz->data;
  77. return bitmap->width();
  78. }
  79. int ftk_bitmap_height(FtkBitmap* thiz)
  80. {
  81. SkBitmap* bitmap = NULL;
  82. return_val_if_fail(thiz != NULL, 0);
  83. bitmap = (SkBitmap*)thiz->data;
  84. return bitmap->height();
  85. }
  86. FtkColor* ftk_bitmap_lock(FtkBitmap* thiz)
  87. {
  88. SkBitmap* bitmap = NULL;
  89. return_val_if_fail(thiz != NULL, NULL);
  90. bitmap = (SkBitmap*)thiz->data;
  91. return (FtkColor*)bitmap->getPixels();
  92. }
  93. void ftk_bitmap_unlock(FtkBitmap* thiz)
  94. {
  95. return;
  96. }
  97. FtkColor ftk_bitmap_get_pixel(FtkBitmap* thiz, int x, int y)
  98. {
  99. SkColor sc;
  100. FtkColor c = {0, 0, 0, 0};
  101. return_val_if_fail(thiz != NULL && x < ftk_bitmap_width(thiz) && y < ftk_bitmap_height(thiz), c);
  102. SkBitmap* bitmap = (SkBitmap*)thiz->data;
  103. #ifdef ANDROID
  104. sc = *bitmap->getAddr32(x, y);
  105. c.a = SkColorGetA(sc);
  106. c.r = SkColorGetR(sc);
  107. c.g = SkColorGetG(sc);
  108. c.b = SkColorGetB(sc);
  109. #else
  110. sc = bitmap->getColor(x, y);
  111. c.a = SkColorGetA(sc);
  112. c.r = SkColorGetR(sc);
  113. c.g = SkColorGetG(sc);
  114. c.b = SkColorGetB(sc);
  115. #endif
  116. return c;
  117. }
  118. void ftk_bitmap_destroy(FtkBitmap* thiz)
  119. {
  120. if(thiz != NULL)
  121. {
  122. SkBitmap* bitmap = (SkBitmap*)thiz->data;
  123. delete bitmap;
  124. FTK_ZFREE(thiz, sizeof(FtkBitmap));
  125. }
  126. return;
  127. }