📄 image_pnm.c
字号:
blue = (int)ch; QccIMGImageRGBtoYUV(&(image->Y.image[row][col]), &(image->U.image[row][col]), &(image->V.image[row][col]), red, green, blue); } } else { for (row = 0; row < image->Y.num_rows; row++) for (col = 0; col < image->Y.num_cols; col++) { fscanf(infile, "%d", &red); if (ferror(infile) || feof(infile)) goto Error; fscanf(infile, "%d", &green); if (ferror(infile) || feof(infile)) goto Error; fscanf(infile, "%d", &blue); if (ferror(infile) || feof(infile)) goto Error; QccIMGImageRGBtoYUV(&(image->Y.image[row][col]), &(image->U.image[row][col]), &(image->V.image[row][col]), red, green, blue); } } return(0); Error: QccErrorAddMessage("(QccIMGImagePPMReadData): Error reading data in %s", image->filename); return(1);}int QccIMGImagePNMRead(QccIMGImage *image){ FILE *infile; int image_raw; int return_value; if ((infile = QccFileOpen(image->filename, "r")) == NULL) { QccErrorAddMessage("(QccIMGImagePNMRead): Error calling QccFileOpen()"); goto Error; } if (QccIMGImagePNMReadHeader(infile, image, &image_raw)) { QccErrorAddMessage("(QccIMGImagePNMRead): Error calling QccIMGImagePNMReadheader()"); goto Error; } if (QccIMGImageAlloc(image)) { QccErrorAddMessage("(QccIMGImagePNMRead): Error calling QccIMGImageAlloc()"); goto Error; } switch (image->image_type) { case QCCIMGTYPE_PBM: if (QccIMGImagePBMReadData(infile, image, image_raw)) { QccErrorAddMessage("(QccIMGImagePNMRead): Error calling QccIMGImagePBMReadData()"); goto Error; } break; case QCCIMGTYPE_PGM: if (QccIMGImagePGMReadData(infile, image, image_raw)) { QccErrorAddMessage("(QccIMGImagePNMRead): Error calling QccIMGImagePGMReadData()"); goto Error; } break; case QCCIMGTYPE_PPM: if (QccIMGImagePPMReadData(infile, image, image_raw)) { QccErrorAddMessage("(QccIMGImagePNMRead): Error calling QccIMGImagePPMReadData()"); goto Error; } break; } return_value = 0; goto Return; Error: return_value = 1; Return: QccFileClose(infile); return(return_value);}static int QccIMGImagePNMWriteHeader(FILE *outfile, const QccIMGImage *image){ int num_rows, num_cols; int major_version, minor_version; QccString date; switch (image->image_type) { case QCCIMGTYPE_PBM: fprintf(outfile, "P4\n"); if (ferror(outfile)) { QccErrorAddMessage("(QccIMGImagePNMWriteHeader): Error writing header to %s", image->filename); goto Error; } break; case QCCIMGTYPE_PGM: fprintf(outfile, "P5\n"); if (ferror(outfile)) { QccErrorAddMessage("(QccIMGImagePNMWriteHeader): Error writing header to %s", image->filename); goto Error; } break; case QCCIMGTYPE_PPM: fprintf(outfile, "P6\n"); if (ferror(outfile)) { QccErrorAddMessage("(QccIMGImagePNMWriteHeader): Error writing header to %s", image->filename); goto Error; } break; default: QccErrorAddMessage("(QccIMGImagePNMWriteHeader): Unrecognized image type"); goto Error; } QccGetQccPackVersion(&major_version, &minor_version, date); fprintf(outfile, "# Created by QccPack Version %d.%d (%s)\n", major_version, minor_version, date); if (ferror(outfile)) { QccErrorAddMessage("(QccIMGImagePNMWriteHeader): Error writing header to %s", image->filename); goto Error; } if (QccIMGImageGetSize(image, &num_rows, &num_cols)) { QccErrorAddMessage("(QccIMGImagewritePNM): Error calling QccIMGImageGetSize()"); goto Error; } fprintf(outfile, "%d %d\n", num_cols, num_rows); if (ferror(outfile)) { QccErrorAddMessage("(QccIMGImagePNMWriteHeader): Error writing header to %s", image->filename); goto Error; } if ((image->image_type == QCCIMGTYPE_PGM) || (image->image_type == QCCIMGTYPE_PPM)) { fprintf(outfile, "%d\n", 255); if (ferror(outfile)) { QccErrorAddMessage("(QccIMGImagePNMWriteHeader): Error writing header to %s", image->filename); goto Error; } } return(0); Error: return(1);}static int QccIMGImagePBMWriteData(FILE *outfile, const QccIMGImage *image){ int row, col; int bit_count = 8; unsigned char ch = 0; for (row = 0; row < image->Y.num_rows; row++) for (col = 0; col < image->Y.num_cols; col++) { ch <<= 1; ch |= (image->Y.image[row][col] == 0); bit_count--; if (!bit_count) { if (QccFileWriteChar(outfile, ch)) goto Error; ch = 0; bit_count = 8; } } if (bit_count != 8) { ch >>= bit_count; if (QccFileWriteChar(outfile, ch)) goto Error; } return(0); Error: QccErrorAddMessage("(QccIMGImagePBMWriteData): Error writing data to %s", image->filename); return(1);}static int QccIMGImagePGMWriteData(FILE *outfile, const QccIMGImage *image){ int row, col; for (row = 0; row < image->Y.num_rows; row++) for (col = 0; col < image->Y.num_cols; col++) if (QccFileWriteChar(outfile, (char)(QccIMGImageComponentClipPixel(image->Y.image [row][col]) + 0.5))) { QccErrorAddMessage("(QccIMGImagePGMWriteData): Error writing data to %s", image->filename); return(1); } return(0);}static int QccIMGImagePPMWriteData(FILE *outfile, const QccIMGImage *image){ int row, col; int red, green, blue; for (row = 0; row < image->Y.num_rows; row++) for (col = 0; col < image->Y.num_cols; col++) { QccIMGImageYUVtoRGB(&red, &green, &blue, image->Y.image[row][col], image->U.image[row][col], image->V.image[row][col]); if (QccFileWriteChar(outfile, (char)red)) { QccErrorAddMessage("(QccIMGImagePPMWriteData): Error writing data to %s", image->filename); return(1); } if (QccFileWriteChar(outfile, (char)green)) { QccErrorAddMessage("(QccIMGImagePPMWriteData): Error writing data to %s", image->filename); return(1); } if (QccFileWriteChar(outfile, (char)blue)) { QccErrorAddMessage("(QccIMGImagePPMWriteData): Error writing data to %s", image->filename); return(1); } } return(0);}int QccIMGImagePNMWrite(const QccIMGImage *image){ FILE *outfile; if (image == NULL) return(0); if ((outfile = QccFileOpen(image->filename, "w")) == NULL) { QccErrorAddMessage("(QccIMGImagePNMWrite): Error calling QccFileOpen()"); goto Error; } if (QccIMGImagePNMWriteHeader(outfile, image)) { QccErrorAddMessage("(QccIMGImagePNMWrite): Error calling QccIMGImagePNMWriteHeader()"); goto Error; } switch (image->image_type) { case QCCIMGTYPE_PBM: if (QccIMGImagePBMWriteData(outfile, image)) { QccErrorAddMessage("(QccIMGImagePNMWrite): Error calling QccIMGImagePBMWriteData()"); goto Error; } break; case QCCIMGTYPE_PGM: if (QccIMGImagePGMWriteData(outfile, image)) { QccErrorAddMessage("(QccIMGImagePNMWrite): Error calling QccIMGImagePGMWriteData()"); goto Error; } break; case QCCIMGTYPE_PPM: if (!QccIMGImageColor(image)) { QccErrorAddMessage("(QccIMGImagePNMWrite): Error %s is not a color file", image->filename); return(1); } if (QccIMGImagePPMWriteData(outfile, image)) { QccErrorAddMessage("(QccIMGImagePNMWrite): Error calling QccIMGImagePPMWriteData()"); goto Error; } break; } QccFileClose(outfile); return(0); Error: return(1);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -