Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

165 righe
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. #if 0
  30. size_t len;
  31. len = mbstowcs(NULL, name, 0);
  32. wchar_t *wname = new wchar_t[len + 1];
  33. if(mbstowcs(wname, name, len + 1) != (size_t)-1)
  34. {
  35. delete[] wname;
  36. return NULL;
  37. }
  38. ULONG_PTR token;
  39. Gdiplus::GdiplusStartupInput input;
  40. Gdiplus::GdiplusStartup(&token, &input, NULL);
  41. Gdiplus::Bitmap *b = Gdiplus::Bitmap::FromFile(wname, 0);
  42. if(!b)
  43. {
  44. delete[] wname;
  45. return NULL;
  46. }
  47. delete[] wname;
  48. Gdiplus::BitmapData bdata;
  49. Gdiplus::Rect rect(0, 0, b->GetWidth(), b->GetHeight());
  50. if(b->LockBits(rect, Gdiplus::ImageLockModeRead,
  51. PixelFormat32bppARGB, &bdata) != Gdiplus::Ok)
  52. {
  53. delete b;
  54. return NULL;
  55. }
  56. pipi_image_t *img = pipi_new(b->GetWidth(), b->GetHeight());
  57. pipi_pixels_t *p = pipi_getpixels(img, PIPI_PIXELS_RGBA_C);
  58. memcpy(p->pixels, bdata.Scan0, bdata.Width * bdata.Height * 4);
  59. b->UnlockBits(&bdata);
  60. delete b;
  61. img->codec_priv = NULL;
  62. img->wrap = 0;
  63. img->u8 = 1;
  64. return img;
  65. #endif
  66. return NULL;
  67. }
  68. extern "C" int pipi_save_gdiplus(pipi_image_t *img, const char *name)
  69. {
  70. #if 0
  71. wchar_t const *fmt;
  72. if(strstr(name, ".gif"))
  73. fmt = L"image/gif";
  74. else if(strstr(name, ".jpeg") || strstr(name, ".jpeg"))
  75. fmt = L"image/jpeg";
  76. else if(strstr(name, ".png"))
  77. fmt = L"image/png";
  78. else if(strstr(name, ".tiff"))
  79. fmt = L"image/tiff";
  80. unsigned int num = 0, size = 0;
  81. Gdiplus::GetImageEncodersSize(&num, &size);
  82. if(size == 0)
  83. return -1;
  84. Gdiplus::ImageCodecInfo *codecs
  85. = (Gdiplus::ImageCodecInfo *)(malloc(size));
  86. if(!codecs)
  87. return -1;
  88. Gdiplus::GetImageEncoders(num, size, codecs);
  89. CLSID clsid;
  90. for(unsigned int i = 0; i < num; i++)
  91. {
  92. if(wcscmp(codecs[i].MimeType, fmt) == 0)
  93. {
  94. clsid = codecs[i].Clsid;
  95. break;
  96. }
  97. }
  98. size_t len;
  99. len = mbstowcs(NULL, name, 0);
  100. wchar_t *wname = new wchar_t[len + 1];
  101. if(mbstowcs(wname, name, len + 1) != (size_t)-1)
  102. {
  103. delete[] wname;
  104. return -1;
  105. }
  106. ULONG_PTR token;
  107. Gdiplus::GdiplusStartupInput input;
  108. Gdiplus::GdiplusStartup(&token, &input, NULL);
  109. Gdiplus::Bitmap *b = new Gdiplus::Bitmap(img->w, img->h,
  110. PixelFormat32bppARGB);
  111. Gdiplus::BitmapData bdata;
  112. Gdiplus::Rect rect(0, 0, img->w, img->h);
  113. if(b->LockBits(rect, (unsigned int)Gdiplus::ImageLockModeWrite,
  114. PixelFormat32bppARGB, &bdata) != Gdiplus::Ok)
  115. {
  116. delete b;
  117. return -1;
  118. }
  119. pipi_pixels_t *p = pipi_getpixels(img, PIPI_PIXELS_RGBA_C);
  120. memcpy(bdata.Scan0, p->pixels, bdata.Width * bdata.Height * 4);
  121. b->UnlockBits(&bdata);
  122. if(b->Save(wname, &clsid, NULL) != Gdiplus::Ok)
  123. {
  124. delete[] wname;
  125. delete b;
  126. return -1;
  127. }
  128. delete[] wname;
  129. delete b;
  130. #endif
  131. return 0;
  132. }
  133. /*
  134. * XXX: The following functions are local.
  135. */