20 #define JPEGQUALITY 95
25 puts(
"Apply an inpainting mask, P. Getreuer, 2012\n\n"
26 "Syntax: applymask <input> <mask> <output>\n");
27 puts(
"where <input>, <mask>, and <output> are "
30 " applymask input.bmp mask.bmp output.bmp\n");
34 void ApplyMask(
unsigned char *Image,
int Width,
int Height,
int NumChannels,
35 const unsigned char *Mask,
int MaskWidth,
int MaskHeight);
36 int IsGrayscale(
const unsigned char *Image,
int Width,
int Height);
39 int main(
int argc,
char **argv)
41 const char *InputFile, *MaskFile, *OutputFile;
42 unsigned char *Image = NULL, *Mask = NULL;
43 int Width, Height, NumChannels, MaskWidth, MaskHeight, Status = 1;
56 if(!(Image = (
unsigned char *)
ReadImage(&Width, &Height,
57 InputFile, IMAGEIO_RGB | IMAGEIO_PLANAR | IMAGEIO_U8))
58 || !(Mask = (
unsigned char *)
ReadImage(&MaskWidth, &MaskHeight,
59 MaskFile, IMAGEIO_GRAYSCALE | IMAGEIO_U8)))
62 NumChannels =
IsGrayscale(Image, Width, Height) ? 1 : 3;
65 ApplyMask(Image, Width, Height, NumChannels, Mask, MaskWidth, MaskHeight);
68 if(!
WriteImage(Image, Width, Height, OutputFile,
69 ((NumChannels == 1) ? IMAGEIO_GRAYSCALE : IMAGEIO_RGB)
72 fprintf(stderr,
"Error writing to \"%s\".\n", OutputFile);
95 void ApplyMask(
unsigned char *Image,
int Width,
int Height,
int NumChannels,
96 const unsigned char *Mask,
int MaskWidth,
int MaskHeight)
98 const long NumPixels = ((long)Width) * ((long)Height);
99 long x, y, MinWidth, MinHeight, Channel;
101 MinWidth = (MaskWidth < Width) ? MaskWidth : Width;
102 MinHeight = (MaskHeight < Height) ? MaskHeight : Height;
104 for(Channel = 0; Channel < NumChannels; Channel++, Image += NumPixels)
105 for(y = 0; y < MinHeight; y++)
106 for(x = 0; x < MinWidth; x++)
107 if(Mask[x + MaskWidth*y] >= 128)
108 Image[x + Width*y] = 128;
113 int IsGrayscale(
const unsigned char *Image,
int Width,
int Height)
115 const long NumPixels = ((long)Width) * ((long)Height);
116 const unsigned char *Red = Image;
117 const unsigned char *Green = Image + NumPixels;
118 const unsigned char *Blue = Image + 2*NumPixels;
121 for(n = 0; n < NumPixels; n++)
122 if(Red[n] != Green[n] || Red[n] != Blue[n])