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

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