source: libcaca/trunk/src/common-image.c @ 769

Last change on this file since 769 was 769, checked in by Sam Hocevar, 14 years ago
  • Removed \file directives from all files except caca.h and cucul.h, to remove redundencies in the Doxygen documentation.
  • Property svn:keywords set to Id
File size: 6.8 KB
Line 
1/*
2 *  Imaging tools for cacaview and img2irc
3 *  Copyright (c) 2003-2006 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id: common-image.c 769 2006-04-14 07:30:53Z sam $
7 *
8 *  This program is free software; you can redistribute it and/or
9 *  modify it under the terms of the Do What The Fuck You Want To
10 *  Public License, Version 2, as published by Sam Hocevar. See
11 *  http://sam.zoy.org/wtfpl/COPYING for more details.
12 */
13
14#include "config.h"
15
16#include <stdio.h>
17#include <string.h>
18#include <stdlib.h>
19
20#if defined(HAVE_IMLIB2_H)
21#   include <Imlib2.h>
22#else
23#   include <stdio.h>
24#endif
25
26#include "cucul.h"
27#include "common-image.h"
28
29#if !defined(HAVE_IMLIB2_H)
30static unsigned int u32fread(FILE *);
31static unsigned int u16fread(FILE *);
32static unsigned int u8fread(FILE *);
33#endif
34
35struct image * load_image(char const * name)
36{
37    struct image * im = malloc(sizeof(struct image));
38    unsigned int depth, bpp, rmask, gmask, bmask, amask;
39
40#if defined(HAVE_IMLIB2_H)
41    Imlib_Image image;
42
43    /* Load the new image */
44    image = imlib_load_image(name);
45
46    if(!image)
47    {
48        free(im);
49        return NULL;
50    }
51
52    imlib_context_set_image(image);
53    im->pixels = (char *)imlib_image_get_data_for_reading_only();
54    im->w = imlib_image_get_width();
55    im->h = imlib_image_get_height();
56    rmask = 0x00ff0000;
57    gmask = 0x0000ff00;
58    bmask = 0x000000ff;
59    amask = 0xff000000;
60    bpp = 32;
61    depth = 4;
62
63    /* Create the libcucul dither */
64    im->dither = cucul_create_dither(bpp, im->w, im->h, depth * im->w,
65                                     rmask, gmask, bmask, amask);
66    if(!im->dither)
67    {
68        imlib_free_image();
69        free(im);
70        return NULL;
71    }
72
73    im->priv = (void *)image;
74
75#else
76    /* Try to load a BMP file */
77    unsigned int red[256], green[256], blue[256], alpha[256];
78    unsigned int i, colors, offset, tmp, planes;
79    FILE *fp;
80
81    fp = fopen(name, "rb");
82    if(!fp)
83    {
84        free(im);
85        return NULL;
86    }
87
88    if(u16fread(fp) != 0x4d42)
89    {
90        fclose(fp);
91        free(im);
92        return NULL;
93    }
94
95    u32fread(fp); /* size */
96    u16fread(fp); /* reserved 1 */
97    u16fread(fp); /* reserved 2 */
98
99    offset = u32fread(fp);
100
101    tmp = u32fread(fp); /* header size */
102    if(tmp == 40)
103    {
104        im->w = u32fread(fp);
105        im->h = u32fread(fp);
106        planes = u16fread(fp);
107        bpp = u16fread(fp);
108
109        tmp = u32fread(fp); /* compression */
110        if(tmp != 0)
111        {
112            fclose(fp);
113            free(im);
114            return NULL;
115        }
116
117        u32fread(fp); /* sizeimage */
118        u32fread(fp); /* xpelspermeter */
119        u32fread(fp); /* ypelspermeter */
120        u32fread(fp); /* biclrused */
121        u32fread(fp); /* biclrimportantn */
122
123        colors = (offset - 54) / 4;
124        for(i = 0; i < colors && i < 256; i++)
125        {
126            blue[i] = u8fread(fp) * 16;
127            green[i] = u8fread(fp) * 16;
128            red[i] = u8fread(fp) * 16;
129            alpha[i] = 0;
130            u8fread(fp);
131        }
132    }
133    else if(tmp == 12)
134    {
135        im->w = u32fread(fp);
136        im->h = u32fread(fp);
137        planes = u16fread(fp);
138        bpp = u16fread(fp);
139
140        colors = (offset - 26) / 3;
141        for(i = 0; i < colors && i < 256; i++)
142        {
143            blue[i] = u8fread(fp);
144            green[i] = u8fread(fp);
145            red[i] = u8fread(fp);
146            alpha[i] = 0;
147        }
148    }
149    else
150    {
151        fclose(fp);
152        free(im);
153        return NULL;
154    }
155
156    /* Fill the rest of the palette */
157    for(i = colors; i < 256; i++)
158        blue[i] = green[i] = red[i] = alpha[i] = 0;
159
160    depth = (bpp + 7) / 8;
161
162    /* Sanity check */
163    if(!im->w || im->w > 0x10000 || !im->h || im->h > 0x10000 || planes != 1)
164    {
165        fclose(fp);
166        free(im);
167        return NULL;
168    }
169
170    /* Allocate the pixel buffer */
171    im->pixels = malloc(im->w * im->h * depth);
172    if(!im->pixels)
173    {
174        fclose(fp);
175        free(im);
176        return NULL;
177    }
178
179    memset(im->pixels, 0, im->w * im->h * depth);
180
181    /* Read the bitmap data */
182    for(i = im->h; i--; )
183    {
184        unsigned int j, k, bits = 0;
185
186        switch(bpp)
187        {
188            case 1:
189                for(j = 0; j < im->w; j++)
190                {
191                    k = j % 32;
192                    if(k == 0)
193                        bits = u32fread(fp);
194                    im->pixels[im->w * i * depth + j] =
195                        (bits >> ((k & ~0xf) + 0xf - (k & 0xf))) & 0x1;
196                }
197                break;
198            case 4:
199                for(j = 0; j < im->w; j++)
200                {
201                    k = j % 8;
202                    if(k == 0)
203                        bits = u32fread(fp);
204                    im->pixels[im->w * i * depth + j] =
205                        (bits >> (4 * ((k & ~0x1) + 0x1 - (k & 0x1)))) & 0xf;
206                }
207                break;
208            default:
209                /* Works for 8bpp, but also for 16, 24 etc. */
210                fread(im->pixels + im->w * i * depth, im->w * depth, 1, fp);
211                /* Pad reads to 4 bytes */
212                tmp = (im->w * depth) % 4;
213                tmp = (4 - tmp) % 4;
214                while(tmp--)
215                    u8fread(fp);
216                break;
217        }
218    }
219
220    switch(depth)
221    {
222    case 3:
223        rmask = 0xff0000;
224        gmask = 0x00ff00;
225        bmask = 0x0000ff;
226        amask = 0x000000;
227        break;
228    case 2: /* XXX: those are the 16 bits values */
229        rmask = 0x7c00;
230        gmask = 0x03e0;
231        bmask = 0x001f;
232        amask = 0x0000;
233        break;
234    case 1:
235    default:
236        bpp = 8;
237        rmask = gmask = bmask = amask = 0;
238        break;
239    }
240
241    fclose(fp);
242
243    /* Create the libcucul dither */
244    im->dither = cucul_create_dither(bpp, im->w, im->h, depth * im->w,
245                                     rmask, gmask, bmask, amask);
246    if(!im->dither)
247    {
248        free(im->pixels);
249        free(im);
250        return NULL;
251    }
252
253    if(bpp == 8)
254        cucul_set_dither_palette(im->dither, red, green, blue, alpha);
255#endif
256
257    return im;
258}
259
260void unload_image(struct image * im)
261{
262#if defined(HAVE_IMLIB2_H)
263    /* Imlib_Image image = (Imlib_Image)im->priv; */
264    imlib_free_image();
265#else
266    free(im->pixels);
267#endif
268    cucul_free_dither(im->dither);
269}
270
271#if !defined(HAVE_IMLIB2_H)
272static unsigned int u32fread(FILE *fp)
273{
274    unsigned char buffer[4];
275    fread(buffer, 4, 1, fp);
276    return ((unsigned int)buffer[3] << 24) | ((unsigned int)buffer[2] << 16)
277             | ((unsigned int)buffer[1] << 8) | ((unsigned int)buffer[0]);
278}
279
280static unsigned int u16fread(FILE *fp)
281{
282    unsigned char buffer[2];
283    fread(buffer, 2, 1, fp);
284    return ((unsigned int)buffer[1] << 8) | ((unsigned int)buffer[0]);
285}
286
287static unsigned int u8fread(FILE *fp)
288{
289    unsigned char buffer;
290    fread(&buffer, 1, 1, fp);
291    return (unsigned int)buffer;
292}
293#endif
294
Note: See TracBrowser for help on using the repository browser.