File: image.c

package info (click to toggle)
xplanet 0.94-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,624 kB
  • ctags: 1,191
  • sloc: cpp: 9,466; ansic: 6,375; sh: 2,065; makefile: 380; perl: 55
file content (409 lines) | stat: -rw-r--r-- 11,480 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/****************************************************************************
    image.c - a simple image manipulation library.  Distributed with Xplanet.
    Copyright (C) 2002 Hari Nair <hari@alumni.caltech.edu>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
****************************************************************************/

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "checkfuncs.h"

int
read_bmp(const char *filename, int *width, int *height, unsigned char **rgb);
int
write_bmp(const char *filename, int width, int height, unsigned char *rgb);

#ifdef HAVE_LIBGIF
int
read_gif(const char *filename, int *width, int *height, unsigned char **rgb);
int
write_gif(const char *filename, int width, int height, unsigned char *rgb);
#endif

#ifdef HAVE_LIBJPEG
int
read_jpeg(const char *filename, int *width, int *height, unsigned char **rgb);
int
write_jpeg(FILE *outfile, int width, int height, unsigned char *rgb, 
           int quality);
#endif

#ifdef HAVE_LIBPNG
int
read_png(const char *filename, int *width, int *height, unsigned char **rgb);
int
write_png(FILE *outfile, int width, int height, unsigned char *rgb, 
	  unsigned char *alpha);
#endif

#ifdef HAVE_LIBPNM
#include <pnm.h>
int
read_pnm(const char *filename, int *width, int *height, unsigned char **rgb);
int
write_pnm(FILE *outfile, int width, int height, unsigned char *rgb,
          int maxv, int format, int forceplain);
#endif

#ifdef HAVE_LIBTIFF
int
read_tiff(const char *filename, int *width, int *height, unsigned char **rgb);
int
write_tiff(const char *filename, int width, int height, unsigned char *rgb);
#endif

static unsigned char *alpha;     /* PNG alpha (opacity) channel */
static int Q;                    /* JPEG Quality */

void
set_alpha(unsigned char *A)
{
    alpha = A;
}

void
set_quality(int q)
{
    Q = q;
}

int
read_image(const char *filename, int *width, int *height, 
	   unsigned char **rgb)
{
    char buf[4];
    unsigned char *ubuf = (unsigned char *) buf;
    int success = 0;

    FILE *file;
    file = fopen(filename, "rb");
    if (file == NULL) return(0);
  
    /* see what kind of file we have */

    fread(buf, 1, 4, file);
    fclose(file);

    if (!strncmp("BM", buf, 2))
    {
        success = read_bmp(filename, width, height, rgb);
    }
    else if (!strncmp("GIF8", buf, 4))
    {
#ifdef HAVE_LIBGIF
        success = read_gif(filename, width, height, rgb);
#else
        fprintf(stderr, 
                "Sorry, this program was not compiled with GIF support\n");
        success = 0;
#endif /* HAVE_LIBGIF */
    }
    else if ((ubuf[0] == 0xff) && (ubuf[1] == 0xd8))
    {
#ifdef HAVE_LIBJPEG
        success = read_jpeg(filename, width, height, rgb);
#else
        fprintf(stderr, 
                "Sorry, this program was not compiled with JPEG support\n");
        success = 0;
#endif /* HAVE_LIBJPEG */
    }
    else if ((ubuf[0] == 0x89) && !strncmp("PNG", buf+1, 3))
    {
#ifdef HAVE_LIBPNG
        success = read_png(filename, width, height, rgb);
#else
        fprintf(stderr, 
                "Sorry, this program was not compiled with PNG support\n");
        success = 0;
#endif /* HAVE_LIBPNG */
    }
    else if ((   !strncmp("P6\n", buf, 3))
             || (!strncmp("P5\n", buf, 3))
             || (!strncmp("P4\n", buf, 3))
             || (!strncmp("P3\n", buf, 3))
             || (!strncmp("P2\n", buf, 3))
             || (!strncmp("P1\n", buf, 3)))
    {
#ifdef HAVE_LIBPNM
        success = read_pnm(filename, width, height, rgb);
#else
        fprintf(stderr, 
                "Sorry, this program was not compiled with PNM support\n");
        success = 0;
#endif /* HAVE_LIBPNM */
    }
    else if (((!strncmp ("MM", buf, 2)) && (ubuf[2] == 0x00) 
              && (ubuf[3] == 0x2a))
             || ((!strncmp ("II", buf, 2)) && (ubuf[2] == 0x2a) 
                 && (ubuf[3] == 0x00)))
    {
#ifdef HAVE_LIBTIFF
        success = read_tiff(filename, width, height, rgb);
#else
        fprintf(stderr, 
                "Sorry, this program was not compiled with TIFF support\n");
        success = 0;
#endif
    }
    else
    {
        fprintf(stderr, "Unknown image format\n");
        success = 0;
    }

    return(success);
}

