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.
 
 
 
 
 
 

153 linhas
4.1 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 "pipi.h"
  23. #include "pipi_internals.h"
  24. pipi_image_t *pipi_load_gdi(const char *name)
  25. {
  26. BITMAPINFO binfo;
  27. pipi_image_t *img;
  28. pipi_pixels_t *p;
  29. uint8_t *data;
  30. HBITMAP hbmap;
  31. HDC hdc;
  32. int x, y;
  33. hbmap = (HBITMAP)LoadImage(NULL, name, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
  34. if(!hbmap)
  35. return NULL;
  36. hdc = CreateCompatibleDC(NULL);
  37. SelectObject(hdc, hbmap);
  38. memset(&binfo, 0, sizeof(binfo));
  39. binfo.bmiHeader.biSize = sizeof(binfo.bmiHeader);
  40. if(!GetDIBits(hdc, hbmap, 0, 0, 0, &binfo, DIB_RGB_COLORS))
  41. {
  42. ReleaseDC(0, hdc);
  43. DeleteObject(hbmap);
  44. return NULL;
  45. }
  46. img = pipi_new(binfo.bmiHeader.biWidth, binfo.bmiHeader.biHeight);
  47. p = pipi_getpixels(img, PIPI_PIXELS_RGBA_C);
  48. data = p->pixels;
  49. binfo.bmiHeader.biBitCount = 32;
  50. binfo.bmiHeader.biCompression = BI_RGB;
  51. binfo.bmiHeader.biHeight = - abs(binfo.bmiHeader.biHeight);
  52. if(!GetDIBits(hdc, hbmap, 0, abs(binfo.bmiHeader.biHeight), data,
  53. &binfo, DIB_RGB_COLORS))
  54. {
  55. ReleaseDC(0, hdc);
  56. DeleteObject(hbmap);
  57. return NULL;
  58. }
  59. /* FIXME: get rid of this loop, maybe by using BITMAPV4HEADER above
  60. * so that GDI reorders the pixels for us. */
  61. for(y = 0; y < img->h; y++)
  62. for(x = 0; x < img->w; x++)
  63. {
  64. /* Swap B and R, don't touch G and A. */
  65. uint8_t tmp = data[0]; data[0] = data[2]; data[2] = tmp;
  66. data += 4;
  67. }
  68. img->codec_priv = NULL;
  69. img->wrap = 0;
  70. img->u8 = 1;
  71. ReleaseDC(0, hdc);
  72. DeleteObject(hbmap);
  73. return img;
  74. }
  75. int pipi_save_gdi(pipi_image_t *img, const char *name)
  76. {
  77. BITMAPINFOHEADER binfo;
  78. BITMAPFILEHEADER bfheader;
  79. uint8_t dummy[4] = { 0 };
  80. HANDLE hfile;
  81. pipi_pixels_t *p;
  82. DWORD ret = 0;
  83. uint8_t *data;
  84. int x, y, padding;
  85. p = pipi_getpixels(img, PIPI_PIXELS_RGBA_C);
  86. data = p->pixels;
  87. padding = ((img->w * 3) + 3) / 4 * 4 - img->w * 3;
  88. memset(&binfo, 0, sizeof(binfo));
  89. binfo.biSize = sizeof(binfo);
  90. binfo.biWidth = img->w;
  91. binfo.biHeight = img->h;
  92. binfo.biPlanes = 1;
  93. binfo.biBitCount = 24;
  94. binfo.biCompression = BI_RGB;
  95. binfo.biSizeImage = (img->w * 3 + padding) * img->h;
  96. memset(&bfheader, 0, sizeof(bfheader));
  97. bfheader.bfType = 0x4D42;
  98. bfheader.bfOffBits = sizeof(bfheader) + sizeof(binfo);
  99. bfheader.bfSize = bfheader.bfOffBits + binfo.biSizeImage;
  100. /* We don’t even create the bitmap object, since we know exactly
  101. * what kind of file we are saving. But later, when we support
  102. * different depths and BMP options, we'll need to care about it. */
  103. hfile = CreateFile(name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
  104. FILE_ATTRIBUTE_ARCHIVE, NULL);
  105. if(!hfile)
  106. return -1;
  107. WriteFile(hfile, &bfheader, sizeof(bfheader), &ret, NULL);
  108. WriteFile(hfile, &binfo, sizeof(binfo), &ret, NULL);
  109. for(y = 0; y < img->h; y++)
  110. {
  111. for(x = 0; x < img->w; x++)
  112. {
  113. uint8_t tmp[3];
  114. tmp[0] = data[4 * (img->w * (img->h - 1 - y) + x) + 2];
  115. tmp[1] = data[4 * (img->w * (img->h - 1 - y) + x) + 1];
  116. tmp[2] = data[4 * (img->w * (img->h - 1 - y) + x) + 0];
  117. WriteFile(hfile, tmp, 3, &ret, NULL);
  118. }
  119. if(padding)
  120. WriteFile(hfile, dummy, padding, &ret, NULL);
  121. }
  122. CloseHandle(hfile);
  123. return 0;
  124. }
  125. /*
  126. * XXX: The following functions are local.
  127. */