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 | /* |
---|
16 | * gdi.c: Windows GDI I/O functions (BMP only) |
---|
17 | */ |
---|
18 | |
---|
19 | #include "config.h" |
---|
20 | |
---|
21 | #include <stdio.h> |
---|
22 | #include <stdlib.h> |
---|
23 | #include <string.h> |
---|
24 | |
---|
25 | #include <windows.h> |
---|
26 | |
---|
27 | #include "pipi.h" |
---|
28 | #include "pipi_internals.h" |
---|
29 | |
---|
30 | pipi_image_t *pipi_load_gdi(const char *name) |
---|
31 | { |
---|
32 | BITMAPINFO binfo; |
---|
33 | pipi_image_t *img; |
---|
34 | pipi_pixels_t *p; |
---|
35 | uint8_t *data; |
---|
36 | HBITMAP hbmap; |
---|
37 | HDC hdc; |
---|
38 | |
---|
39 | hbmap = (HBITMAP)LoadImage(NULL, name, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE); |
---|
40 | if(!hbmap) |
---|
41 | return NULL; |
---|
42 | |
---|
43 | hdc = CreateCompatibleDC(NULL); |
---|
44 | SelectObject(hdc, hbmap); |
---|
45 | |
---|
46 | memset(&binfo, 0, sizeof(binfo)); |
---|
47 | binfo.bmiHeader.biSize = sizeof(binfo.bmiHeader); |
---|
48 | if(!GetDIBits(hdc, hbmap, 0, 0, 0, &binfo, DIB_RGB_COLORS)) |
---|
49 | { |
---|
50 | ReleaseDC(0, hdc); |
---|
51 | DeleteObject(hbmap); |
---|
52 | return NULL; |
---|
53 | } |
---|
54 | |
---|
55 | img = pipi_new(binfo.bmiHeader.biWidth, binfo.bmiHeader.biHeight); |
---|
56 | p = pipi_get_pixels(img, PIPI_PIXELS_RGBA_C); |
---|
57 | data = p->pixels; |
---|
58 | |
---|
59 | binfo.bmiHeader.biBitCount = 32; |
---|
60 | binfo.bmiHeader.biCompression = BI_RGB; |
---|
61 | binfo.bmiHeader.biHeight = - abs(binfo.bmiHeader.biHeight); |
---|
62 | if(!GetDIBits(hdc, hbmap, 0, abs(binfo.bmiHeader.biHeight), data, |
---|
63 | &binfo, DIB_RGB_COLORS)) |
---|
64 | { |
---|
65 | ReleaseDC(0, hdc); |
---|
66 | DeleteObject(hbmap); |
---|
67 | return NULL; |
---|
68 | } |
---|
69 | |
---|
70 | /* FIXME: do we need to swap bytes? Apparently Vista doesn't need it, |
---|
71 | * but we'd need a more thorough test. */ |
---|
72 | |
---|
73 | pipi_release_pixels(img, p); |
---|
74 | |
---|
75 | img->codec_priv = NULL; |
---|
76 | |
---|
77 | img->wrap = 0; |
---|
78 | img->u8 = 1; |
---|
79 | |
---|
80 | ReleaseDC(0, hdc); |
---|
81 | DeleteObject(hbmap); |
---|
82 | |
---|
83 | return img; |
---|
84 | } |
---|
85 | |
---|
86 | int pipi_save_gdi(pipi_image_t *img, const char *name) |
---|
87 | { |
---|
88 | BITMAPINFOHEADER binfo; |
---|
89 | BITMAPFILEHEADER bfheader; |
---|
90 | uint8_t dummy[4] = { 0 }; |
---|
91 | HANDLE hfile; |
---|
92 | pipi_pixels_t *p; |
---|
93 | DWORD ret = 0; |
---|
94 | uint8_t *data; |
---|
95 | int x, y, padding; |
---|
96 | |
---|
97 | p = pipi_get_pixels(img, PIPI_PIXELS_RGBA_C); |
---|
98 | data = p->pixels; |
---|
99 | |
---|
100 | padding = ((img->w * 3) + 3) / 4 * 4 - img->w * 3; |
---|
101 | |
---|
102 | memset(&binfo, 0, sizeof(binfo)); |
---|
103 | binfo.biSize = sizeof(binfo); |
---|
104 | binfo.biWidth = img->w; |
---|
105 | binfo.biHeight = img->h; |
---|
106 | binfo.biPlanes = 1; |
---|
107 | binfo.biBitCount = 24; |
---|
108 | binfo.biCompression = BI_RGB; |
---|
109 | binfo.biSizeImage = (img->w * 3 + padding) * img->h; |
---|
110 | |
---|
111 | memset(&bfheader, 0, sizeof(bfheader)); |
---|
112 | bfheader.bfType = 0x4D42; |
---|
113 | bfheader.bfOffBits = sizeof(bfheader) + sizeof(binfo); |
---|
114 | bfheader.bfSize = bfheader.bfOffBits + binfo.biSizeImage; |
---|
115 | |
---|
116 | /* We don’t even create the bitmap object, since we know exactly |
---|
117 | * what kind of file we are saving. But later, when we support |
---|
118 | * different depths and BMP options, we'll need to care about it. */ |
---|
119 | hfile = CreateFile(name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, |
---|
120 | FILE_ATTRIBUTE_ARCHIVE, NULL); |
---|
121 | if(!hfile) |
---|
122 | return -1; |
---|
123 | WriteFile(hfile, &bfheader, sizeof(bfheader), &ret, NULL); |
---|
124 | WriteFile(hfile, &binfo, sizeof(binfo), &ret, NULL); |
---|
125 | for(y = 0; y < img->h; y++) |
---|
126 | { |
---|
127 | for(x = 0; x < img->w; x++) |
---|
128 | { |
---|
129 | uint8_t tmp[3]; |
---|
130 | tmp[0] = data[4 * (img->w * (img->h - 1 - y) + x) + 0]; |
---|
131 | tmp[1] = data[4 * (img->w * (img->h - 1 - y) + x) + 1]; |
---|
132 | tmp[2] = data[4 * (img->w * (img->h - 1 - y) + x) + 2]; |
---|
133 | WriteFile(hfile, tmp, 3, &ret, NULL); |
---|
134 | } |
---|
135 | if(padding) |
---|
136 | WriteFile(hfile, dummy, padding, &ret, NULL); |
---|
137 | } |
---|
138 | CloseHandle(hfile); |
---|
139 | |
---|
140 | pipi_release_pixels(img, p); |
---|
141 | |
---|
142 | return 0; |
---|
143 | } |
---|
144 | |
---|
145 | /* |
---|
146 | * XXX: The following functions are local. |
---|
147 | */ |
---|
148 | |
---|