about summary refs log tree commit diff
path: root/converter/other/pamtoavs.c
blob: 4764c9e8833638509fbd84c41b4f2d423e223880 (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
/* ----------------------------------------------------------------------
 *
 * Convert a PAM image to an AVS X image
 *
 * By Scott Pakin <scott+pbm@pakin.org>
 *
 * ----------------------------------------------------------------------
 *
 * Copyright (C) 2010 Scott Pakin <scott+pbm@pakin.org>
 *
 * 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 3 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, see http://www.gnu.org/licenses/.
 *
 * ----------------------------------------------------------------------
 */

#include <stdio.h>
#include "pm.h"
#include "pam.h"



static char
sample2char(sample const s,
            sample const maxval) {
/* Scale down a sample to a single byte. */

    return maxval==255 ? s : s * 255 / maxval;
}


#define THIS_SAMPLE_CHAR(PLANE) \
  sample2char(tuplerow[col][PLANE], pamP->maxval)

static void
produceAvs(struct pam * const pamP,
           FILE *       const avsFileP) {

    tuple * tuplerow;

    /* Write the AVS header (image width and height as 4-byte
       big-endian integers).
    */
    pm_writebiglong(avsFileP, pamP->width);
    pm_writebiglong(avsFileP, pamP->height);

    /* Write the AVS data (alpha, red, green, blue -- one byte apiece. */
    tuplerow = pnm_allocpamrow(pamP);
    switch (pamP->depth) {
    case 1: {
        /* Black-and-white or grayscale, no alpha */
        unsigned int row;
        for (row = 0; row < pamP->height; ++row) {
            unsigned int col;
            pnm_readpamrow(pamP, tuplerow);
            for (col = 0; col < pamP->width; ++col) {
                pm_writechar(avsFileP, (char)255);
                pm_writechar(avsFileP, THIS_SAMPLE_CHAR(0));
                pm_writechar(avsFileP, THIS_SAMPLE_CHAR(0));
                pm_writechar(avsFileP, THIS_SAMPLE_CHAR(0));
            }
        }
    } break;

    case 2: {
        /* Black-and-white or grayscale plus alpha */
        unsigned int row;
        for (row = 0; row < pamP->height; ++row) {
            unsigned int col;
            pnm_readpamrow(pamP, tuplerow);
            for (col = 0; col < pamP->width; ++col) {
                pm_writechar(avsFileP, THIS_SAMPLE_CHAR(1));
                pm_writechar(avsFileP, THIS_SAMPLE_CHAR(0));
                pm_writechar(avsFileP, THIS_SAMPLE_CHAR(0));
                pm_writechar(avsFileP, THIS_SAMPLE_CHAR(0));
            }
        }
    } break;

    case 3: {
        /* RGB, no alpha */
        unsigned int row;
        for (row = 0; row < pamP->height; ++row) {
            unsigned int col;
            pnm_readpamrow(pamP, tuplerow);
            for (col = 0; col < pamP->width; ++col) {
                pm_writechar(avsFileP, (char)255);
                pm_writechar(avsFileP, THIS_SAMPLE_CHAR(0));
                pm_writechar(avsFileP, THIS_SAMPLE_CHAR(1));
                pm_writechar(avsFileP, THIS_SAMPLE_CHAR(2));
            }
        }
    } break;

    case 4: {
        /* RGB plus alpha */
        unsigned int row;
        for (row = 0; row < pamP->height; ++row) {
            unsigned int col;
            pnm_readpamrow( pamP, tuplerow );
            for (col = 0; col < pamP->width; ++col) {
                pm_writechar(avsFileP, THIS_SAMPLE_CHAR(3));
                pm_writechar(avsFileP, THIS_SAMPLE_CHAR(0));
                pm_writechar(avsFileP, THIS_SAMPLE_CHAR(1));
                pm_writechar(avsFileP, THIS_SAMPLE_CHAR(2));
            }
        }
    } break;

    default:
        pm_error("Unrecognized PAM depth %u.  We understand only "
                 "1, 2, 3, and 4", pamP->depth);
        break;
    }
    pnm_freepamrow(tuplerow);
}



int
main(int argc, const char *argv[]) {
    struct pam   inPam;
    const char * inputFilename;
    FILE       * inFileP;

    pm_proginit(&argc, argv);

    inputFilename = (argc > 1) ? argv[1] : "-";

    inFileP = pm_openr(inputFilename);

    pnm_readpaminit(inFileP, &inPam, PAM_STRUCT_SIZE(tuple_type));

    produceAvs(&inPam, stdout);

    pm_closer(inFileP);

    return 0;
}