📄 image_pnm.c
字号:
for (col = 0; col < image->Y.num_cols; col++)
{
fscanf(infile, "%d", &red);
if (ferror(infile) || feof(infile))
goto QccError;
fscanf(infile, "%d", &green);
if (ferror(infile) || feof(infile))
goto QccError;
fscanf(infile, "%d", &blue);
if (ferror(infile) || feof(infile))
goto QccError;
QccIMGImageRGBtoYUV(&(image->Y.image[row][col]),
&(image->U.image[row][col]),
&(image->V.image[row][col]),
red, green, blue);
}
}
return(0);
QccError:
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 QccError;
}
if (QccIMGImagePnmReadHeader(infile, image, &image_raw))
{
QccErrorAddMessage("(QccIMGImagePnmRead): Error calling QccIMGImagePnmReadheader()");
goto QccError;
}
if (QccIMGImageAlloc(image))
{
QccErrorAddMessage("(QccIMGImagePnmRead): Error calling QccIMGImageAlloc()");
goto QccError;
}
switch (image->image_type)
{
case QCCIMGTYPE_PBM:
if (QccIMGImagePbmReadData(infile, image, image_raw))
{
QccErrorAddMessage("(QccIMGImagePnmRead): Error calling QccIMGImagePbmReadData()");
goto QccError;
}
break;
case QCCIMGTYPE_PGM:
if (QccIMGImagePgmReadData(infile, image, image_raw))
{
QccErrorAddMessage("(QccIMGImagePnmRead): Error calling QccIMGImagePgmReadData()");
goto QccError;
}
break;
case QCCIMGTYPE_PPM:
if (QccIMGImagePpmReadData(infile, image, image_raw))
{
QccErrorAddMessage("(QccIMGImagePnmRead): Error calling QccIMGImagePpmReadData()");
goto QccError;
}
break;
}
return_value = 0;
goto QccReturn;
QccError:
return_value = 1;
QccReturn:
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 QccError;
}
break;
case QCCIMGTYPE_PGM:
fprintf(outfile, "P5\n");
if (ferror(outfile))
{
QccErrorAddMessage("(QccIMGImagePnmWriteHeader): Error writing header to %s",
image->filename);
goto QccError;
}
break;
case QCCIMGTYPE_PPM:
fprintf(outfile, "P6\n");
if (ferror(outfile))
{
QccErrorAddMessage("(QccIMGImagePnmWriteHeader): Error writing header to %s",
image->filename);
goto QccError;
}
break;
default:
QccErrorAddMessage("QccIMGImagePnmWriteHeader): Unrecognized image type");
goto QccError;
}
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 QccError;
}
if (QccIMGImageGetResolution(image, &num_rows, &num_cols))
{
QccErrorAddMessage("(QccIMGImagewritePnm): Error calling QccIMGImageGetResolution()");
goto QccError;
}
fprintf(outfile, "%d %d\n", num_cols, num_rows);
if (ferror(outfile))
{
QccErrorAddMessage("(QccIMGImagePnmWriteHeader): Error writing header to %s",
image->filename);
goto QccError;
}
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 QccError;
}
}
return(0);
QccError:
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 QccError;
ch = 0;
bit_count = 8;
}
}
if (bit_count != 8)
{
ch >>= bit_count;
if (QccFileWriteChar(outfile, ch))
goto QccError;
}
return(0);
QccError:
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)(QccIMGClipPixelValue(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 QccError;
}
if (QccIMGImagePnmWriteHeader(outfile, image))
{
QccErrorAddMessage("QccIMGImagePnmWrite): Error calling QccIMGImagePnmWriteHeader()");
goto QccError;
}
switch (image->image_type)
{
case QCCIMGTYPE_PBM:
if (QccIMGImagePbmWriteData(outfile, image))
{
QccErrorAddMessage("(QccIMGImagePnmWrite): Error calling QccIMGImagePbmWriteData()");
goto QccError;
}
break;
case QCCIMGTYPE_PGM:
if (QccIMGImagePgmWriteData(outfile, image))
{
QccErrorAddMessage("(QccIMGImagePnmWrite): Error calling QccIMGImagePgmWriteData()");
goto QccError;
}
break;
case QCCIMGTYPE_PPM:
if (!QccIMGImageColor(image))
{
QccErrorAddMessage("(QccIMGImagePpmWrite): Error %s is not a color file",
image->filename);
return(1);
}
if (QccIMGImagePpmWriteData(outfile, image))
{
QccErrorAddMessage("(QccIMGImagePnmWrite): Error calling QccIMGImagePpmWriteData()");
goto QccError;
}
break;
}
QccFileClose(outfile);
return(0);
QccError:
return(1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -