| 1 |
/* pnmtosir.c - read a portable anymap and produce a Solitaire Image Recorder |
| 2 |
** file (MGI TYPE 11 or MGI TYPE 17) |
| 3 |
** |
| 4 |
** Copyright (C) 1991 by Marvin Landis |
| 5 |
** |
| 6 |
** Permission to use, copy, modify, and distribute this software and its |
| 7 |
** documentation for any purpose and without fee is hereby granted, provided |
| 8 |
** that the above copyright notice appear in all copies and that both that |
| 9 |
** copyright notice and this permission notice appear in supporting |
| 10 |
** documentation. This software is provided "as is" without express or |
| 11 |
** implied warranty. |
| 12 |
*/ |
| 13 |
|
| 14 |
#include <stdbool.h> |
| 15 |
#include "pnm.h" |
| 16 |
|
| 17 |
#define MAXCOLORS 256 |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
int |
| 22 |
main(int argc, const char * argv[]) { |
| 23 |
|
| 24 |
FILE * ifP; |
| 25 |
xel ** xels; |
| 26 |
int rows, cols, format; |
| 27 |
unsigned int n; |
| 28 |
bool isGrayscale; |
| 29 |
xelval maxval; |
| 30 |
unsigned short Header[10]; |
| 31 |
unsigned short LutHeader[5]; |
| 32 |
unsigned short Lut[1024]; |
| 33 |
|
| 34 |
pm_proginit(&argc, argv); |
| 35 |
|
| 36 |
if (argc-1 > 1) |
| 37 |
pm_error("There is only one possible argument: the input file. " |
| 38 |
"You specified %d", argc-1); |
| 39 |
|
| 40 |
if (argc-1 > 0) { |
| 41 |
const char * const inputFileName = argv[1]; |
| 42 |
ifP = pm_openr(inputFileName); |
| 43 |
} else { |
| 44 |
ifP = stdin; |
| 45 |
} |
| 46 |
|
| 47 |
xels = pnm_readpnm(ifP, &cols, &rows, &maxval, &format); |
| 48 |
|
| 49 |
/* Figure out the colormap. */ |
| 50 |
switch (PNM_FORMAT_TYPE(format) ) { |
| 51 |
case PPM_TYPE: |
| 52 |
isGrayscale = false; |
| 53 |
pm_message("Writing a 24-bit SIR format (MGI TYPE 11)"); |
| 54 |
break; |
| 55 |
|
| 56 |
case PGM_TYPE: |
| 57 |
isGrayscale = true; |
| 58 |
pm_message("Writing a grayscale SIR format (MGI TYPE 17)"); |
| 59 |
break; |
| 60 |
|
| 61 |
default: |
| 62 |
isGrayscale = true; |
| 63 |
pm_message("Writing a monochrome SIR format (MGI TYPE 17)"); |
| 64 |
break; |
| 65 |
} |
| 66 |
|
| 67 |
/* Set up the header. */ |
| 68 |
Header[0] = 0x3a4f; |
| 69 |
Header[1] = 0; |
| 70 |
if (isGrayscale) |
| 71 |
Header[2] = 17; |
| 72 |
else |
| 73 |
Header[2] = 11; |
| 74 |
Header[3] = cols; |
| 75 |
Header[4] = rows; |
| 76 |
Header[5] = 0; |
| 77 |
Header[6] = 1; |
| 78 |
Header[7] = 6; |
| 79 |
Header[8] = 0; |
| 80 |
Header[9] = 0; |
| 81 |
for (n = 0; n < 10; n++) |
| 82 |
pm_writelittleshort(stdout,Header[n]); |
| 83 |
for (n = 10; n < 256; n++) |
| 84 |
pm_writelittleshort(stdout,0); |
| 85 |
|
| 86 |
/* Create color map */ |
| 87 |
LutHeader[0] = 0x1524; |
| 88 |
LutHeader[1] = 0; |
| 89 |
LutHeader[2] = 5; |
| 90 |
LutHeader[3] = 256; |
| 91 |
LutHeader[4] = 256; |
| 92 |
for (n = 0; n < 5; ++n) |
| 93 |
pm_writelittleshort(stdout,LutHeader[n]); |
| 94 |
for (n = 5; n < 256; ++n) |
| 95 |
pm_writelittleshort(stdout,0); |
| 96 |
|
| 97 |
for (n = 0; n < 256; ++n) { |
| 98 |
unsigned int m; |
| 99 |
for (m = 0; m < 3; ++m) |
| 100 |
Lut[n * 4 + m] = n << 8; |
| 101 |
|
| 102 |
Lut[n * 4 + 3] = 0; |
| 103 |
/* Clear to ensure repeatable output, suppress Valgrind error */ |
| 104 |
} |
| 105 |
|
| 106 |
for (n = 0; n < 1024; ++n) |
| 107 |
pm_writelittleshort(stdout,Lut[n]); |
| 108 |
|
| 109 |
/* Finally, write out the data. */ |
| 110 |
switch (PNM_FORMAT_TYPE(format)) { |
| 111 |
case PPM_TYPE: { |
| 112 |
unsigned int row; |
| 113 |
for (row = 0; row < rows; ++row) { |
| 114 |
unsigned int col; |
| 115 |
for (col = 0; col < cols; ++col) { |
| 116 |
unsigned char const ub = |
| 117 |
(char) (PPM_GETR(xels[row][col]) * (255 / maxval)); |
| 118 |
fputc(ub, stdout); |
| 119 |
} |
| 120 |
} |
| 121 |
for (row = 0; row < rows; ++row) { |
| 122 |
unsigned int col; |
| 123 |
for (col = 0; col < cols; ++col) { |
| 124 |
unsigned const char ub = |
| 125 |
(char) (PPM_GETG(xels[row][col]) * (255 / maxval)); |
| 126 |
fputc(ub, stdout); |
| 127 |
} |
| 128 |
} |
| 129 |
for (row = 0; row < rows; ++row) { |
| 130 |
unsigned int col; |
| 131 |
for (col = 0; col < cols; ++col) { |
| 132 |
unsigned const char ub = |
| 133 |
(char) (PPM_GETB(xels[row][col]) * (255 / maxval)); |
| 134 |
fputc(ub, stdout); |
| 135 |
} |
| 136 |
} |
| 137 |
} break; |
| 138 |
|
| 139 |
default: { |
| 140 |
unsigned int row; |
| 141 |
for (row = 0; row < rows; ++row) { |
| 142 |
unsigned int col; |
| 143 |
for (col = 0; col < cols; ++col) { |
| 144 |
unsigned long const val = PNM_GET1(xels[row][col]); |
| 145 |
unsigned const char ub = (char) (val * (255 / maxval)); |
| 146 |
fputc(ub, stdout); |
| 147 |
} |
| 148 |
} |
| 149 |
} break; |
| 150 |
} |
| 151 |
|
| 152 |
pm_close(ifP); |
| 153 |
|
| 154 |
return 0; |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|