~ubuntu-branches/ubuntu/warty/xplanet/warty

« back to all changes in this revision

Viewing changes to src/libimage/pnm.c

  • Committer: Bazaar Package Importer
  • Author(s): LaMont Jones
  • Date: 2004-08-24 07:14:00 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040824071400-2dr4qnjbjmm8z3ia
Tags: 1.0.6-1ubuntu1
Build-depend: libtiff4-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
    pnm.c - read and write pnm images using libpnm/netpbm routines.
 
3
    Distributed with Xplanet.
 
4
    Copyright (C) 2002 Hari Nair <hari@alumni.caltech.edu>
 
5
 
 
6
    This program is free software; you can redistribute it and/or modify
 
7
    it under the terms of the GNU General Public License as published by
 
8
    the Free Software Foundation; either version 2 of the License, or
 
9
    (at your option) any later version.
 
10
 
 
11
    This program is distributed in the hope that it will be useful,
 
12
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
    GNU General Public License for more details.
 
15
 
 
16
    You should have received a copy of the GNU General Public License
 
17
    along with this program; if not, write to the Free Software
 
18
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
19
****************************************************************************/
 
20
 
 
21
#include <stdio.h>
 
22
#include <string.h>
 
23
 
 
24
#include <pnm.h>
 
25
 
 
26
int
 
27
read_pnm(const char *filename, int *width, int *height, char **rgb)
 
28
{
 
29
    FILE *infile = fopen(filename, "rb");
 
30
 
 
31
    xelval maxvalP;
 
32
    int formatP;
 
33
 
 
34
    xel **imagearray;
 
35
 
 
36
    int argcP = 1;
 
37
    char **argv;
 
38
 
 
39
    int i, j, ipos = 0;
 
40
 
 
41
    argv = malloc(sizeof(char **));
 
42
    if (argv == NULL)
 
43
    {
 
44
        fprintf(stderr, "Can't allocate memory in read_pnm().\n");
 
45
        return(0);
 
46
    }
 
47
 
 
48
    argv[0] = malloc(10);
 
49
    if (argv[0] == NULL)
 
50
    {
 
51
        fprintf(stderr, "Can't allocate memory in read_pnm().\n");
 
52
        return(0);
 
53
    }
 
54
 
 
55
    strcpy(argv[0], "loadimage");
 
56
 
 
57
    pnm_init(&argcP, argv);
 
58
 
 
59
    imagearray = pnm_readpnm(infile, width, height, &maxvalP, &formatP);
 
60
 
 
61
    rgb[0] = malloc(3 * *width * *height);
 
62
    if (rgb[0] == NULL)
 
63
    {
 
64
        fprintf(stderr, "Can't allocate memory for PNM file.\n");
 
65
        return(0);
 
66
    }
 
67
 
 
68
    switch (PNM_FORMAT_TYPE(formatP))
 
69
    {
 
70
    case PPM_TYPE:
 
71
        for (j = 0; j < *height; j++)
 
72
            for (i = 0; i < *width; i++)
 
73
            {
 
74
                rgb[0][ipos++] = (unsigned char) (PPM_GETR(imagearray[j][i]) 
 
75
                                                  & 0xff); 
 
76
                rgb[0][ipos++] = (unsigned char) (PPM_GETG(imagearray[j][i]) 
 
77
                                                  & 0xff); 
 
78
                rgb[0][ipos++] = (unsigned char) (PPM_GETB(imagearray[j][i]) 
 
79
                                                  & 0xff); 
 
80
            }
 
81
        break;
 
82
    case PGM_TYPE:
 
83
    case PBM_TYPE:
 
84
        for (j = 0; j < *height; j++)
 
85
            for (i = 0; i < *width; i++)
 
86
            {
 
87
                memset(rgb[0] + ipos, 
 
88
                       (unsigned char) (PNM_GET1(imagearray[j][i]) & 0xff), 
 
89
                       3);
 
90
                ipos +=3;
 
91
            }
 
92
        break;
 
93
    default:
 
94
        fprintf(stderr, "Unknown magic number for pnm file\n");
 
95
        return(0);
 
96
    }
 
97
 
 
98
    pnm_freearray(imagearray, *height);
 
99
    return(1);
 
100
}
 
101
 
 
102
int
 
103
write_pnm(FILE *outfile, int width, int height, unsigned char *rgb,
 
104
          int maxv, int format, int forceplain)
 
105
{
 
106
    xelval maxval = maxv;
 
107
    xel **imagearray = pnm_allocarray(width, height);
 
108
    int i, j, ipos;
 
109
    xelval avg;
 
110
 
 
111
    switch (format)
 
112
    {
 
113
    case PBM_TYPE:
 
114
        for (j = 0; j < height; j++)
 
115
        { 
 
116
            ipos = j * width;
 
117
            for (i = 0; i < width; i++)
 
118
            {
 
119
                avg = (rgb[3 * (ipos + i)] + rgb[3 * (ipos + i) + 1] 
 
120
                       + rgb[3 * (ipos + i) + 2]) / 3;
 
121
                PNM_ASSIGN1(imagearray[j][i], (avg > 127 ? 1 : 0));
 
122
            }
 
123
        }
 
124
        break;
 
125
    case PGM_TYPE:
 
126
        for (j = 0; j < height; j++)
 
127
        { 
 
128
            ipos = j * width;
 
129
            for (i = 0; i < width; i++)
 
130
            {
 
131
                avg = (rgb[3 * (ipos + i)] + rgb[3 * (ipos + i) + 1] 
 
132
                       + rgb[3 * (ipos + i) + 2]) / 3;
 
133
                PNM_ASSIGN1(imagearray[j][i], avg);
 
134
            }
 
135
        }
 
136
        break;
 
137
    case PPM_TYPE:
 
138
        for (j = 0; j < height; j++)
 
139
        { 
 
140
            ipos = j * width;
 
141
            for (i = 0; i < width; i++)
 
142
            {
 
143
                PPM_ASSIGN(imagearray[j][i],
 
144
                           (xelval) rgb[3 * (ipos + i)],
 
145
                           (xelval) rgb[3 * (ipos + i) + 1],
 
146
                           (xelval) rgb[3 * (ipos + i) + 2]);
 
147
            }
 
148
        }
 
149
        break;
 
150
    default:
 
151
        fprintf(stderr, "Unknown pnm format\n");
 
152
    }
 
153
 
 
154
    pnm_writepnm(outfile, imagearray, width, height, maxval, format, 
 
155
                 forceplain);
 
156
 
 
157
    pnm_freearray(imagearray, height);
 
158
    return(1);
 
159
}