C++:比较 if 语句中的十进制值
C++: Comparing the decimal value in a if statement
我正在打印出.bmp文件中像素的值。我的问题是,如果像素不是 255 255 255(白色),那么我希望它是 0 0 0(黑色)。
这是完整的代码:(是的,这是从 24bpp 位图中获取每个像素的 RGB 值以转换为 C 格式的 GBA 格式)这适用于 24 位.bmp
#include <stdio.h>
#include <stdlib.h>
#pragma pack(2)
typedef struct
{
char signature[2];
unsigned int fileSize;
unsigned int reserved;
unsigned int offset;
} BmpHeader;
typedef struct
{
unsigned int headerSize;
unsigned int width;
unsigned int height;
unsigned short planeCount;
unsigned short bitDepth;
unsigned int compression;
unsigned int compressedImageSize;
unsigned int horizontalResolution;
unsigned int verticalResolution;
unsigned int numColors;
unsigned int importantColors;
} BmpImageInfo;
typedef struct
{
unsigned char blue;
unsigned char green;
unsigned char red;
//unsigned char reserved; Removed for convenience in fread; info.bitDepth/8 doesn't seem to work for some reason
} Rgb;
int main( int argc, char **argv ) {
FILE *inFile;
BmpHeader header;
BmpImageInfo info;
Rgb *palette;
int i = 0;
printf( "Opening file %s for reading.n", argv[1] );
inFile = fopen( argv[1], "rb" );
if( !inFile ) {
printf( "Error opening file %s.n", argv[1] );
return -1;
}
if( fread(&header, 1, sizeof(BmpHeader), inFile) != sizeof(BmpHeader) ) {
printf( "Error reading bmp header.n" );
return -1;
}
if( fread(&info, 1, sizeof(BmpImageInfo), inFile) != sizeof(BmpImageInfo) ) {
printf( "Error reading image info.n" );
return -1;
}
if( info.numColors > 0 ) {
printf( "Reading palette.n" );
palette = (Rgb*)malloc(sizeof(Rgb) * info.numColors);
if( fread(palette, sizeof(Rgb), info.numColors, inFile) != (info.numColors * sizeof(Rgb)) ) {
printf( "Error reading palette.n" );
return -1; // error
}
}
printf( "Opening file %s for writing.n", argv[2] );
FILE *outFile = fopen( argv[2], "wb" );
if( !outFile ) {
printf( "Error opening outputfile.n" );
return -1;
}
Rgb *pixel = (Rgb*) malloc( sizeof(Rgb) );
int read, j;
for( j=0; j<info.height; j++ ) {
printf( "------ Row %dn", j+1 );
read = 0;
for( i=0; i<info.width; i++ ) {
if( fread(pixel, 1, sizeof(Rgb), inFile) != sizeof(Rgb) ) {
printf( "Error reading pixel!n" );
return -1;
}
read += sizeof(Rgb);
if (pixel->red != 255 || pixel->green != 255 || pixel->blue != 255)
printf( "Pixel %d: 0 0 0n", i+1, );
else
printf( "Pixel %d: %3d %3d %3dn", i+1, pixel->red, pixel->green, pixel->blue );
/*if (pixel->red != 255 || pixel->green != 255 || pixel->blue != 255)
printf ("Pixel %d: 0 0 0n", i+1);
else
printf( "Pixel %d: 255 255 255n", i+1);*/
}
if( read % 4 != 0 ) {
read = 4 - (read%4);
printf( "Padding: %d bytesn", read );
fread( pixel, read, 1, inFile );
}
}
printf( "Done.n" );
fclose(inFile);
fclose(outFile);
printf( "nBMP-Info:n" );
printf( "Width x Height: %i x %in", info.width, info.height );
printf( "Depth: %in", (int)info.bitDepth );
return 0;
}
像素的 RGB 值可能是整数,而不是字符串:
if (pixel->red != 255 || pixel->green != 255 || pixel->blue != 255)
printf ("Pixel %d: 0 0 0n", i+1);
else
printf( "Pixel %d: %3d %3d %3dn", i+1, pixel->red, pixel->green, pixel->blue );
由于如果像素为白色,则要打印"255 255 255",否则要打印"0 0 0",因此这样做更简单:
if (pixel->red != 255 || pixel->green != 255 || pixel->blue != 255)
printf ("Pixel %d: 0 0 0n", i+1);
else
printf( "Pixel %d: 255 255 255n", i+1);
相关文章:
- 我的简单if-else语句是如何无法访问的代码
- 有一个打印语句的函数是一种糟糕的编程实践吗
- 线程,如果else语句,都是错误的上下文切换后,会发生什么
- 为什么是0;C++中的有效语句
- visual在c++中将十进制数转换为二进制数
- C++十进制到二进制,如何转换
- Insert函数不适用于2 if语句C++
- If语句未被求值C++
- C++嵌套if语句,基本货币交换
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- 是否可以使用if constexpr删除控制流语句
- 无法理解此 return 语句的功能,没有它就会发生运行时错误
- 我似乎对if/else的基本语句有问题:/
- 当调用switch语句中的函数时(即使函数不包含循环),似乎是永不结束的循环的问题
- If语句在c++中被忽略
- 比较if语句中的数组值和int值
- 以在Qt中的IF语句中设置时间延迟
- C++:比较 if 语句中的十进制值
- 在C++中将二进制转换为十进制而不使用条件语句
- C++十进制检查语句是否有错误