/src/os/windows/ftk_image_win32_decoder.cpp

http://ftk.googlecode.com/ · C++ · 140 lines · 86 code · 25 blank · 29 comment · 18 complexity · 6c9b10518503fc523c3964cbd07a204a 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. #ifdef VC6
  31. #define ULONG_PTR unsigned long*
  32. #endif
  33. #include <windows.h>
  34. #include <gdiplus.h>
  35. #include "ftk_win32.h"
  36. using namespace Gdiplus;
  37. static ULONG_PTR gdiplusToken;
  38. #include "ftk_image_win32_decoder.h"
  39. static Ret ftk_image_win32_decoder_match(FtkImageDecoder* thiz, const char* filename)
  40. {
  41. return_val_if_fail(filename != NULL, RET_FAIL);
  42. return (strstr(filename, ".png") != NULL
  43. || strstr(filename, ".jpg") != NULL
  44. || strstr(filename, ".jpeg") != NULL) ? RET_OK : RET_FAIL;
  45. }
  46. static FtkBitmap* load_win32 (const char *filename)
  47. {
  48. int x = 0;
  49. int y = 0;
  50. int w = 0;
  51. int h = 0;
  52. FtkColor bg = {0};
  53. FtkBitmap* bitmap = NULL;
  54. WCHAR wfilename[MAX_PATH] = {0};
  55. mbstowcs(wfilename, filename, MAX_PATH);
  56. Bitmap* img = Bitmap::FromFile(wfilename);
  57. return_val_if_fail(img != NULL, NULL);
  58. if(img->GetWidth() == 0 || img->GetHeight() == 0)
  59. {
  60. delete img;
  61. return NULL;
  62. }
  63. w = img->GetWidth();
  64. h = img->GetHeight();
  65. bg.a = 0xff;
  66. bitmap = ftk_bitmap_create(w, h, bg);
  67. Rect r(0, 0, w, h);
  68. BitmapData bitmapData;
  69. #ifdef VC6
  70. img->LockBits(r, ImageLockModeRead, PixelFormat32bppARGB, &bitmapData);
  71. #else
  72. img->LockBits(&r, ImageLockModeRead, PixelFormat32bppARGB, &bitmapData);
  73. #endif
  74. FtkColor* src = (FtkColor*)bitmapData.Scan0;
  75. FtkColor* dst = ftk_bitmap_lock(bitmap);
  76. for(y = 0; y < h; y++)
  77. {
  78. for(x = 0; x < w; x++)
  79. {
  80. *dst = *src;
  81. dst++;
  82. src++;
  83. }
  84. }
  85. img->UnlockBits(&bitmapData);
  86. delete img;
  87. return bitmap;
  88. }
  89. static FtkBitmap* ftk_image_win32_decoder_decode(FtkImageDecoder* thiz, const char* filename)
  90. {
  91. return_val_if_fail(ftk_image_win32_decoder_match(thiz, filename) == RET_OK, NULL);
  92. return load_win32(filename);
  93. }
  94. static void ftk_image_win32_decoder_destroy(FtkImageDecoder* thiz)
  95. {
  96. if(thiz != NULL)
  97. {
  98. FTK_ZFREE(thiz, sizeof(thiz));
  99. }
  100. GdiplusShutdown(gdiplusToken);
  101. return;
  102. }
  103. FtkImageDecoder* ftk_image_win32_decoder_create(void)
  104. {
  105. FtkImageDecoder* thiz = (FtkImageDecoder*)FTK_ZALLOC(sizeof(FtkImageDecoder));
  106. if(thiz != NULL)
  107. {
  108. GdiplusStartupInput gdiplusStartupInput;
  109. GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
  110. thiz->match = ftk_image_win32_decoder_match;
  111. thiz->decode = ftk_image_win32_decoder_decode;
  112. thiz->destroy = ftk_image_win32_decoder_destroy;
  113. }
  114. return thiz;
  115. }