C++:比较 if 语句中的十进制值

标签 c++ pixel

我正在打印 .bmp 文件中的像素值。我的问题是,如果像素不是 255 255 255(白色),那么我希望它是 0 0 0(黑色)。

这是完整的代码:(是的,这是来自 Getting RGB values for each pixel from a 24bpp Bitmap for conversion to GBA format in C ) 这适用于 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 %d\n", 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 0\n", i+1, );
                        else
                            printf( "Pixel %d: %3d %3d %3d\n", i+1, pixel->red, pixel->green, pixel->blue );
                        /*if (pixel->red != 255 || pixel->green != 255 || pixel->blue != 255)
                            printf ("Pixel %d: 0 0 0\n", i+1);
                        else
                            printf( "Pixel %d: 255 255 255\n", i+1);*/

                }
                if( read % 4 != 0 ) {
                        read = 4 - (read%4);
                        printf( "Padding: %d bytes\n", read );
                        fread( pixel, read, 1, inFile );
                }
        }

        printf( "Done.\n" );
        fclose(inFile);
        fclose(outFile);

        printf( "\nBMP-Info:\n" );
        printf( "Width x Height: %i x %i\n", info.width, info.height );
        printf( "Depth: %i\n", (int)info.bitDepth );

        return 0;

}

最佳答案

您的像素的 RGB 值可能是整数,而不是字符串:

if (pixel->red != 255 || pixel->green != 255 || pixel->blue != 255)
    printf ("Pixel %d: 0 0 0\n", i+1);
else
    printf( "Pixel %d: %3d %3d %3d\n", 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 0\n", i+1);
else
    printf( "Pixel %d: 255 255 255\n", i+1);

关于C++:比较 if 语句中的十进制值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10194705/

相关文章:

css - 我应该使用 px 还是百分比?

graphics - 计算相机光线方向到 3d 世界像素

c++ - 单元测试++和主要

c++ - 通过 COM 在 64 位进程中使用 32 位 DLL

c++ - 将参数放入 Qt 中的槽中

ios - "Leaves"iOS 库

c++ - 什么软件在设计/规划一个新的大型个人项目时有用?

C++ 引用变量的初始化

html - 保持视口(viewport)在同一高度

ios - 如何更改 UIImageView 中单个像素的图像