Image Demosaicking with Contour Stencils
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
displaycontours.c
Go to the documentation of this file.
1 
15 #include <stdio.h>
16 #include <math.h>
17 #include "basic.h"
18 #include "imageio.h"
19 #include "dmbilinear.h"
20 #include "mstencils.h"
21 #include "psio.h"
22 
24 #define ROUNDCLAMP(X) (((X) < 0) ? 0 : (((X) > 1) ? 255 : floor(X*255 + 0.5f)))
25 
26 
59 int DisplayContours(const float *Image, int Width, int Height,
60  int RedX, int RedY, const char *OutputFile)
61 {
62  const int NumPixels = Width*Height;
63  FILE *File = NULL;
64  float *Mosaic = NULL;
65  int *Stencil = NULL;
66  uint8_t *ImageU8 = NULL;
67  float Temp, x1, y1, x2, y2;
68  int i, x, y, DimScale = 5, Success = 0;
69 
70  if(!(Mosaic = (float *)Malloc(sizeof(float)*NumPixels))
71  || !(Stencil = (int *)Malloc(sizeof(int)*NumPixels))
72  || !(ImageU8 = (uint8_t *)Malloc(sizeof(uint8_t)*3*NumPixels))
73  /* Start an EPS file with DimScale*Width by DimScale*Height canvas */
74  || !(File = PsOpen(OutputFile, 0, 0,
75  DimScale*Width, DimScale*Height)))
76  goto Catch;
77 
78  CfaFlatten(Mosaic, Image, Width, Height, RedX, RedY);
79  /* Estimate the contour orientations */
80  FitMosaicedStencils(Stencil, Mosaic, Width, Height, RedX, RedY);
81 
82  /* Lighten the image according to 0.5 + 0.5*Image and convert to
83  unsigned 8-bit data. */
84  for(i = 0; i < NumPixels; i++)
85  {
86  Temp = 0.5f + 0.5f*Image[i];
87  ImageU8[3*i + 0] = (uint8_t)ROUNDCLAMP(Temp);
88  Temp = 0.5f + 0.5f*Image[i + NumPixels];
89  ImageU8[3*i + 1] = (uint8_t)ROUNDCLAMP(Temp);
90  Temp = 0.5f + 0.5f*Image[i + 2*NumPixels];
91  ImageU8[3*i + 2] = (uint8_t)ROUNDCLAMP(Temp);
92  }
93 
94  /* Set distiller parameters for PDF conversion */
95  if(!PsSetDistillerParams(File,"/AutoFilterColorImages false "
96  "/ColorImageFilter /FlateEncode"))
97  goto Catch;
98  /* Define PS macros and rescale canvas by DimScale */
99  fprintf(File,"/bdef {bind def} bind def\n"
100  "/m1 {moveto rlineto stroke} bdef\n"
101  "0.08 setlinewidth\n"
102  "%d %d scale\n", DimScale, DimScale);
103 
104  /* Write the image data ImageU8 into the EPS file */
105  if(!PsWriteColorImage(File, ImageU8, Width, Height))
106  goto Catch;
107 
108  /* Draw the contour orientations on top of the image */
109  for(y = 0, i = 0; y < Height; y++)
110  for(x = 0; x < Width; x++, i++)
111  {
112  x2 = (float)(cos(Stencil[i]*M_PI_8)*0.45);
113  y2 = (float)(sin(Stencil[i]*M_PI_8)*0.45);
114  x1 = -x2;
115  y1 = -y2;
116  /* Draw a line from (x + x1 + 0.5, y1 + (Height - y - 0.5))
117  to (x + x2 + 0.5, y2 + (Height - y - 0.5)) */
118  fprintf(File, "%.2f %.2f %.2f %.2f m1\n",
119  x2 - x1, y2 - y1,
120  x + x1 + 0.5f, y1 + (Height - y - 0.5f));
121  }
122 
123  /* Finish writing the EPS file */
124  if(!PsClose(File))
125  goto Catch;
126 
127  Success = 1;
128 Catch:
129  Free(ImageU8);
130  Free(Stencil);
131  Free(Mosaic);
132  return Success;
133 }