14 const image NullImage = {NULL, 0, 0, 0};
17 int AllocImageObj(
image *f,
int Width,
int Height,
int NumChannels)
22 if(!(f->
Data = (num *)malloc(
sizeof(num)*
23 (((
size_t)Width)*((
size_t)Height))*NumChannels)))
36 void FreeImageObj(
image f)
43 int ReadImageObj(
image *f,
const char *FileName)
46 IMAGEIO_NUM | IMAGEIO_RGB | IMAGEIO_PLANAR)))
57 int ReadImageObjGrayscale(
image *f,
const char *FileName)
60 IMAGEIO_NUM | IMAGEIO_GRAYSCALE | IMAGEIO_PLANAR)))
71 int WriteImageObj(
image f,
const char *FileName,
int JpegQuality)
73 if(!f.
Data || !FileName)
80 IMAGEIO_NUM | IMAGEIO_GRAYSCALE | IMAGEIO_PLANAR, JpegQuality);
83 IMAGEIO_NUM | IMAGEIO_RGB | IMAGEIO_PLANAR, JpegQuality);
86 IMAGEIO_NUM | IMAGEIO_RGBA | IMAGEIO_PLANAR, JpegQuality);
96 const long NumPixels = ((long)Width) * ((long)Height);
97 const num *Red = Data;
98 const num *Green = Data + NumPixels;
99 const num *Blue = Data + 2*NumPixels;
102 for(n = 0; n < NumPixels; n++)
103 if(Red[n] != Green[n] || Red[n] != Blue[n])
118 int GetStrToken(
char *Token,
const char *Start,
int MaxLength,
const char *Delim)
120 int NumChars = strcspn(Start, Delim);
121 int NumCopy = (NumChars <= MaxLength) ? NumChars : MaxLength;
123 strncpy(Token, Start, NumCopy);
124 Token[NumCopy] =
'\0';
144 double Accum = 0, Div = 1, Exponent = 0;
145 int i = 0, Sign = 1, ExponentSign = 1;
152 while(isspace(String[i]))
160 else if(String[i] ==
'+')
164 if(isdigit(c = String[i]))
169 while(isdigit(c = String[++i]))
170 Accum = 10*Accum + (c -
'0');
175 while(isdigit(c = String[++i]))
178 Accum += (c -
'0')/Div;
182 if(c ==
'e' || c ==
'E')
191 else if(String[i] ==
'+')
195 if(isdigit(c = String[i]))
199 while(isdigit(c = String[++i]))
200 Exponent = 10*Exponent + (c -
'0');
202 Exponent *= ExponentSign;
203 Accum = Accum * pow(10, Exponent);
249 char *TokenBuf,
int MaxLength,
int k,
const char *Start,
250 int argc,
const char *argv[],
const char *Delimiters)
252 int TokLen, kread = k;
254 *Param = *Value = NULL;
255 TokLen =
GetStrToken(TokenBuf, Start, MaxLength, Delimiters);
257 if(TokLen > MaxLength)
265 if(Start[TokLen] && Start[TokLen + 1])
266 *Value = &Start[TokLen + 1];
268 for(kread = k + 1; kread < argc; kread++)
271 else if(!strchr(Delimiters, argv[kread][0]))
273 *Value = &argv[kread][0];
276 else if(argv[kread][1])
278 *Value = &argv[kread][1];
288 int CliGetNum(num *Value,
const char *String,
const char *Param)
295 fprintf(stderr,
"Null pointer.\n");
300 fprintf(stderr,
"Expected a number for %s.\n", Param);
309 fprintf(stderr,
"Invalid syntax \"%s\".\n", String);
314 *Value = (num)DoubleValue;
326 long DestNumEl = 0, DestCapacity = 64;
327 int c, Line = 1, Col = 0, NumRows = 0, NumCols = 0;
334 if(!(File = fopen(FileName,
"rt")))
336 fprintf(stderr,
"Error reading \"%s\":\n", FileName);
337 fprintf(stderr,
"Unable to open file.\n");
342 if(!(Dest = (num *)malloc(
sizeof(num)*DestCapacity)))
352 if(c ==
'\n' || c ==
'\r' || !isspace(c))
354 else if(ferror(File))
356 fprintf(stderr,
"Error reading \"%s\".\n", FileName);
368 if(c ==
'\n' || c ==
'\r')
370 else if(ferror(File))
372 fprintf(stderr,
"Error reading \"%s\".\n", FileName);
378 if(c == EOF || c ==
'\n' || c ==
'\r')
384 else if(NumCols != Col)
387 "Error reading \"%s\" on line %d:\n"
388 "Rows must have a consistent number of elements.\n",
407 if(fscanf(File,
"%lg", &Value) != 1)
410 "Error reading \"%s\" on line %d:\nInvalid number.\n",
416 if(DestNumEl == DestCapacity)
419 DestCapacity += DestCapacity/10 + 1;
421 if(!(Dest = (num *)realloc(Dest,
sizeof(num)*DestCapacity)))
423 fprintf(stderr,
"Memory allocation failed.\n");
428 Dest[DestNumEl++] = (num)Value;
449 int (*RescaleFun)(
image *f))
462 if(!ReadImageObjGrayscale(f, FileName))
464 fprintf(stderr,
"Error reading \"%s\".\n", FileName);
468 if(RescaleFun && !RescaleFun(f))