about summary refs log tree commit diff
path: root/lib/libppm2.c
blob: f54f2cef9301c00acf90b5a1b314dcf7c1d5ffee (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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/* libppm2.c - ppm utility library part 2
**
** Copyright (C) 1989 by Jef Poskanzer.
**
** Permission to use, copy, modify, and distribute this software and its
** documentation for any purpose and without fee is hereby granted, provided
** that the above copyright notice appear in all copies and that both that
** copyright notice and this permission notice appear in supporting
** documentation.  This software is provided "as is" without express or
** implied warranty.
*/

#include <string.h>
#include <errno.h>


#include "netpbm/pm_c_util.h"
#include "netpbm/mallocvar.h"
#include "libpgm.h"
#include "libppm.h"
#include "ppm.h"



void
ppm_writeppminit(FILE*  const fileP,
                 int    const cols,
                 int    const rows,
                 pixval const maxval,
                 int    const forceplain) {

    bool const plainFormat = forceplain || pm_plain_output;

    /* For Caller's convenience, we include validating computability of the
       image parameters, since Caller may be using them in arithmetic after
       our return.
    */
    ppm_validateComputableSize(cols, rows);
    pgm_validateComputableMaxval(maxval);

    if (maxval > PPM_OVERALLMAXVAL && !plainFormat)
        pm_error("too-large maxval passed to ppm_writeppminit(): %d."
                 "Maximum allowed by the PPM format is %d.",
                 maxval, PPM_OVERALLMAXVAL);

    fprintf(fileP, "%c%c\n%d %d\n%d\n",
            PPM_MAGIC1,
            plainFormat || maxval >= 1<<16 ? PPM_MAGIC2 : RPPM_MAGIC2,
            cols, rows, maxval );
}



static void
putus(unsigned short const n,
      FILE *         const fileP) {

    if (n >= 10)
        putus(n / 10, fileP);
    putc('0' + n % 10, fileP);
}



static void
format1bpsRow(const pixel *   const pixelrow,
              unsigned int    const cols,
              unsigned char * const rowBuffer) {

    /* single byte samples. */

    unsigned int col;
    unsigned int bufferCursor;

    bufferCursor = 0;

    for (col = 0; col < cols; ++col) {
        rowBuffer[bufferCursor++] = PPM_GETR(pixelrow[col]);
        rowBuffer[bufferCursor++] = PPM_GETG(pixelrow[col]);
        rowBuffer[bufferCursor++] = PPM_GETB(pixelrow[col]);
    }
}




static void
format2bpsRow(const pixel *   const pixelrow,
              unsigned int    const cols,
              unsigned char * const rowBuffer) {

    /* two byte samples. */

    unsigned int col;
    unsigned int bufferCursor;

    bufferCursor = 0;

    for (col = 0; col < cols; ++col) {
        pixval const r = PPM_GETR(pixelrow[col]);
        pixval const g = PPM_GETG(pixelrow[col]);
        pixval const b = PPM_GETB(pixelrow[col]);

        rowBuffer[bufferCursor++] = r >> 8;
        rowBuffer[bufferCursor++] = (unsigned char)r;
        rowBuffer[bufferCursor++] = g >> 8;
        rowBuffer[bufferCursor++] = (unsigned char)g;
        rowBuffer[bufferCursor++] = b >> 8;
        rowBuffer[bufferCursor++] = (unsigned char)b;
    }
}



static void
ppm_writeppmrowraw(FILE *        const fileP,
                   const pixel * const pixelrow,
                   unsigned int  const cols,
                   pixval        const maxval ) {

    unsigned int const bytesPerSample = maxval < 256 ? 1 : 2;
    unsigned int const bytesPerRow    = cols * 3 * bytesPerSample;

    unsigned char * rowBuffer;
    ssize_t rc;

    MALLOCARRAY(rowBuffer, bytesPerRow);

    if (rowBuffer == NULL)
        pm_error("Unable to allocate memory for row buffer "
                 "for %u columns", cols);

    if (maxval < 256)
        format1bpsRow(pixelrow, cols, rowBuffer);
    else
        format2bpsRow(pixelrow, cols, rowBuffer);

    rc = fwrite(rowBuffer, 1, bytesPerRow, fileP);

    if (rc < 0)
        pm_error("Error writing row.  fwrite() errno=%d (%s)",
                 errno, strerror(errno));
    else {
        size_t const bytesWritten = rc;

        if (bytesWritten != bytesPerRow)
            pm_error("Error writing row.  Short write of %u bytes "
                     "instead of %u", (unsigned)bytesWritten, bytesPerRow);
    }
    free(rowBuffer);
}




static void
ppm_writeppmrowplain(FILE *        const fileP,
                     const pixel * const pixelrow,
                     unsigned int  const cols,
                     pixval        const maxval) {

    unsigned int col;
    unsigned int charcount;

    charcount = 0;

    for (col = 0; col < cols; ++col) {
        if (charcount >= 65) {
            putc('\n', fileP);
            charcount = 0;
        } else if (charcount > 0) {
            putc(' ', fileP);
            putc(' ', fileP);
            charcount += 2;
        }
        putus(PPM_GETR(pixelrow[col]), fileP);
        putc(' ', fileP);
        putus(PPM_GETG(pixelrow[col]), fileP);
        putc(' ', fileP);
        putus(PPM_GETB(pixelrow[col]), fileP);
        charcount += 11;
    }
    if (charcount > 0)
        putc('\n', fileP);
}



void
ppm_writeppmrow(FILE *        const fileP,
                const pixel * const pixelrow,
                int           const cols,
                pixval        const maxval,
                int           const forceplain) {

    if (forceplain || pm_plain_output || maxval >= 1<<16)
        ppm_writeppmrowplain(fileP, pixelrow, cols, maxval);
    else
        ppm_writeppmrowraw(fileP, pixelrow, cols, maxval);
}



void
ppm_writeppm(FILE *  const file,
             pixel** const pixels,
             int     const cols,
             int     const rows,
             pixval  const maxval,
             int     const forceplain)  {
    int row;

    ppm_writeppminit(file, cols, rows, maxval, forceplain);

    for (row = 0; row < rows; ++row)
        ppm_writeppmrow(file, pixels[row], cols, maxval, forceplain);
}