Debian Codegraph @

converter/other/zeisstopnm.c

raw
1 /* zeisstopnm.c - convert a Zeiss confocal image into a portable anymap
2 **
3 ** Copyright (C) 1993 by Oliver Trepte, [email protected]
4 **
5 ** Derived from the pbmplus package,
6 ** Copyright (C) 1989 by Jef Poskanzer.
7 **
8 ** Permission to use, copy, modify, and distribute this software and its
9 ** documentation for any purpose and without fee is hereby granted, provided
10 ** that the above copyright notice appear in all copies and that both that
11 ** copyright notice and this permission notice appear in supporting
12 ** documentation. This software is provided "as is" without express or
13 ** implied warranty.
14 **
15 **
16 ** This conversion utility is based on a mail from Keith Bartels to
17 ** the confocal mail group ([email protected]) in June 1993.
18 **
19 ** I'm including here a description of the Zeiss confocal image format. I
20 ** obtained this over the phone on 4-10-1991 form a Zeiss Engineer, and from
21 ** what I hear, it is probably not correct for the new scopes.
22 ** Zeiss puts its header information and the end of the file, so I call it
23 ** a tailer. This is nice because most conversions programs that work with raw
24 ** image files will read the image simply ignore the tailer. The file contains:
25 **
26 ** The image data: NxN 1 byte pixels (where N is the number of pixels in a
27 ** scan line) in a standard raw rastering order.
28 **
29 ** The tailer contains:
30 ** 256 bytes: the blue Look-Up-Table (LUT)
31 ** 256 bytes: the red LUT
32 ** 256 bytes: the green LUT
33 ** 8 bytes: empty
34 ** 2 bytes: no. of columns in the image. hi-byte, low-byte
35 ** 2 bytes: no. of rows in the image.
36 ** 2 bytes: x-position of upper left pixel (0 for 512x512 images)
37 ** 2 bytes: y-position of upper left pixel (0 for 512x512 images)
38 ** 16 bytes: empty
39 ** 32 bytes: test from upper right corner of image, ASCII.
40 ** 128 bytes: text from the bottom two rows of the screen, ASCII.
41 ** 64 bytes: reserved
42 ** -----------
43 ** 1024 bytes TOTAL
44 **
45 ** So, image files contain NxN + 1024 bytes.
46 **
47 ** Keith Bartels
48 ** [email protected]
49 **
50 */
51
52 #include "pnm.h"
53
54 int
55 main(int argc, char* argv[]) {
56
57 FILE* ifp;
58 int argn, row, i;
59 int col;
60 int rows=0, cols=0;
61 int format = 0;
62 xel* xelrow;
63 xel* xP;
64 char* buf = NULL;
65 unsigned char *lutr, *lutg, *lutb;
66 long int nread = 0;
67 unsigned char* byteP;
68 const char* const usage = "[-pgm|-ppm] [Zeissfile]";
69
70
71 pnm_init( &argc, argv );
72
73 argn = 1;
74
75 while ( argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0' )
76 {
77 if ( pm_keymatch( argv[argn], "-pgm", 3 ) )
78 {
79 if ( argn >= argc )
80 pm_usage( usage );
81 format = PGM_TYPE;
82 }
83 else if ( pm_keymatch( argv[argn], "-ppm", 3 ) )
84 {
85 if ( argn >= argc )
86 pm_usage( usage );
87 format = PPM_TYPE;
88 }
89 else
90 pm_usage( usage );
91 ++argn;
92 }
93
94 if ( argn < argc )
95 {
96 ifp = pm_openr( argv[argn] );
97 ++argn;
98 }
99 else
100 ifp = stdin;
101
102 if ( argn != argc )
103 pm_usage( usage );
104
105 /* Read the image to a buffer */
106
107 buf = pm_read_unknown_size( ifp, &nread );
108
109 /* Check the format of the file */
110
111 if (nread <=1024)
112 pm_error( "Input file not in Zeiss format (too small)" );
113
114 lutg = (unsigned char *)buf+(nread-1024+512);
115 lutr = (unsigned char *)buf+(nread-1024+256);
116 lutb = (unsigned char *)buf+(nread-1024);
117
118 cols = ((unsigned char) buf[nread-1024+768+8]) +
119 (((unsigned char) buf[nread-1024+768+9]) << 8);
120 rows = ((unsigned char) buf[nread-1024+768+10]) +
121 (((unsigned char) buf[nread-1024+768+11]) << 8);
122
123 if ( cols <= 0 )
124 pm_error( "invalid cols: %d", cols );
125 if ( rows <= 0 )
126 pm_error( "invalid rows: %d", rows );
127
128 if (cols*rows != nread-1024)
129 pm_error( "Hmm, %d rows, %d cols, %ld total image size",
130 rows, cols, nread-1024);
131
132 /* Choose pgm or ppm */
133 /* If the LUTs all contain 0,1,2,3,4..255, it is a pgm file */
134
135 for (i=0; i<256 && format==0; i++)
136 if (lutr[i] != i || lutg[i] != i || lutb[i] != i)
137 format = PPM_TYPE;
138
139 if (format == 0)
140 format = PGM_TYPE;
141
142 pnm_writepnminit( stdout, cols, rows, 255, format, 0 );
143 xelrow = pnm_allocrow( cols );
144 byteP = (unsigned char *) buf;
145
146 switch ( PNM_FORMAT_TYPE(format) )
147 {
148 case PGM_TYPE:
149 pm_message( "writing PGM file, %d rows %d columns", rows, cols );
150 break;
151
152 case PPM_TYPE:
153 pm_message( "writing PPM file, %d rows %d columns", rows, cols );
154 break;
155
156 default:
157 pm_error( "shouldn't happen" );
158 }
159
160 for ( row = 0; row < rows; ++row )
161 {
162 switch ( PNM_FORMAT_TYPE(format) )
163 {
164 case PGM_TYPE:
165 for ( col = 0, xP = xelrow; col < cols; ++col, ++xP, ++byteP )
166 PNM_ASSIGN1( *xP, *byteP );
167 break;
168
169 case PPM_TYPE:
170 for ( col = 0, xP = xelrow; col < cols; ++col, ++xP, ++byteP )
171 PPM_ASSIGN( *xP, lutr[*byteP], lutg[*byteP], lutb[*byteP] );
172 break;
173
174 default:
175 pm_error( "shouldn't happen" );
176 }
177
178 pnm_writepnmrow( stdout, xelrow, cols, 255, format, 0 );
179 }
180
181 free( buf );
182 pm_close( stdout );
183
184 exit( 0 );
185 }
186
187
188