about summary refs log tree commit diff
path: root/converter/pbm/pbmtoppa/pbm.c
blob: ae36e0d295e70148f5868a870a13fe45d4b82ec2 (plain) (blame)
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
/* pbm.c
 * code for reading the header of an ASCII PBM file
 * Copyright (c) 1998 Tim Norman.  See LICENSE for details
 * 2-25-98
 *
 * Mar 18, 1998  Jim Peterson  <jspeter@birch.ee.vt.edu>
 *
 *     Restructured to encapsulate more of the PBM handling.
 */

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

#include "pm.h"
#include "nstring.h"
#include "ppapbm.h"

int
make_pbm_stat(pbm_stat * const pbmStatP,
              FILE *     const ifP) {

    char line[1024];
    char * rc;
    int retval;

    pbmStatP->fptr         = ifP;
    pbmStatP->version      = none;
    pbmStatP->current_line = 0;
    pbmStatP->unread       = 0;

    rc = fgets(line, 1024, ifP);
    if (rc == NULL)
        retval = 0;
    else {
        line[strlen(line)-1] = 0;

        if (streq(line,"P1"))
            pbmStatP->version=P1;
        if (streq(line,"P4"))
            pbmStatP->version=P4;

        if (pbmStatP->version == none) {
            pm_message("unknown PBM magic '%s'", line);
            retval = 0;
        } else {
            do {
                char * rc;
                rc = fgets(line, 1024, ifP);
                if (rc == NULL)
                    return 0;
            } while (line[0] == '#');
            {
                int rc;
                rc = sscanf(line, "%d %d",
                            &pbmStatP->width, &pbmStatP->height);
                if (rc != 2)
                    retval = 0;
                else {
                    if (pbmStatP->width < 0) {
                        pm_message("Image has negative width");
                        retval = 0;
                    } else if (pbmStatP->width > INT_MAX/2) {
                        pm_message("Uncomputeably large width: %d",
                                   pbmStatP->width);
                        retval = 0;
                    } else if (pbmStatP->height < 0) {
                        pm_message("Image has negative height");
                        retval = 0;
                    } else if (pbmStatP->height > INT_MAX/2) {
                        pm_message("Uncomputeably large height: %d",
                                   pbmStatP->height);
                        retval = 0;
                    } else
                        retval = 1;
                }
            }
        }
    }
    return retval;
}



static int
getbytes(FILE *          const ifP,
         unsigned int    const width,
         unsigned char * const data) {

    unsigned char mask;
    unsigned char acc;
    unsigned char * place;
    unsigned int num;
    int retval;

    if (width == 0)
        retval = 0;
    else {
        for (mask = 0x80, acc = 0, num = 0, place = data; num < width; ) {
            switch (getc(ifP)) {
            case EOF:
                return 0;
            case '1':
                acc |= mask;
                /* fall through */
            case '0':
                mask >>= 1;
                ++num;
                if (mask == 0x00) { /* if (num % 8 == 0) */
                    *place++ = acc;
                    acc = 0;
                    mask = 0x80;
                }
            }
        }
        if (width % 8 != 0)
            *place = acc;

        retval = 1;
    }
    return retval;
}



int
pbm_readline(pbm_stat *      const pbmStatP,
             unsigned char * const data) {
/*----------------------------------------------------------------------------
  Read a single line into data which must be at least (pbmStatP->width+7)/8
  bytes of storage.
-----------------------------------------------------------------------------*/
    int retval;

    if (pbmStatP->current_line >= pbmStatP->height)
        retval = 0;
    else {
        if (pbmStatP->unread) {
            memcpy(data, pbmStatP->revdata, (pbmStatP->width+7)/8);
            ++pbmStatP->current_line;
            pbmStatP->unread = 0;
            free(pbmStatP->revdata);
            pbmStatP->revdata = NULL;
            retval = 1;
        } else {
            switch (pbmStatP->version) {
            case P1:
                if (getbytes(pbmStatP->fptr, pbmStatP->width, data)) {
                    pbmStatP->current_line++;
                    retval = 1;
                } else
                    retval = 0;
                break;
            case P4: {
                int tmp, tmp2;
                tmp = (pbmStatP->width+7)/8;
                tmp2 = fread(data,1,tmp,pbmStatP->fptr);
                if (tmp2 == tmp) {
                    ++pbmStatP->current_line;
                    retval = 1;
                } else {
                    pm_message("error reading line data (%d)", tmp2);
                    retval = 0;
                }
            } break;

            default:
                pm_message("unknown PBM version");
                retval = 0;
            }
        }
    }
    return retval;
}



void
pbm_unreadline(pbm_stat * const pbmStatP,
               void *     const data) {
/*----------------------------------------------------------------------------
  Push a line back into the buffer; we read too much!
-----------------------------------------------------------------------------*/
    /* can store only one line in the unread buffer */

    if (!pbmStatP->unread) {
        pbmStatP->unread = 1;
        pbmStatP->revdata = malloc ((pbmStatP->width+7)/8);
        memcpy(pbmStatP->revdata, data, (pbmStatP->width+7)/8);
        --pbmStatP->current_line;
    }
}