Debian Codegraph @

converter/pbm/pbmtomgr.c

raw
1 /* pbmtomgr.c - read a PBM image and produce a MGR bitmap
2
3 Copyright information is at end of file.
4
5 You can find MGR and some MGR format test images at
6 ftp://sunsite.unc.edu/pub/Linux/apps/MGR/!INDEX.html
7 */
8
9 #include <assert.h>
10 #include "pbm.h"
11 #include "mgr.h"
12
13
14
15 static void
16 putinit(unsigned int const rows,
17 unsigned int const cols) {
18
19 struct b_header head;
20 size_t writtenCount;
21
22 /* Because of argument restrictions: maximum dimensions: */
23 assert((rows & 0xfff) == rows);
24 assert((cols & 0xfff) == cols);
25
26 head.magic[0] = 'y';
27 head.magic[1] = 'z';
28 head.h_wide = ((cols >> 6) & 0x3f) + ' ';
29 head.l_wide = (cols & 0x3f) + ' ';
30 head.h_high = (( rows >> 6) & 0x3f) + ' ';
31 head.l_high = (rows & 0x3f) + ' ';
32 head.depth = (1 & 0x3f) + ' ';
33 head._reserved = ' ';
34 writtenCount = fwrite(&head, sizeof(head), 1, stdout);
35 if (writtenCount != 1)
36 pm_error("fwrite() failed to write the MGR header.");
37 }
38
39
40
41 int
42 main(int argc,
43 const char * argv[]) {
44
45 FILE * ifP;
46 unsigned char * bitrow;
47 int rows;
48 int cols;
49 int format;
50 unsigned int row;
51 unsigned int bytesPerRow;
52 /* Number of packed bytes (8 columns per byte) in a row. */
53 const char * inputFileName;
54 unsigned int const maxDimension = 4095;
55 /* Dimensions are 2 characters of the header -- 12 bits */
56
57 pm_proginit(&argc, argv);
58
59 if (argc-1 > 1)
60 pm_error("Too many arguments (%u). "
61 "Only argument is optional input file", argc-1);
62 if (argc-1 == 1)
63 inputFileName = argv[1];
64 else
65 inputFileName = "-";
66
67 ifP = pm_openr(inputFileName);
68
69 pbm_readpbminit(ifP, &cols, &rows, &format);
70 if (cols > maxDimension)
71 pm_error("Image width too large: %u (max: %u)", cols, maxDimension);
72 if (rows > maxDimension)
73 pm_error("Image height too large: %u (max: %u)", rows, maxDimension);
74
75 bitrow = pbm_allocrow_packed(cols);
76 bytesPerRow = pbm_packed_bytes(cols);
77
78 putinit(rows, cols);
79
80 for (row = 0; row < rows; ++row) {
81 /* The raster formats are identical.
82 The row end pad bits are set to 0 in mgr.
83 */
84 size_t bytesWritten;
85
86 pbm_readpbmrow_packed(ifP, bitrow, cols, format);
87 pbm_cleanrowend_packed(bitrow, cols);
88
89 bytesWritten = fwrite(bitrow, 1, bytesPerRow, stdout);
90 if (bytesWritten != bytesPerRow )
91 pm_error("fwrite() failed to write MGR bitmap "
92 "to Standard Output.");
93 }
94 pm_close(ifP);
95 return 0;
96 }
97
98
99
100 /* 2006.10 (afu)
101 Changed bitrow from plain to raw, read function from pbm_readpbmrow() to
102 pbm_readpbmrow_packed(), write function from putc() to fwrite().
103
104 Retired bitwise transformation functions.
105
106 Produces only new style bitmap (8 bit padding.) See mgrtopbm.c .
107 */
108
109 /*
110 ** Copyright (C) 1989 by Jef Poskanzer.
111 **
112 ** Permission to use, copy, modify, and distribute this software and its
113 ** documentation for any purpose and without fee is hereby granted, provided
114 ** that the above copyright notice appear in all copies and that both that
115 ** copyright notice and this permission notice appear in supporting
116 ** documentation. This software is provided "as is" without express or
117 ** implied warranty.
118 */