📄 image_component.c
字号:
/* * * QccPack: Quantization, compression, and coding libraries * Copyright (C) 1997-2009 James E. Fowler * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, * MA 02139, USA. * */#include "libQccPack.h"static QccIMGImageArray QccIMGImageArrayAlloc(int num_rows, int num_cols){ QccIMGImageArray new_array; int row; if ((num_rows <= 0) && (num_cols <= 0)) return(NULL); if ((new_array = (double **)malloc(sizeof(double *)*num_rows)) == NULL) { QccErrorAddMessage("(QccIMGImageArrayAlloc): Error allocating memory"); return(NULL); } for (row = 0; row < num_rows; row++) if ((new_array[row] = (double *)malloc(sizeof(double)*num_cols)) == NULL) { QccErrorAddMessage("(QccIMGImageArrayAlloc): Error allocating memory"); return(NULL); } return(new_array);}static void QccIMGImageArrayFree(QccIMGImageArray image_array, int num_rows){ int row; if (image_array != NULL) { for (row = 0; row < num_rows; row++) if (image_array[row] != NULL) QccFree(image_array[row]); QccFree(image_array); }}int QccIMGImageComponentInitialize(QccIMGImageComponent *image_component){ if (image_component == NULL) return(0); QccStringMakeNull(image_component->filename); QccStringCopy(image_component->magic_num, QCCIMGIMAGECOMPONENT_MAGICNUM); QccGetQccPackVersion(&image_component->major_version, &image_component->minor_version, NULL); image_component->num_rows = 0; image_component->num_cols = 0; image_component->min_val = 0.0; image_component->max_val = 0.0; image_component->image = NULL; return(0);}int QccIMGImageComponentAlloc(QccIMGImageComponent *image_component){ if (image_component == NULL) return(0); if (image_component->image == NULL) { if ((image_component->num_rows > 0) || (image_component->num_cols > 0)) { if ((image_component->image = QccIMGImageArrayAlloc(image_component->num_rows, image_component->num_cols)) == NULL) { QccErrorAddMessage("(QccIMGImageComponentAlloc): Error calling QccIMGImageArrayAlloc()"); return(1); } } else image_component->image = NULL; } return(0);}void QccIMGImageComponentFree(QccIMGImageComponent *image_component){ if (image_component == NULL) return; if (image_component->image != NULL) { QccIMGImageArrayFree(image_component->image, image_component->num_rows); image_component->image = NULL; }}int QccIMGImageComponentPrint(const QccIMGImageComponent *image_component){ int row, col; if (QccFilePrintFileInfo(image_component->filename, image_component->magic_num, image_component->major_version, image_component->minor_version)) return(1); printf("Size: %dx%d\n", image_component->num_cols, image_component->num_rows); printf("Min. value: % 10.4f\n", image_component->min_val); printf("Max. value: % 10.4f\n", image_component->max_val); printf("\nImage array:\n\n"); for (row = 0; row < image_component->num_rows; row++) { for (col = 0; col < image_component->num_cols; col++) printf("% 10.4f ", image_component->image[row][col]); printf("\n"); } return(0);}int QccIMGImageComponentSetMin(QccIMGImageComponent *image_component){ double min = MAXDOUBLE; int row, col; if (image_component == NULL) return(0); if (image_component->image == NULL) return(0); for (row = 0; row < image_component->num_rows; row++) for (col = 0; col < image_component->num_cols; col++) if (image_component->image[row][col] < min) min = image_component->image[row][col]; image_component->min_val = min; return(0);}int QccIMGImageComponentSetMax(QccIMGImageComponent *image_component){ double max = -MAXDOUBLE; int row, col; if (image_component == NULL) return(0); if (image_component->image == NULL) return(0); for (row = 0; row < image_component->num_rows; row++) for (col = 0; col < image_component->num_cols; col++) if (image_component->image[row][col] > max) max = image_component->image[row][col]; image_component->max_val = max; return(0);}int QccIMGImageComponentSetMaxMin(QccIMGImageComponent *image_component){ if (QccIMGImageComponentSetMax(image_component)) { QccErrorAddMessage("(QccIMGImageComponentSetMaxMin): Error calling QccIMGImageComponentSetMax()"); return(1); } if (QccIMGImageComponentSetMin(image_component)) { QccErrorAddMessage("(QccIMGImageComponentSetMaxMin): Error calling QccIMGImageComponentSetMin()"); return(1); } return(0);}int QccIMGImageComponentResize(QccIMGImageComponent *image_component, int num_rows, int num_cols){ if (image_component == NULL) return(0); if ((image_component->image = (QccIMGImageArray)QccMatrixResize((QccMatrix)image_component->image, image_component->num_rows, image_component->num_cols, num_rows, num_cols)) == NULL) { QccErrorAddMessage("(QccIMGImageComponentResize): Error calling QccMatrixResize()"); return(1); } image_component->num_rows = num_rows; image_component->num_cols = num_cols; QccIMGImageComponentSetMaxMin(image_component); return(0);}static int QccIMGImageComponentReadHeader(FILE *infile, QccIMGImageComponent *image_component){ if ((infile == NULL) || (image_component == NULL)) return(0); if (QccFileReadMagicNumber(infile, image_component->magic_num, &image_component->major_version, &image_component->minor_version)) { QccErrorAddMessage("(QccIMGImageComponentReadHeader): Error reading magic number in %s", image_component->filename); return(1); } if (strcmp(image_component->magic_num, QCCIMGIMAGECOMPONENT_MAGICNUM)) { QccErrorAddMessage("(QccIMGImageComponentReadHeader): %s is not of image_component (%s) type", image_component->filename, QCCIMGIMAGECOMPONENT_MAGICNUM); return(1); } fscanf(infile, "%d", &(image_component->num_cols)); if (ferror(infile) || feof(infile)) { QccErrorAddMessage("(QccIMGImageComponentReadHeader): Error reading number of columns in %s", image_component->filename); return(1); } if (QccFileSkipWhiteSpace(infile, 0)) { QccErrorAddMessage("(QccIMGImageComponentReadHeader): Error reading number of columns in %s", image_component->filename); return(1); } fscanf(infile, "%d", &(image_component->num_rows)); if (ferror(infile) || feof(infile)) { QccErrorAddMessage("(QccIMGImageComponentReadHeader): Error reading number of rows in %s", image_component->filename); return(1); } if (QccFileSkipWhiteSpace(infile, 0)) { QccErrorAddMessage("(QccIMGImageComponentReadHeader): Error reading number of rows in %s", image_component->filename); return(1); } fscanf(infile, "%lf", &(image_component->min_val)); if (ferror(infile) || feof(infile)) { QccErrorAddMessage("(QccIMGImageComponentReadHeader): Error reading number of rows in %s", image_component->filename); return(1); } if (QccFileSkipWhiteSpace(infile, 0)) { QccErrorAddMessage("(QccIMGImageComponentReadHeader): Error reading number of rows in %s", image_component->filename); return(1); } fscanf(infile, "%lf%*1[\n]", &(image_component->max_val)); if (ferror(infile) || feof(infile)) { QccErrorAddMessage("(QccIMGImageComponentReadHeader): Error reading number of rows in %s", image_component->filename); return(1); } return(0);}static int QccIMGImageComponentReadData(FILE *infile, QccIMGImageComponent *image_component){ int row, col; if ((infile == NULL) || (image_component == NULL)) return(0); for (row = 0; row < image_component->num_rows; row++) for (col = 0; col < image_component->num_cols; col++) if (QccFileReadDouble(infile, &(image_component->image[row][col]))) { QccErrorAddMessage("(QccIMGImageComponentReadData): Error calling QccFileReadDouble()", image_component->filename); return(1); } return(0);}int QccIMGImageComponentRead(QccIMGImageComponent *image_component){ FILE *infile = NULL; if (image_component == NULL) return(0); if ((infile = QccFileOpen(image_component->filename, "r")) == NULL) { QccErrorAddMessage("(QccIMGImageComponentRead): Error opening %s for reading", image_component->filename); return(1); } if (QccIMGImageComponentReadHeader(infile, image_component)) { QccErrorAddMessage("(QccIMGImageComponentRead): Error calling QccIMGImageComponentReadHeader()"); return(1); } if (QccIMGImageComponentAlloc(image_component)) { QccErrorAddMessage("(QccIMGImageComponentRead): Error calling QccIMGImageComponentAlloc()"); return(1); } if (QccIMGImageComponentReadData(infile, image_component)) { QccErrorAddMessage("(QccIMGImageComponentRead): Error calling QccIMGImageComponentReadData()"); return(1); } QccFileClose(infile); return(0);}static int QccIMGImageComponentWriteHeader(FILE *outfile, const QccIMGImageComponent *image_component){ if ((outfile == NULL) || (image_component == NULL)) return(0); if (QccFileWriteMagicNumber(outfile, QCCIMGIMAGECOMPONENT_MAGICNUM)) goto Error; fprintf(outfile, "%d %d\n", image_component->num_cols, image_component->num_rows); if (ferror(outfile)) goto Error; fprintf(outfile, "% 16.9e % 16.9e\n", image_component->min_val, image_component->max_val); if (ferror(outfile)) goto Error; return(0); Error: QccErrorAddMessage("(QccIMGImageComponentWriteHeader): Error writing header to %s", image_component->filename); return(1);}static int QccIMGImageComponentWriteData(FILE *outfile, const QccIMGImageComponent *image_component){ int row, col; if ((image_component == NULL) || (outfile == NULL)) return(0); for (row = 0; row < image_component->num_rows; row++) for (col = 0; col < image_component->num_cols; col++) if (QccFileWriteDouble(outfile, image_component->image[row][col])) { QccErrorAddMessage("(QccIMGImageComponentWriteData): Error calling QccFileWriteDouble()"); return(1); } return(0);}int QccIMGImageComponentWrite(const QccIMGImageComponent *image_component){ FILE *outfile; if (image_component == NULL) return(0); if ((outfile = QccFileOpen(image_component->filename, "w")) == NULL) { QccErrorAddMessage("(QccIMGImageComponentWrite): Error opening %s for writing", image_component->filename); return(1); } if (QccIMGImageComponentWriteHeader(outfile, image_component)) { QccErrorAddMessage("(QccIMGImageComponentWrite): Error calling QccIMGImageComponentWriteHeader()");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -