summaryrefslogtreecommitdiff
path: root/jbig2dec/jbig2_image_pbm.c
blob: ac8e2ebe9484ab987426768732f32940349d05db (plain)
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
/* Copyright (C) 2001-2023 Artifex Software, Inc.
   All Rights Reserved.

   This software is provided AS-IS with no warranty, either express or
   implied.

   This software is distributed under license and may not be copied,
   modified or distributed except as expressly authorized under the terms
   of the license contained in the file LICENSE in this distribution.

   Refer to licensing information at http://www.artifex.com or contact
   Artifex Software, Inc.,  39 Mesa Street, Suite 108A, San Francisco,
   CA 94129, USA, for further information.
*/

/*
    jbig2dec
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "os_types.h"

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

#include "jbig2.h"
#include "jbig2_priv.h"
#include "jbig2_image.h"
#include "jbig2_image_rw.h"

/* take an image structure and write it to a file in pbm format */

int
jbig2_image_write_pbm_file(Jbig2Image *image, char *filename)
{
    FILE *out;
    int code;

    if ((out = fopen(filename, "wb")) == NULL) {
        fprintf(stderr, "unable to open '%s' for writing", filename);
        return 1;
    }

    code = jbig2_image_write_pbm(image, out);

    fclose(out);
    return (code);
}

/* write out an image struct as a pbm stream to an open file pointer */

int
jbig2_image_write_pbm(Jbig2Image *image, FILE *out)
{
    /* pbm header */
    fprintf(out, "P4\n%d %d\n", image->width, image->height);

    /* pbm format pads to a byte boundary, so we can
       just write out the whole data buffer
       NB: this assumes minimal stride for the width */
    fwrite(image->data, 1, image->height * image->stride, out);

    /* success */
    return 0;
}

/* take an image from a file in pbm format */
Jbig2Image *
jbig2_image_read_pbm_file(Jbig2Ctx *ctx, char *filename)
{
    FILE *in;
    Jbig2Image *image;

    if ((in = fopen(filename, "rb")) == NULL) {
        fprintf(stderr, "unable to open '%s' for reading\n", filename);
        return NULL;
    }

    image = jbig2_image_read_pbm(ctx, in);

    fclose(in);

    return (image);
}

/* FIXME: should handle multi-image files */
Jbig2Image *
jbig2_image_read_pbm(Jbig2Ctx *ctx, FILE *in)
{
    int i, dim[2];
    int done;
    Jbig2Image *image;
    int c;
    char buf[32];

    /* look for 'P4' magic */
    while ((c = fgetc(in)) != 'P') {
        if (feof(in))
            return NULL;
    }
    if ((c = fgetc(in)) != '4') {
        fprintf(stderr, "not a binary pbm file.\n");
        return NULL;
    }
    /* read size. we must find two decimal numbers representing
       the image dimensions. 'done' will index whether we're
       looking for the width or the height and 'i' will be our
       array index for copying strings into our buffer */
    done = 0;
    i = 0;
    while (done < 2) {
        c = fgetc(in);
        /* skip whitespace */
        if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
            continue;
        /* skip comments */
        if (c == '#') {
            while ((c = fgetc(in)) != '\n');
            continue;
        }
        /* report unexpected eof */
        if (c == EOF) {
            fprintf(stderr, "end-of-file parsing pbm header\n");
            return NULL;
        }
        if (isdigit(c)) {
            buf[i++] = c;
            while (isdigit(c = fgetc(in))) {
                if (i >= 32) {
                    fprintf(stderr, "pbm parsing error\n");
                    return NULL;
                }
                buf[i++] = c;
            }
            buf[i] = '\0';
            if (sscanf(buf, "%d", &dim[done]) != 1) {
                fprintf(stderr, "failed to read pbm image dimensions\n");
                return NULL;
            }
            i = 0;
            done++;
        }
    }
    /* allocate image structure */
    image = jbig2_image_new(ctx, dim[0], dim[1]);
    if (image == NULL) {
        fprintf(stderr, "failed to allocate %dx%d image for pbm file\n", dim[0], dim[1]);
        return NULL;
    }
    /* the pbm data is byte-aligned, so we can
       do a simple block read */
    (void)fread(image->data, 1, image->height * image->stride, in);
    if (feof(in)) {
        fprintf(stderr, "unexpected end of pbm file.\n");
        jbig2_image_release(ctx, image);
        return NULL;
    }
    /* success */
    return image;
}