Debian Codegraph @

converter/pgm/pgmtofs.c

raw
1 /* pgmtofs.c - convert portable graymap to Usenix FaceSaver(tm) format
2 **
3 ** Copyright (C) 1991 by Jef Poskanzer.
4 **
5 ** Permission to use, copy, modify, and distribute this software and its
6 ** documentation for any purpose and without fee is hereby granted, provided
7 ** that the above copyright notice appear in all copies and that both that
8 ** copyright notice and this permission notice appear in supporting
9 ** documentation. This software is provided "as is" without express or
10 ** implied warranty.
11 */
12
13 #include "pgm.h"
14
15 static void putinit ( int const cols, int const rows, int const bps );
16 static void putitem ( void );
17 static void putgray ( gray const g );
18 static void putrest ( void );
19
20 int
21 main(int argc, const char * argv[]) {
22 FILE* ifp;
23 gray** grays;
24 gray* gP;
25 int argn, rows, cols, bps, padright, row, col;
26 gray maxval, nmaxval;
27 const char* const usage = "[pgmfile]";
28
29
30 pm_proginit(&argc, argv);
31
32 argn = 1;
33
34 if ( argn < argc )
35 {
36 ifp = pm_openr( argv[argn] );
37 ++argn;
38 }
39 else
40 {
41 ifp = stdin;
42 }
43
44 if ( argn != argc )
45 pm_usage( usage );
46
47 grays = pgm_readpgm( ifp, &cols, &rows, &maxval );
48 pm_close( ifp );
49
50 /* Figure out bps. */
51 bps = pm_maxvaltobits( (int) maxval );
52 if ( bps > 2 && bps < 4 )
53 bps = 4;
54 else if ( bps > 4 && bps < 8 )
55 bps = 8;
56 else if ( bps > 8 )
57 pm_error(
58 "maxval of %d is too large for FaceSaver(tm)", maxval );
59 nmaxval = pm_bitstomaxval( bps );
60
61 /* Compute padding to round cols * bps up to the nearest multiple of 8. */
62 padright = ( ( cols * bps + 7 ) / 8 ) * 8 - cols * bps;
63
64 putinit( cols, rows, bps );
65 for ( row = rows - 1; row >= 0; --row )
66 {
67 for ( col = 0, gP = grays[row]; col < cols; ++col, ++gP )
68 {
69 if ( maxval != nmaxval )
70 *gP = (int) *gP * nmaxval / maxval;
71 putgray( *gP );
72 }
73 for ( col = 0; col < padright; ++col )
74 putgray( 0 );
75 }
76
77 putrest( );
78
79 exit( 0 );
80 }
81
82
83
84 static int bitspersample, item, bitsperitem, bitshift, itemsperline, items;
85
86 static void
87 putinit(int const cols,
88 int const rows,
89 int const bps)
90 {
91 printf( "FirstName: \n" );
92 printf( "LastName: \n" );
93 printf( "E-mail: \n" );
94 printf( "Telephone: \n" );
95 printf( "Company: \n" );
96 printf( "Address1: \n" );
97 printf( "Address2: \n" );
98 printf( "CityStateZip: \n" );
99 printf( "Date: \n" );
100 printf( "PicData: %d %d %d\n", cols, rows, bps );
101 printf( "Image: %d %d %d\n", cols, rows, bps );
102 printf( "\n" );
103
104 bitspersample = bps;
105 itemsperline = items = 0;
106 item = 0;
107 bitsperitem = 0;
108 bitshift = 8 - bitspersample;
109 }
110
111
112
113 static void
114 putitem( )
115 {
116 const char* const hexits = "0123456789abcdef";
117
118 if ( itemsperline == 30 )
119 {
120 putchar( '\n' );
121 itemsperline = 0;
122 }
123 putchar( hexits[item >> 4] );
124 putchar( hexits[item & 15] );
125 ++itemsperline;
126 ++items;
127 item = 0;
128 bitsperitem = 0;
129 bitshift = 8 - bitspersample;
130 }
131
132
133
134 static void
135 putgray( gray const g )
136 {
137 if ( bitsperitem == 8 )
138 putitem( );
139 item += g << bitshift;
140 bitsperitem += bitspersample;
141 bitshift -= bitspersample;
142 }
143
144
145
146 static void
147 putrest( )
148 {
149 if ( bitsperitem > 0 )
150 putitem( );
151 printf( "\n" );
152 }
153
154
155