source: libpipi/trunk/pipi/codec/gdi.c @ 2840

Last change on this file since 2840 was 2840, checked in by Sam Hocevar, 12 years ago

gdi.c: the GDI codec can now open and save BMP files.

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