?? image.c
字號:
static void *readBinaryCooked(FILE *inFile, ImageHeader **readimHeader){ unsigned width, height; boolean failed; BinaryImage im; int x, y; /* * File format: * P1 <width> <height> * bunches of 0<ws> and 1<ws>, suitable for reading with gettoken. */ width = gettoken(inFile, &failed); if (failed) { return((void *)NULL); } height = gettoken(inFile, &failed); if (failed) { return((void *)NULL); } im = (BinaryImage)imNew(IMAGE_BINARY, width, height); if (im == (BinaryImage)NULL) { return((void *)NULL); } for (y = 0; y < height; y++) { for (x = 0; x < width; x++) { /* Can't use gettoken - may not be separating whitespace */ imRef(im, x, y) = getpbmtoken(inFile, &failed); if (failed) { imFree(im); return((void *)NULL); } } } *readimHeader = im->header; return(im); }static void *readGrayCooked(FILE *inFile, ImageHeader **readimHeader){ unsigned width, height; unsigned maxval; boolean failed; GrayImage im; int x, y; /* * File format: * P2 <width> <height> <maxval> * bunches of <value><ws> where 0 <= <value> <= maxval, suitable for * reading with gettoken. * If maxval is not 255, we'll have to rescale the values; this will * be slow. */ width = gettoken(inFile, &failed); if (failed) { return((void *)NULL); } height = gettoken(inFile, &failed); if (failed) { return((void *)NULL); } maxval = gettoken(inFile, &failed); if (failed) { return((void *)NULL); } im = (GrayImage)imNew(IMAGE_GRAY, width, height); if (im == (GrayImage)NULL) { return((void *)NULL); } if (maxval == COLRNG - 1) { /* Special case the nice case */ for (y = 0; y < height; y++) { for (x = 0; x < width; x++) { imRef(im, x, y) = gettoken(inFile, &failed); if (failed) { imFree(im); return((void *)NULL); } } } } else { /* Have to rescale. */ for (y = 0; y < height; y++) { for (x = 0; x < width; x++) { imRef(im, x, y) = (gettoken(inFile, &failed) * (COLRNG - 1)) / maxval; if (failed) { imFree(im); return((void *)NULL); } } } } *readimHeader = im->header; return(im); }static void *readRGBCooked(FILE *inFile, ImageHeader **readimHeader){ unsigned width, height; unsigned maxval; boolean failed; RGBImage im; int x, y; /* * File format: * P3 <width> <height> <maxval> * bunches of <rvalue><ws><gvalue><ws><bvalue><ws> where * 0 <= <rvalue>, <gvalue>, <bvalue> <= maxval, suitable for * reading with gettoken. * If maxval is not 255, we'll have to rescale the values; this will * be slow. */ width = gettoken(inFile, &failed); if (failed) { return((void *)NULL); } height = gettoken(inFile, &failed); if (failed) { return((void *)NULL); } maxval = gettoken(inFile, &failed); if (failed) { return((void *)NULL); } im = (RGBImage)imNew(IMAGE_RGB, width, height); if (im == (RGBImage)NULL) { return((void *)NULL); } if (maxval == COLRNG - 1) { /* Special case the nice case */ for (y = 0; y < height; y++) { for (x = 0; x < width; x++) { imRef(im, x, y).r = gettoken(inFile, &failed); if (failed) { imFree(im); return((void *)NULL); } imRef(im, x, y).g = gettoken(inFile, &failed); if (failed) { imFree(im); return((void *)NULL); } imRef(im, x, y).b = gettoken(inFile, &failed); if (failed) { imFree(im); return((void *)NULL); } } } } else { /* Have to rescale. */ for (y = 0; y < height; y++) { for (x = 0; x < width; x++) { imRef(im, x, y).r = (gettoken(inFile, &failed) * (COLRNG - 1)) / maxval; if (failed) { imFree(im); return((void *)NULL); } imRef(im, x, y).g = (gettoken(inFile, &failed) * (COLRNG - 1)) / maxval; if (failed) { imFree(im); return((void *)NULL); } imRef(im, x, y).b = (gettoken(inFile, &failed) * (COLRNG - 1)) / maxval; if (failed) { imFree(im); return((void *)NULL); } } } } *readimHeader = im->header; return(im); }static void *readBinary(FILE *inFile, ImageHeader **readimHeader){ unsigned width, height; boolean failed; BinaryImage im; int x, y; int c, bitsleft; /* * File format: * P4 <width> <height> * binary image, packed, high bit is leftmost; rows begin on byte * boundaries. */ width = gettoken(inFile, &failed); if (failed) { return((void *)NULL); } height = gettoken(inFile, &failed); if (failed) { return((void *)NULL); } im = (BinaryImage)imNew(IMAGE_BINARY, width, height); if (im == (BinaryImage)NULL) { return((void *)NULL); } c = EOF; for (y = 0; y < height; y++) { bitsleft = 0; for (x = 0; x < width; x++) { if (bitsleft == 0) { c = getc(inFile); if (c == EOF) { imFree(im); return((void *)NULL); } bitsleft = 8; } /* High bit first */ if (c & (1 << (--bitsleft))) { imRef(im, x, y) = 1; } /* It's already zero by default */ } } *readimHeader = im->header; return(im); }static void *readGray(FILE *inFile, ImageHeader **readimHeader){ unsigned width, height; unsigned maxval; boolean failed; GrayImage im; int x, y; /* * File format: * P5 <width> <height> <maxval> * image data in reading order, one byte per pixel. * If maxval is not 255, we'll have to rescale the values; this will * be slow. */ width = gettoken(inFile, &failed); if (failed) { return((void *)NULL); } height = gettoken(inFile, &failed); if (failed) { return((void *)NULL); } maxval = gettoken(inFile, &failed); if (failed) { return((void *)NULL); } im = (GrayImage)imNew(IMAGE_GRAY, width, height); if (im == (GrayImage)NULL) { return((void *)NULL); } /* Inhale everything. */ if (fread((char *)(im->data[0]), sizeof(unsigned char), (width * height), inFile) != (width * height)) { imFree(im); return((void *)NULL); } if (maxval != COLRNG - 1) { /* Have to rescale. */ for (y = 0; y < height; y++) { for (x = 0; x < width; x++) { imRef(im, x, y) = (imRef(im, x, y) * (COLRNG - 1)) / maxval; } } } *readimHeader = im->header; return(im); }static void *readRGB(FILE *inFile, ImageHeader **readimHeader){ unsigned width, height; unsigned maxval; boolean failed; RGBImage im; int x, y; /* * File format: * P3 <width> <height> <maxval> * image data, three bytes per pixel, in RGB order. * If maxval is not 255, we'll have to rescale the values; this will * be slow. */ width = gettoken(inFile, &failed); if (failed) { return((void *)NULL); } height = gettoken(inFile, &failed); if (failed) { return((void *)NULL); } maxval = gettoken(inFile, &failed); if (failed) { return((void *)NULL); } im = (RGBImage)imNew(IMAGE_RGB, width, height); if (im == (RGBImage)NULL) { return((void *)NULL); } /* Inhale everything. */ if (fread((char *)(im->data[0]), sizeof(RGB), (width * height), inFile) != (width * height)) { imFree(im); return((void *)NULL); } if (maxval != COLRNG - 1) { /* Have to rescale. */ for (y = 0; y < height; y++) { for (x = 0; x < width; x++) { imRef(im, x, y).r = (imRef(im, x, y).r * (COLRNG - 1)) / maxval; imRef(im, x, y).g = (imRef(im, x, y).g * (COLRNG - 1)) / maxval; imRef(im, x, y).b = (imRef(im, x, y).b * (COLRNG - 1)) / maxval; } } } *readimHeader = im->header; return(im); }/* Here's a macro that expands out to a function to read a simple file */#define READFUNC(funcname, imtype, imtag, eltype) \static void * \funcname(FILE *inFile, ImageHeader **readimHeader) \{ \ unsigned width, height; \ boolean failed; \ imtype im; \ width = gettoken(inFile, &failed); \ if (failed) { \ return((void *)NULL); \ } \ height = gettoken(inFile, &failed); \ if (failed) { \ return((void *)NULL); \ } \ im = (imtype)imNew(imtag, width, height); \ if (im == (imtype)NULL) { \ return((void *)NULL); \ } \ if (fread((char *)(im->data[0]), sizeof(eltype), \ (width * height), inFile) != (width * height)) { \ imFree(im); \ return((void *)NULL); \ } \ *readimHeader = im->header; \ return(im); \ }READFUNC(readFloat, FloatImage, IMAGE_FLOAT, float)READFUNC(readDouble, DoubleImage, IMAGE_DOUBLE, double)READFUNC(readLong, LongImage, IMAGE_LONG, long)READFUNC(readShort, ShortImage, IMAGE_SHORT, short)#undef READFUNC/* * Here be dragons. * * Well, more like an enormous number of conversion routines... which are * cooked up out of some fairly horrendous macros. */#define CONVFUNC(funcname, fromimtype, fromimtag, fromptrtype, toimtype, toimtag, toptrtype, assignment) \static void * \funcname(void *im) \{ \ fromimtype fromim; \ toimtype toim; \ fromptrtype fromptr; \ toptrtype toptr; \ int x, y; \ assert(im != (void *)NULL); \ fromim = (fromimtype)im; \ toim = (toimtype)imNewOffset(toimtag, \ imGetWidth(fromim), imGetHeight(fromim), \ imGetXBase(fromim), imGetYBase(fromim)); \ if (toim == (toimtype)NULL) { \ return((void *)NULL); \ } \ for (y = imGetYBase(fromim); y <= imGetYMax(fromim); y++) { \ fromptr = imGetPixPtr(fromim, imGetXBase(fromim), y); \ toptr = imGetPixPtr(toim, imGetXBase(toim), y); \ for (x = imGetXBase(fromim); x <= imGetXMax(fromim); x++) { \ assignment; \ imPtrRight(fromim, fromptr); \ imPtrRight(toim, toptr); \ } \ } \ return((void *)toim); \ }#define PROMFUNC(funcname, fromimtype, fromimtag, fromptrtype, toimtype, toimtag, toptrtype, assignment1, assignment2) \static void * \funcname(void *im) \{ \ fromimtype fromim; \ toimtype toim; \ fromptrtype fromptr; \ toptrtype toptr; \ double slope; \ double offset; \ int x, y; \ assert(im != (void *)NULL); \ fromim = (fromimtype)im; \ toim = (toimtype)imNewOffset(toimtag, \ imGetWidth(fromim), imGetHeight(fromim), \ imGetXBase(fromim), imGetYBase(fromim)); \ if (toim == (toimtype)NULL) { \ return((void *)NULL); \ } \ slope = convertTable[fromimtag][toimtag].slope; \ offset = convertTable[fromimtag][toimtag].offset; \ if ((slope == 1.) && (offset == 0.)) { \ /* Special case - straight assignment *
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -