📄 klt.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"int QccHYPkltInitialize(QccHYPklt *klt){ if (klt == NULL) return(0); QccStringMakeNull(klt->filename); QccStringCopy(klt->magic_num, QCCHYPKLT_MAGICNUM); QccGetQccPackVersion(&klt->major_version, &klt->minor_version, NULL); klt->num_bands = 0; klt->mean = NULL; klt->matrix = NULL; return(0);}int QccHYPkltAlloc(QccHYPklt *klt){ if (klt == NULL) return(0); if (klt->mean == NULL) { if (klt->num_bands > 0) { if ((klt->mean = QccVectorAlloc(klt->num_bands)) == NULL) { QccErrorAddMessage("(QccHYPkltAlloc): Error calling QccVectorAlloc()"); return(1); } } else klt->mean = NULL; } if (klt->matrix == NULL) { if (klt->num_bands > 0) { if ((klt->matrix = QccMatrixAlloc(klt->num_bands, klt->num_bands)) == NULL) { QccErrorAddMessage("(QccHYPkltAlloc): Error calling QccMatrixAlloc()"); return(1); } } else klt->matrix = NULL; } return(0);}void QccHYPkltFree(QccHYPklt *klt){ if (klt == NULL) return; if (klt->mean != NULL) { QccVectorFree(klt->mean); klt->mean = NULL; } if (klt->matrix != NULL) { QccMatrixFree(klt->matrix, klt->num_bands); klt->matrix = NULL; }}int QccHYPkltPrint(const QccHYPklt *klt){ int row, col; if (QccFilePrintFileInfo(klt->filename, klt->magic_num, klt->major_version, klt->minor_version)) return(1); printf("Num Bands: %d\n", klt->num_bands); printf("\nMean Vector:\n\n"); for (col = 0; col < klt->num_bands; col++) printf("% 10.4f ", klt->mean[col]); printf("\nTransform matrix:\n\n"); for (row = 0; row < klt->num_bands; row++) { for (col = 0; col < klt->num_bands; col++) printf("% 10.4f ", klt->matrix[row][col]); printf("\n"); } return(0);}static int QccHYPkltReadHeader(FILE *infile, QccHYPklt *klt){ if ((infile == NULL) || (klt == NULL)) return(0); if (QccFileReadMagicNumber(infile, klt->magic_num, &klt->major_version, &klt->minor_version)) { QccErrorAddMessage("(QccHYPkltReadHeader): Error reading magic number in %s", klt->filename); return(1); } if (strcmp(klt->magic_num, QCCHYPKLT_MAGICNUM)) { QccErrorAddMessage("(QccHYPkltReadHeader): %s is not of klt (%s) type", klt->filename, QCCHYPKLT_MAGICNUM); return(1); } fscanf(infile, "%d%*1[\n]", &(klt->num_bands)); if (ferror(infile) || feof(infile)) { QccErrorAddMessage("(QccHYPkltReadHeader): Error reading number of bands in %s", klt->filename); return(1); } return(0);}static int QccHYPkltReadData(FILE *infile, QccHYPklt *klt){ int row, col; if ((infile == NULL) || (klt == NULL)) return(0); for (col = 0; col < klt->num_bands; col++) if (QccFileReadDouble(infile, &(klt->mean[col]))) { QccErrorAddMessage("(QccHYPkltReadData): Error calling QccFileReadDouble()"); return(1); } for (row = 0; row < klt->num_bands; row++) for (col = 0; col < klt->num_bands; col++) if (QccFileReadDouble(infile, &(klt->matrix[row][col]))) { QccErrorAddMessage("(QccHYPkltReadData): Error calling QccFileReadDouble()"); return(1); } return(0);}int QccHYPkltRead(QccHYPklt *klt){ FILE *infile = NULL; if (klt == NULL) return(0); if ((infile = QccFileOpen(klt->filename, "r")) == NULL) { QccErrorAddMessage("(QccHYPkltRead): Error opening %s for reading", klt->filename); return(1); } if (QccHYPkltReadHeader(infile, klt)) { QccErrorAddMessage("(QccHYPkltRead): Error calling QccHYPkltReadHeader()"); return(1); } if (QccHYPkltAlloc(klt)) { QccErrorAddMessage("(QccHYPkltRead): Error calling QccHYPkltAlloc()"); return(1); } if (QccHYPkltReadData(infile, klt)) { QccErrorAddMessage("(QccHYPkltRead): Error calling QccHYPkltReadData()"); return(1); } QccFileClose(infile); return(0);}static int QccHYPkltWriteHeader(FILE *outfile, const QccHYPklt *klt){ if ((outfile == NULL) || (klt == NULL)) return(0); if (QccFileWriteMagicNumber(outfile, QCCHYPKLT_MAGICNUM)) goto Error; fprintf(outfile, "%d\n", klt->num_bands); if (ferror(outfile)) goto Error; return(0); Error: QccErrorAddMessage("(QccHYPkltWriteHeader): Error writing header to %s", klt->filename); return(1);}static int QccHYPkltWriteData(FILE *outfile, const QccHYPklt *klt){ int row, col; if ((klt == NULL) || (outfile == NULL)) return(0); for (col = 0; col < klt->num_bands; col++) if (QccFileWriteDouble(outfile, klt->mean[col])) { QccErrorAddMessage("(QccHYPkltWriteData): Error calling QccFileWriteDouble()"); return(1); } for (row = 0; row < klt->num_bands; row++) for (col = 0; col < klt->num_bands; col++) if (QccFileWriteDouble(outfile, klt->matrix[row][col])) { QccErrorAddMessage("(QccHYPkltWriteData): Error calling QccFileWriteDouble()"); return(1); } return(0);}int QccHYPkltWrite(const QccHYPklt *klt){ FILE *outfile; if (klt == NULL) return(0); if ((outfile = QccFileOpen(klt->filename, "w")) == NULL) { QccErrorAddMessage("(QccHYPkltWrite): Error opening %s for writing", klt->filename); return(1); } if (QccHYPkltWriteHeader(outfile, klt)) { QccErrorAddMessage("(QccHYPkltWrite): Error calling QccHYPkltWriteHeader()"); return(1); } if (QccHYPkltWriteData(outfile, klt)) { QccErrorAddMessage("(QccHYPkltWrite): Error calling QccHYPkltWriteData()"); return(1);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -