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.
 
 
 
 
 
 

162 lines
3.8 KiB

  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_get_pixels(img, PIPI_PIXELS_RGBA_U8);
  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. ULONG_PTR token;
  68. Gdiplus::GdiplusStartupInput input;
  69. Gdiplus::GdiplusStartup(&token, &input, NULL);
  70. wchar_t const *fmt;
  71. if(strstr(name, ".gif"))
  72. fmt = L"image/gif";
  73. else if(strstr(name, ".jpeg") || strstr(name, ".jpeg"))
  74. fmt = L"image/jpeg";
  75. else if(strstr(name, ".png"))
  76. fmt = L"image/png";
  77. else if(strstr(name, ".tiff"))
  78. fmt = L"image/tiff";
  79. else /* if(strstr(name, ".bmp")) */
  80. fmt = L"image/bmp";
  81. unsigned int num = 0, size = 0;
  82. Gdiplus::GetImageEncodersSize(&num, &size);
  83. if(size == 0)
  84. return -1;
  85. Gdiplus::ImageCodecInfo *codecs
  86. = (Gdiplus::ImageCodecInfo *)(malloc(size));
  87. if(!codecs)
  88. return -1;
  89. Gdiplus::GetImageEncoders(num, size, codecs);
  90. CLSID clsid;
  91. for(unsigned int i = 0; i < num; i++)
  92. {
  93. if(wcscmp(codecs[i].MimeType, fmt) == 0)
  94. {
  95. clsid = codecs[i].Clsid;
  96. break;
  97. }
  98. }
  99. size_t len;
  100. len = mbstowcs(NULL, name, 0);
  101. wchar_t *wname = new wchar_t[len + 1];
  102. if(mbstowcs(wname, name, len + 1) == (size_t)-1)
  103. {
  104. delete[] wname;
  105. return -1;
  106. }
  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_get_pixels(img, PIPI_PIXELS_RGBA_U8);
  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. */