/src/canvas/skia/ftk_bitmap_factory_skia.cpp

http://ftk.googlecode.com/ · C++ · 120 lines · 71 code · 20 blank · 29 comment · 5 complexity · b2f269a8c44c4ae093192e3c3f1282da MD5 · raw file

  1. /*
  2. * File: ftk_bitmap_factory.c
  3. * Author: Li XianJing <xianjimli@hotmail.com>
  4. * Brief: bitmap factory.
  5. *
  6. * Copyright (c) 2009 - 2010 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. * 2009-10-03 Li XianJing <xianjimli@hotmail.com> created
  28. *
  29. */
  30. #include "SkString.h"
  31. #include "SkBitmap.h"
  32. #include "SkColor.h"
  33. #include "SkStream.h"
  34. #include "SkTemplates.h"
  35. #include "images/SkImageDecoder.h"
  36. #include "effects/SkPorterDuff.h"
  37. #include "ftk_log.h"
  38. #include "ftk_allocator.h"
  39. #include "ftk_bitmap_factory.h"
  40. #ifdef ANDROID
  41. using namespace android;
  42. #include "ftk_jni.h"
  43. #endif
  44. struct _FtkBitmapFactory
  45. {
  46. int unused;
  47. };
  48. FtkBitmapFactory* ftk_bitmap_factory_create(void)
  49. {
  50. FtkBitmapFactory* thiz = (FtkBitmapFactory*)FTK_ZALLOC(sizeof(FtkBitmapFactory));
  51. return thiz;
  52. }
  53. static bool decodeFile(SkBitmap* bitmap, const char srcPath[]) {
  54. SkFILEStream stream(srcPath);
  55. if (!stream.isValid()) {
  56. SkDebugf("ERROR: bad filename <%s>\n", srcPath);
  57. return false;
  58. }
  59. SkImageDecoder* codec = SkImageDecoder::Factory(&stream);
  60. if (NULL == codec) {
  61. SkDebugf("ERROR: no codec found for <%s>\n", srcPath);
  62. return false;
  63. }
  64. SkAutoTDelete<SkImageDecoder> ad(codec);
  65. stream.rewind();
  66. if (!codec->decode(&stream, bitmap, SkBitmap::kARGB_8888_Config,
  67. SkImageDecoder::kDecodePixels_Mode)) {
  68. SkDebugf("ERROR: codec failed for <%s>\n", srcPath);
  69. return false;
  70. }
  71. return true;
  72. }
  73. static void destroy_bitmap(void* data)
  74. {
  75. SkBitmap* b = (SkBitmap*)data;
  76. delete b;
  77. return;
  78. }
  79. FtkBitmap* ftk_bitmap_factory_load(FtkBitmapFactory* thiz, const char* filename)
  80. {
  81. #ifdef ANDROID
  82. return Android_LoadImage(filename);
  83. #else
  84. SkBitmap* b = new SkBitmap();
  85. FtkBitmap* bmp = NULL;
  86. if (decodeFile(b, filename))
  87. {
  88. bmp = ftk_bitmap_create_with_native(b);
  89. }
  90. return bmp;
  91. #endif
  92. }
  93. Ret ftk_bitmap_factory_add_decoder(FtkBitmapFactory* thiz, FtkImageDecoder* decoder)
  94. {
  95. return RET_OK;
  96. }
  97. void ftk_bitmap_factory_destroy(FtkBitmapFactory* thiz)
  98. {
  99. return;
  100. }