You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * libpipi Pathetic image processing interface library
  3. * Copyright (c) 2004-2008 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This library is free software. It comes without any warranty, to
  9. * the extent permitted by applicable law. You can redistribute it
  10. * and/or modify it under the terms of the Do What The Fuck You Want
  11. * To Public License, Version 2, as published by Sam Hocevar. See
  12. * http://sam.zoy.org/wtfpl/COPYING for more details.
  13. */
  14. /*
  15. * gdi.c: Windows GDI I/O functions (BMP only)
  16. */
  17. #include "config.h"
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <windows.h>
  22. #include <Gdiplus.h>
  23. extern "C" {
  24. #include "pipi.h"
  25. #include "pipi_internals.h"
  26. }
  27. extern "C" pipi_image_t *pipi_load_gdiplus(const char *name)
  28. {
  29. size_t len;
  30. len = mbstowcs(NULL, name, 0);
  31. wchar_t *wname = new wchar_t[len + 1];
  32. if(mbstowcs(wname, name, len + 1) == (size_t)-1)
  33. {
  34. delete[] wname;
  35. return NULL;
  36. }
  37. ULONG_PTR token;
  38. Gdiplus::GdiplusStartupInput input;
  39. Gdiplus::GdiplusStartup(&token, &input, NULL);
  40. Gdiplus::Bitmap *b = Gdiplus::Bitmap::FromFile(wname, 0);
  41. if(!b)
  42. {
  43. delete[] wname;
  44. return NULL;
  45. }
  46. delete[] wname;
  47. Gdiplus::BitmapData bdata;
  48. Gdiplus::Rect rect(0, 0, b->GetWidth(), b->GetHeight());
  49. if(b->LockBits(&rect, Gdiplus::ImageLockModeRead,
  50. PixelFormat32bppARGB, &bdata) != Gdiplus::Ok)
  51. {
  52. delete b;
  53. return NULL;
  54. }
  55. pipi_image_t *img = pipi_new(b->GetWidth(), b->GetHeight());
  56. pipi_pixels_t *p = pipi_getpixels(img, PIPI_PIXELS_RGBA_C);
  57. memcpy(p->pixels, bdata.Scan0, bdata.Width * bdata.Height * 4);
  58. b->UnlockBits(&bdata);
  59. delete b;
  60. img->codec_priv = NULL;
  61. img->wrap = 0;
  62. img->u8 = 1;
  63. return img;
  64. }
  65. extern "C" int pipi_save_gdiplus(pipi_image_t *img, const char *name)
  66. {
  67. wchar_t const *fmt;
  68. if(strstr(name, ".gif"))
  69. fmt = L"image/gif";
  70. else if(strstr(name, ".jpeg") || strstr(name, ".jpeg"))
  71. fmt = L"image/jpeg";
  72. else if(strstr(name, ".png"))
  73. fmt = L"image/png";
  74. else if(strstr(name, ".tiff"))
  75. fmt = L"image/tiff";
  76. else /* if(strstr(name, ".bmp")) */
  77. fmt = L"image/bmp";
  78. unsigned int num = 0, size = 0;
  79. Gdiplus::GetImageEncodersSize(&num, &size);
  80. if(size == 0)
  81. return -1;
  82. Gdiplus::ImageCodecInfo *codecs
  83. = (Gdiplus::ImageCodecInfo *)(malloc(size));
  84. if(!codecs)
  85. return -1;
  86. Gdiplus::GetImageEncoders(num, size, codecs);
  87. CLSID clsid;
  88. for(unsigned int i = 0; i < num; i++)
  89. {
  90. if(wcscmp(codecs[i].MimeType, fmt) == 0)
  91. {
  92. clsid = codecs[i].Clsid;
  93. break;
  94. }
  95. }
  96. size_t len;
  97. len = mbstowcs(NULL, name, 0);
  98. wchar_t *wname = new wchar_t[len + 1];
  99. if(mbstowcs(wname, name, len + 1) == (size_t)-1)
  100. {
  101. delete[] wname;
  102. return -1;
  103. }
  104. ULONG_PTR token;
  105. Gdiplus::GdiplusStartupInput input;
  106. Gdiplus::GdiplusStartup(&token, &input, NULL);
  107. Gdiplus::Bitmap *b = new Gdiplus::Bitmap(img->w, img->h,
  108. PixelFormat32bppARGB);
  109. Gdiplus::BitmapData bdata;
  110. Gdiplus::Rect rect(0, 0, img->w, img->h);
  111. if(b->LockBits(&rect, (unsigned int)Gdiplus::ImageLockModeWrite,
  112. PixelFormat32bppARGB, &bdata) != Gdiplus::Ok)
  113. {
  114. delete b;
  115. return -1;
  116. }
  117. pipi_pixels_t *p = pipi_getpixels(img, PIPI_PIXELS_RGBA_C);
  118. memcpy(bdata.Scan0, p->pixels, bdata.Width * bdata.Height * 4);
  119. b->UnlockBits(&bdata);
  120. if(b->Save(wname, &clsid, NULL) != Gdiplus::Ok)
  121. {
  122. delete[] wname;
  123. delete b;
  124. return -1;
  125. }
  126. delete[] wname;
  127. delete b;
  128. return 0;
  129. }
  130. /*
  131. * XXX: The following functions are local.
  132. */