/src/os/ucos-ii/ftk_image_win32_decoder.cpp

http://ftk.googlecode.com/ · C++ · 131 lines · 78 code · 24 blank · 29 comment · 18 complexity · d473e2f8264f9104fc878f8de7bb3c53 MD5 · raw file

  1. /*
  2. * File: ftk_image_win32_decoder.c
  3. * Author: Li XianJing <xianjimli@hotmail.com>
  4. * Brief: png/jpeg format image decoder on win32.
  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. * 2010-01-17 Li XianJing <xianjimli@hotmail.com> created
  28. *
  29. */
  30. #include <windows.h>
  31. #include <gdiplus.h>
  32. using namespace Gdiplus;
  33. static ULONG_PTR gdiplusToken;
  34. #include "ftk_image_win32_decoder.h"
  35. static Ret ftk_image_win32_decoder_match(FtkImageDecoder* thiz, const char* filename)
  36. {
  37. return_val_if_fail(filename != NULL, RET_FAIL);
  38. return (strstr(filename, ".png") != NULL
  39. || strstr(filename, ".jpg") != NULL
  40. || strstr(filename, ".jpeg") != NULL) ? RET_OK : RET_FAIL;
  41. }
  42. static FtkBitmap* load_win32 (const char *filename)
  43. {
  44. int x = 0;
  45. int y = 0;
  46. int w = 0;
  47. int h = 0;
  48. FtkColor bg = {0};
  49. FtkBitmap* bitmap = NULL;
  50. WCHAR wfilename[MAX_PATH] = {0};
  51. mbstowcs(wfilename, filename, MAX_PATH);
  52. Bitmap* img = Bitmap::FromFile(wfilename);
  53. return_val_if_fail(img != NULL, NULL);
  54. if(img->GetWidth() == 0 || img->GetHeight() == 0)
  55. {
  56. delete img;
  57. return NULL;
  58. }
  59. w = img->GetWidth();
  60. h = img->GetHeight();
  61. bg.a = 0xff;
  62. bitmap = ftk_bitmap_create(w, h, bg);
  63. Rect r(0, 0, w, h);
  64. BitmapData bitmapData;
  65. img->LockBits(&r, ImageLockModeRead, PixelFormat32bppARGB, &bitmapData);
  66. FtkColor* src = (FtkColor*)bitmapData.Scan0;
  67. FtkColor* dst = ftk_bitmap_lock(bitmap);
  68. for(y = 0; y < h; y++)
  69. {
  70. for(x = 0; x < w; x++)
  71. {
  72. *dst = *src;
  73. dst++;
  74. src++;
  75. }
  76. }
  77. img->UnlockBits(&bitmapData);
  78. delete img;
  79. return bitmap;
  80. }
  81. static FtkBitmap* ftk_image_win32_decoder_decode(FtkImageDecoder* thiz, const char* filename)
  82. {
  83. return_val_if_fail(ftk_image_win32_decoder_match(thiz, filename) == RET_OK, NULL);
  84. return load_win32(filename);
  85. }
  86. static void ftk_image_win32_decoder_destroy(FtkImageDecoder* thiz)
  87. {
  88. if(thiz != NULL)
  89. {
  90. FTK_ZFREE(thiz, sizeof(thiz));
  91. }
  92. GdiplusShutdown(gdiplusToken);
  93. return;
  94. }
  95. FtkImageDecoder* ftk_image_win32_decoder_create(void)
  96. {
  97. FtkImageDecoder* thiz = (FtkImageDecoder*)FTK_ZALLOC(sizeof(FtkImageDecoder));
  98. if(thiz != NULL)
  99. {
  100. GdiplusStartupInput gdiplusStartupInput;
  101. GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
  102. thiz->match = ftk_image_win32_decoder_match;
  103. thiz->decode = ftk_image_win32_decoder_decode;
  104. thiz->destroy = ftk_image_win32_decoder_destroy;
  105. }
  106. return thiz;
  107. }