| 1 |
/* ---------------------------------------------------------------------- |
| 2 |
* |
| 3 |
* Convert a PAM image to an AVS X image |
| 4 |
* |
| 5 |
* By Scott Pakin <[email protected]> |
| 6 |
* |
| 7 |
* ---------------------------------------------------------------------- |
| 8 |
* |
| 9 |
* Copyright (C) 2010 Scott Pakin <[email protected]> |
| 10 |
* |
| 11 |
* This program is free software: you can redistribute it and/or modify |
| 12 |
* it under the terms of the GNU General Public License as published by |
| 13 |
* the Free Software Foundation, either version 3 of the License, or (at |
| 14 |
* your option) any later version. |
| 15 |
* |
| 16 |
* This program is distributed in the hope that it will be useful, but |
| 17 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 19 |
* General Public License for more details. |
| 20 |
* |
| 21 |
* You should have received a copy of the GNU General Public License |
| 22 |
* along with this program. If not, see http://www.gnu.org/licenses/. |
| 23 |
* |
| 24 |
* ---------------------------------------------------------------------- |
| 25 |
*/ |
| 26 |
|
| 27 |
#include <stdio.h> |
| 28 |
#include "pm.h" |
| 29 |
#include "pam.h" |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
static char |
| 34 |
sample2char(sample const s, |
| 35 |
sample const maxval) { |
| 36 |
/* Scale down a sample to a single byte. */ |
| 37 |
|
| 38 |
return maxval==255 ? s : s * 255 / maxval; |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
#define THIS_SAMPLE_CHAR(PLANE) \ |
| 44 |
sample2char(tuplerow[col][PLANE], pamP->maxval) |
| 45 |
|
| 46 |
static void |
| 47 |
produceAvs(struct pam * const pamP, |
| 48 |
FILE * const avsFileP) { |
| 49 |
|
| 50 |
tuple * tuplerow; |
| 51 |
|
| 52 |
/* Write the AVS header (image width and height as 4-byte |
| 53 |
big-endian integers). |
| 54 |
*/ |
| 55 |
pm_writebiglong(avsFileP, pamP->width); |
| 56 |
pm_writebiglong(avsFileP, pamP->height); |
| 57 |
|
| 58 |
/* Write the AVS data (alpha, red, green, blue -- one byte apiece. */ |
| 59 |
tuplerow = pnm_allocpamrow(pamP); |
| 60 |
switch (pamP->depth) { |
| 61 |
case 1: { |
| 62 |
/* Black-and-white or grayscale, no alpha */ |
| 63 |
unsigned int row; |
| 64 |
for (row = 0; row < pamP->height; ++row) { |
| 65 |
unsigned int col; |
| 66 |
pnm_readpamrow(pamP, tuplerow); |
| 67 |
for (col = 0; col < pamP->width; ++col) { |
| 68 |
pm_writechar(avsFileP, (char)255); |
| 69 |
pm_writechar(avsFileP, THIS_SAMPLE_CHAR(0)); |
| 70 |
pm_writechar(avsFileP, THIS_SAMPLE_CHAR(0)); |
| 71 |
pm_writechar(avsFileP, THIS_SAMPLE_CHAR(0)); |
| 72 |
} |
| 73 |
} |
| 74 |
} break; |
| 75 |
|
| 76 |
case 2: { |
| 77 |
/* Black-and-white or grayscale plus alpha */ |
| 78 |
unsigned int row; |
| 79 |
for (row = 0; row < pamP->height; ++row) { |
| 80 |
unsigned int col; |
| 81 |
pnm_readpamrow(pamP, tuplerow); |
| 82 |
for (col = 0; col < pamP->width; ++col) { |
| 83 |
pm_writechar(avsFileP, THIS_SAMPLE_CHAR(1)); |
| 84 |
pm_writechar(avsFileP, THIS_SAMPLE_CHAR(0)); |
| 85 |
pm_writechar(avsFileP, THIS_SAMPLE_CHAR(0)); |
| 86 |
pm_writechar(avsFileP, THIS_SAMPLE_CHAR(0)); |
| 87 |
} |
| 88 |
} |
| 89 |
} break; |
| 90 |
|
| 91 |
case 3: { |
| 92 |
/* RGB, no alpha */ |
| 93 |
unsigned int row; |
| 94 |
for (row = 0; row < pamP->height; ++row) { |
| 95 |
unsigned int col; |
| 96 |
pnm_readpamrow(pamP, tuplerow); |
| 97 |
for (col = 0; col < pamP->width; ++col) { |
| 98 |
pm_writechar(avsFileP, (char)255); |
| 99 |
pm_writechar(avsFileP, THIS_SAMPLE_CHAR(0)); |
| 100 |
pm_writechar(avsFileP, THIS_SAMPLE_CHAR(1)); |
| 101 |
pm_writechar(avsFileP, THIS_SAMPLE_CHAR(2)); |
| 102 |
} |
| 103 |
} |
| 104 |
} break; |
| 105 |
|
| 106 |
case 4: { |
| 107 |
/* RGB plus alpha */ |
| 108 |
unsigned int row; |
| 109 |
for (row = 0; row < pamP->height; ++row) { |
| 110 |
unsigned int col; |
| 111 |
pnm_readpamrow( pamP, tuplerow ); |
| 112 |
for (col = 0; col < pamP->width; ++col) { |
| 113 |
pm_writechar(avsFileP, THIS_SAMPLE_CHAR(3)); |
| 114 |
pm_writechar(avsFileP, THIS_SAMPLE_CHAR(0)); |
| 115 |
pm_writechar(avsFileP, THIS_SAMPLE_CHAR(1)); |
| 116 |
pm_writechar(avsFileP, THIS_SAMPLE_CHAR(2)); |
| 117 |
} |
| 118 |
} |
| 119 |
} break; |
| 120 |
|
| 121 |
default: |
| 122 |
pm_error("Unrecognized PAM depth %u. We understand only " |
| 123 |
"1, 2, 3, and 4", pamP->depth); |
| 124 |
break; |
| 125 |
} |
| 126 |
pnm_freepamrow(tuplerow); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
int |
| 132 |
main(int argc, const char *argv[]) { |
| 133 |
struct pam inPam; |
| 134 |
const char * inputFilename; |
| 135 |
FILE * inFileP; |
| 136 |
|
| 137 |
pm_proginit(&argc, argv); |
| 138 |
|
| 139 |
inputFilename = (argc > 1) ? argv[1] : "-"; |
| 140 |
|
| 141 |
inFileP = pm_openr(inputFilename); |
| 142 |
|
| 143 |
pnm_readpaminit(inFileP, &inPam, PAM_STRUCT_SIZE(tuple_type)); |
| 144 |
|
| 145 |
produceAvs(&inPam, stdout); |
| 146 |
|
| 147 |
pm_closer(inFileP); |
| 148 |
|
| 149 |
return 0; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|