int
write_image(const char *filename, int width, int height, unsigned char *rgb)
{
    FILE *outfile;
    char *extension = strrchr(filename, '.');
    char *lowercase;
    char *ptr;
    int success = 0;
  
    lowercase = malloc(strlen(extension) + 1);
    strcpy(lowercase, extension);
    ptr = lowercase;

    while (*ptr != '\0') *ptr++ = tolower(*extension++);

    outfile = fopen(filename, "wb");
    if (outfile == NULL) return(0);
  
    if (strcmp(lowercase, ".bmp" ) == 0)
    {
        success = write_bmp(filename, width, height, rgb); 
    }
    else if (strcmp(lowercase, ".gif" ) == 0)
    {
#ifdef HAVE_LIBGIF
        success = write_gif(filename, width, height, rgb); 
#else
        fprintf(stderr, 
                "Sorry, this program was not compiled with GIF support\n");
        success = 0;
#endif /* HAVE_LIBPNG */
    }
    else if ((   strcmp(lowercase, ".jpg" ) == 0)
             || (strcmp(lowercase, ".jpeg") == 0))
    {
#ifdef HAVE_LIBJPEG
        success = write_jpeg(outfile, width, height, rgb, Q); 
#else
        fprintf(stderr, 
                "Sorry, this program was not compiled with JPEG support\n");
        success = 0;
#endif /* HAVE_LIBJPEG */
    }

    else if (strcmp(lowercase, ".png" ) == 0)
    {
#ifdef HAVE_LIBPNG
        success = write_png(outfile, width, height, rgb, alpha); 
#else
        fprintf(stderr, 
                "Sorry, this program was not compiled with PNG support\n");
        success = 0;
#endif /* HAVE_LIBPNG */
    }

    else if ((   strcmp(lowercase, ".pbm") == 0)
             || (strcmp(lowercase, ".pgm") == 0)
             || (strcmp(lowercase, ".ppm") == 0))
    {
#ifdef HAVE_LIBPNM
        if (strcmp(lowercase, ".pbm") == 0)
            success = write_pnm(outfile, width, height, rgb, 1, PBM_TYPE, 0);
        else if (strcmp(lowercase, ".pgm") == 0)
            success = write_pnm(outfile, width, height, rgb, 255, 
                                PGM_TYPE, 0);
        else if (strcmp(lowercase, ".ppm") == 0)
            success = write_pnm(outfile, width, height, rgb, 255, 
                                PPM_TYPE, 0);
#else
        fprintf(stderr, 
                "Sorry, this program was not compiled with PNM support\n");
        success = 0;
#endif /* HAVE_LIBPNM */
    }

    else if ((strcmp(lowercase, ".tif" ) == 0)
             || (strcmp(lowercase, ".tiff" ) == 0))
    {
#ifdef HAVE_LIBTIFF
        success = write_tiff(filename, width, height, rgb); 
#else
        fprintf(stderr, 
                "Sorry, this program was not compiled with TIFF support\n");
        success = 0;
#endif /* HAVE_LIBTIFF */
    }

    else
    {
        fprintf(stderr, "Unknown image format\n");
        success = 0;
    }

    free(lowercase);
    fclose(outfile);
    return(success);
}

