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.
 
 
 
 
 
 

149 regels
3.9 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. hbmap = (HBITMAP)LoadImage(NULL, name, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
  33. if(!hbmap)
  34. return NULL;
  35. hdc = CreateCompatibleDC(NULL);
  36. SelectObject(hdc, hbmap);
  37. memset(&binfo, 0, sizeof(binfo));
  38. binfo.bmiHeader.biSize = sizeof(binfo.bmiHeader);
  39. if(!GetDIBits(hdc, hbmap, 0, 0, 0, &binfo, DIB_RGB_COLORS))
  40. {
  41. ReleaseDC(0, hdc);
  42. DeleteObject(hbmap);
  43. return NULL;
  44. }
  45. img = pipi_new(binfo.bmiHeader.biWidth, binfo.bmiHeader.biHeight);
  46. p = pipi_get_pixels(img, PIPI_PIXELS_RGBA_C);
  47. data = p->pixels;
  48. binfo.bmiHeader.biBitCount = 32;
  49. binfo.bmiHeader.biCompression = BI_RGB;
  50. binfo.bmiHeader.biHeight = - abs(binfo.bmiHeader.biHeight);
  51. if(!GetDIBits(hdc, hbmap, 0, abs(binfo.bmiHeader.biHeight), data,
  52. &binfo, DIB_RGB_COLORS))
  53. {
  54. ReleaseDC(0, hdc);
  55. DeleteObject(hbmap);
  56. return NULL;
  57. }
  58. /* FIXME: do we need to swap bytes? Apparently Vista doesn't need it,
  59. * but we'd need a more thorough test. */
  60. pipi_release_pixels(img, p);
  61. img->codec_priv = NULL;
  62. img->wrap = 0;
  63. img->u8 = 1;
  64. ReleaseDC(0, hdc);
  65. DeleteObject(hbmap);
  66. return img;
  67. }
  68. int pipi_save_gdi(pipi_image_t *img, const char *name)
  69. {
  70. BITMAPINFOHEADER binfo;
  71. BITMAPFILEHEADER bfheader;
  72. uint8_t dummy[4] = { 0 };
  73. HANDLE hfile;
  74. pipi_pixels_t *p;
  75. DWORD ret = 0;
  76. uint8_t *data;
  77. int x, y, padding;
  78. p = pipi_get_pixels(img, PIPI_PIXELS_RGBA_C);
  79. data = p->pixels;
  80. padding = ((img->w * 3) + 3) / 4 * 4 - img->w * 3;
  81. memset(&binfo, 0, sizeof(binfo));
  82. binfo.biSize = sizeof(binfo);
  83. binfo.biWidth = img->w;
  84. binfo.biHeight = img->h;
  85. binfo.biPlanes = 1;
  86. binfo.biBitCount = 24;
  87. binfo.biCompression = BI_RGB;
  88. binfo.biSizeImage = (img->w * 3 + padding) * img->h;
  89. memset(&bfheader, 0, sizeof(bfheader));
  90. bfheader.bfType = 0x4D42;
  91. bfheader.bfOffBits = sizeof(bfheader) + sizeof(binfo);
  92. bfheader.bfSize = bfheader.bfOffBits + binfo.biSizeImage;
  93. /* We don’t even create the bitmap object, since we know exactly
  94. * what kind of file we are saving. But later, when we support
  95. * different depths and BMP options, we'll need to care about it. */
  96. hfile = CreateFile(name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
  97. FILE_ATTRIBUTE_ARCHIVE, NULL);
  98. if(!hfile)
  99. return -1;
  100. WriteFile(hfile, &bfheader, sizeof(bfheader), &ret, NULL);
  101. WriteFile(hfile, &binfo, sizeof(binfo), &ret, NULL);
  102. for(y = 0; y < img->h; y++)
  103. {
  104. for(x = 0; x < img->w; x++)
  105. {
  106. uint8_t tmp[3];
  107. tmp[0] = data[4 * (img->w * (img->h - 1 - y) + x) + 0];
  108. tmp[1] = data[4 * (img->w * (img->h - 1 - y) + x) + 1];
  109. tmp[2] = data[4 * (img->w * (img->h - 1 - y) + x) + 2];
  110. WriteFile(hfile, tmp, 3, &ret, NULL);
  111. }
  112. if(padding)
  113. WriteFile(hfile, dummy, padding, &ret, NULL);
  114. }
  115. CloseHandle(hfile);
  116. pipi_release_pixels(img, p);
  117. return 0;
  118. }
  119. /*
  120. * XXX: The following functions are local.
  121. */