source: libpipi/trunk/pipi/codec/gdiplus.cpp @ 3067

Last change on this file since 3067 was 3067, checked in by Sam Hocevar, 13 years ago

libpipi: duplicate the GDI codec in order to prepare the GDI+ one.

  • Property svn:executable set to *
File size: 3.9 KB
Line 
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
30extern "C" pipi_image_t *pipi_load_gdiplus(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_getpixels(img, PIPI_PIXELS_RGBA_C);
57    data = (uint8_t *)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    img->codec_priv = NULL;
74
75    img->wrap = 0;
76    img->u8 = 1;
77
78    ReleaseDC(0, hdc);
79    DeleteObject(hbmap);
80
81    return img;
82}
83
84extern "C" int pipi_save_gdiplus(pipi_image_t *img, const char *name)
85{
86    BITMAPINFOHEADER binfo;
87    BITMAPFILEHEADER bfheader;
88    uint8_t dummy[4] = { 0 };
89    HANDLE hfile;
90    pipi_pixels_t *p;
91    DWORD ret = 0;
92    uint8_t *data;
93    int x, y, padding;
94
95    p = pipi_getpixels(img, PIPI_PIXELS_RGBA_C);
96    data = (uint8_t *)p->pixels;
97
98    padding = ((img->w * 3) + 3) / 4 * 4 - img->w * 3;
99
100    memset(&binfo, 0, sizeof(binfo));
101    binfo.biSize = sizeof(binfo);
102    binfo.biWidth = img->w;
103    binfo.biHeight = img->h;
104    binfo.biPlanes = 1;
105    binfo.biBitCount = 24;
106    binfo.biCompression = BI_RGB;
107    binfo.biSizeImage = (img->w * 3 + padding) * img->h;
108
109    memset(&bfheader, 0, sizeof(bfheader));
110    bfheader.bfType = 0x4D42;
111    bfheader.bfOffBits = sizeof(bfheader) + sizeof(binfo);
112    bfheader.bfSize = bfheader.bfOffBits + binfo.biSizeImage;
113
114    /* We don’t even create the bitmap object, since we know exactly
115     * what kind of file we are saving. But later, when we support
116     * different depths and BMP options, we'll need to care about it. */
117    hfile = CreateFile(name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
118                       FILE_ATTRIBUTE_ARCHIVE, NULL);
119    if(!hfile)
120        return -1;
121    WriteFile(hfile, &bfheader, sizeof(bfheader), &ret, NULL);
122    WriteFile(hfile, &binfo, sizeof(binfo), &ret, NULL);
123    for(y = 0; y < img->h; y++)
124    {
125        for(x = 0; x < img->w; x++)
126        {
127            uint8_t tmp[3];
128            tmp[0] = data[4 * (img->w * (img->h - 1 - y) + x) + 0];
129            tmp[1] = data[4 * (img->w * (img->h - 1 - y) + x) + 1];
130            tmp[2] = data[4 * (img->w * (img->h - 1 - y) + x) + 2];
131            WriteFile(hfile, tmp, 3, &ret, NULL);
132        }
133        if(padding)
134            WriteFile(hfile, dummy, padding, &ret, NULL);
135    }
136    CloseHandle(hfile);
137
138    return 0;
139}
140
141/*
142 * XXX: The following functions are local.
143 */
144
Note: See TracBrowser for help on using the repository browser.