int
delete_image(unsigned char **rgb)
{
    free(rgb[0]);
    rgb[0] = NULL;
    return(1);
}

int 
crop_image(unsigned char **rgb, int in_width, int in_height, int x, int y, 
           int width, int height)
{
    int i, j;
    int ipos = 0;
    unsigned char *old_rgb = rgb[0];
    unsigned char *new_rgb = malloc(3 * width * height);

    if (new_rgb == NULL)
    {
        fprintf(stderr, "Can't allocate memory in crop_image().\n");
        return(0);
    }
    
    for (j = 0; j < height; j++)
        for (i = 0; i < width; i++)
        {
            memcpy(new_rgb + ipos, 
                   old_rgb + 3 * ((j + y) * in_width + (i + x)), 3);
            ipos += 3;
        }
    rgb[0] = new_rgb;
    free(old_rgb);
    return(1);
}

int
resize_image(unsigned char **rgb, int in_width, int in_height, 
             int out_width, int out_height, int bilinear)
{
    int i, j, ii, jj;
    double dx, dy;
    double t, u;
    int in_pos[4], out_pos;
    int sum;
    double weight[4];
    int ix[4], iy[4];

    double frac_h = ((double) in_height - 1) / (out_height - 1);
    double frac_w = ((double) in_width - 1) / (out_width - 1);

    unsigned char *old_rgb = rgb[0];
    unsigned char *new_rgb = malloc(3 * out_width * out_height);

    if (new_rgb == NULL)
    {
        fprintf(stderr, "Can't allocate memory in resize_image().\n");
        return(0);
    }

    if (in_width == out_width && in_height == out_height) return(1);

    if (bilinear)
    {
        for (j = 0; j < out_height; j++)
        {
            dy = ((double) j / (out_height - 1)) * (in_height - 1);
          
            iy[0] = iy[1] = (int) dy;
            iy[2] = iy[0] + 1;
            if (iy[2] == in_height) iy[2]--;
            iy[3] = iy[2];
          
            u = 1 - (dy - iy[0]);
            for (i = 0; i < out_width; i++)
            {
                dx = ((double) i / (out_width - 1)) * (in_width - 1);
              
                ix[0] = ix[2] = (int) dx;
                ix[1] = ix[0] + 1;
                if (ix[1] == in_width) ix[1] = 0;
                ix[3] = ix[1];

                t = dx - ix[0];
                if (t > in_width/2) 
                    t -= in_width;
                else if (t < -in_width/2) 
                    t += in_width;
              
                /*
                  Weights are from Numerical Recipes, 2nd Edition
                  weight[0] = (1 - t) * u;
                  weight[1] = t * u;
                  weight[2] = (1-t) * (1-u);
                  weight[3] = t * (1-u);
                */
              
                weight[1] = t * u;
                weight[0] = u - weight[1];
                weight[2] = 1 - t - u + weight[1];
                weight[3] = t - weight[1];
          
                for (jj = 0; jj < 4; jj++)
                    in_pos[jj] = 3 * (iy[jj] * in_width + ix[jj]);

                out_pos = 3 * (j * out_width + i);
                for (ii = 0; ii < 3; ii++)
                {
                    sum = 0;
                    for (jj = 0; jj < 4; jj++)
                        sum += (int) (weight[jj] * rgb[0][in_pos[jj] + ii]);
                    new_rgb[out_pos + ii] = (unsigned char) (sum & 0xff);
                }
            }
        }
    }
    else
    {
        out_pos = 0;
        for (j = 0; j < out_height; j++)
        {
            dy = j * frac_h;
            iy[0] = (int) dy;
            for (i = 0; i < out_width; i++)
            {
                dx = i * frac_w;
                ix[0] = (int) dx;
                in_pos[0] = 3 * (iy[0] * in_width + ix[0]);

                memcpy(new_rgb + out_pos, rgb[0] + in_pos[0], 3);
                out_pos += 3;
            }
        }
    }

    rgb[0] = new_rgb;
    free(old_rgb);
    return(1);
}