Image Demosaicking with Contour Stencils
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
psio.c
Go to the documentation of this file.
1 
16 #include "psio.h"
17 
19 #define FILE_BUFFER_CAPACITY (1024*4)
20 
21 
32 FILE *PsOpen(const char *FileName,
33  int XMin, int YMin, int XMax, int YMax)
34 {
35  FILE *File = NULL;
36 
37  if(!(File = fopen(FileName, "wb")))
38  return NULL;
39 
40  /* Tell File to use buffering */
41  setvbuf(File, 0, _IOFBF, FILE_BUFFER_CAPACITY);
42 
43  /* Adjust bounding box so that bottom-left is always (0,0) */
44  XMax -= XMin;
45  YMax -= YMin;
46 
47  if(fprintf(File,
48  "%%!PS-Adobe-2.0\n"
49  "%%%%BoundingBox: 0 0 %d %d\n"
50  "<< /PageSize [%d %d] >> setpagedevice\n", XMax, YMax, XMax, YMax) < 0
51  || ((XMin != 0 || YMin != 0) && fprintf(File,
52  "gsave %d %d translate\n", -XMin, -YMin) < 0)
53  || ferror(File))
54  {
55  fclose(File);
56  File = NULL;
57  }
58 
59  return File;
60 }
61 
62 
70 int PsClose(FILE *File)
71 {
72  if(!File
73  || fprintf(File, "showpage\n") < 0
74  || ferror(File)
75  || fclose(File))
76  return 0;
77 
78  return 1;
79 }
80 
81 
110 int PsSetDistillerParams(FILE *File, const char *Params)
111 {
112  if(!File
113  || fprintf(File,
114  "systemdict /setdistillerparams known {\n"
115  "<< %s >> setdistillerparams\n"
116  "} if\n", Params) < 0)
117  return 0;
118  else
119  return 1;
120 }
121 
122 
129 static int WriteASCII85(FILE *File, const uint8_t *Data, int NumBytes)
130 {
131  unsigned long Tuple, Plain[4];
132  unsigned int Encoded[5];
133  int i, k, LineCount, Padding;
134 
135 
136  /* Write ASCII85-encoded data */
137  for(i = 0, LineCount = 0; i < NumBytes; i += 4)
138  {
139  for(k = 0; k < 4; k++) /* Get four bytes */
140  Plain[k] = Data[i + k];
141 
142  Tuple = (Plain[0] << 24) | (Plain[1] << 16)
143  | (Plain[2] << 8) | Plain[3];
144 
145  for(k = 4; k >= 0; k--) /* Convert to radix 85 */
146  {
147  Encoded[k] = Tuple % 85;
148  Tuple /= 85;
149  }
150 
151  for(k = 0; k < 5; k++) /* Write ASCII85 tuple */
152  fputc(33 + Encoded[k], File);
153 
154  /* Periodically emit newlines */
155  if(++LineCount >= 15)
156  {
157  LineCount = 0;
158 
159  if(fprintf(File, "\n") < 0)
160  return 0;
161  }
162  }
163 
164  /* Write final tuple */
165  if(i < NumBytes)
166  {
167  for(k = 0; i + k < NumBytes; k++)
168  Plain[k] = Data[i + k];
169 
170  for(Padding = 0; k < 4; k++, Padding++)
171  Plain[k] = 0;
172 
173  Tuple = (Plain[0] << 24) | (Plain[1] << 16)
174  | (Plain[2] << 8) | Plain[3];
175 
176  for(k = 4; k >= 0; k--) /* Convert to radix 85 */
177  {
178  Encoded[k] = Tuple % 85;
179  Tuple /= 85;
180  }
181 
182  for(k = 0; k < 5 - Padding; k++) /* Write ASCII85 tuple */
183  fputc(33 + Encoded[k], File);
184 
185  if(++LineCount >= 15
186  && fprintf(File, "\n") < 0)
187  return 0;
188  }
189 
190  if(fprintf(File, "~>\n") < 0 || ferror(File))
191  return 0;
192 
193  return 1;
194 }
195 
196 
215 int PsWriteGrayImage(FILE *File,
216  const uint8_t *Image, int Width, int Height)
217 {
218  /* Specify ASCII85 sRGB 8-bit color image data */
219  if(!File || !Image || fprintf(File,
220  "gsave\n"
221  "/DeviceGray setcolorspace\n"
222  "0 %d translate %d %d scale\n"
223  "<< /ImageType 1\n"
224  " /Width %d\n"
225  " /Height %d\n"
226  " /ImageMatrix [%d 0 0 -%d 0 0]\n"
227  " /BitsPerComponent 8\n"
228  " /Decode [0 1]\n"
229  " /DataSource currentfile /ASCII85Decode filter\n"
230  " /Interpolate false\n"
231  ">> image\n",
232  Height, Width, Height,
233  Width, Height, Width, Height) < 0
234  || !WriteASCII85(File, Image, Width*Height)
235  || fprintf(File, "grestore\n") < 0)
236  return 0;
237  else
238  return 1;
239 }
240 
241 
260 int PsWriteColorImage(FILE *File,
261  const uint8_t *Image, int Width, int Height)
262 {
263  /* Specify ASCII85 sRGB 8-bit color image data */
264  if(!File || !Image || fprintf(File,
265  "gsave\n"
266  "/DeviceRGB setcolorspace\n"
267  "0 %d translate %d %d scale\n"
268  "<< /ImageType 1\n"
269  " /Width %d\n"
270  " /Height %d\n"
271  " /ImageMatrix [%d 0 0 -%d 0 0]\n"
272  " /BitsPerComponent 8\n"
273  " /Decode [0 1 0 1 0 1]\n"
274  " /DataSource currentfile /ASCII85Decode filter\n"
275  " /Interpolate false\n"
276  ">> image\n",
277  Height, Width, Height,
278  Width, Height, Width, Height) < 0
279  || !WriteASCII85(File, Image, 3*Width*Height)
280  || fprintf(File, "grestore\n") < 0)
281  return 0;
282  else
283  return 1;
284 }