Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

162 linhas
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_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_get_pixels(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. */