Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 转换此代码中的像素_C_Bmp_Cs50 - Fatal编程技术网

C 转换此代码中的像素

C 转换此代码中的像素,c,bmp,cs50,C,Bmp,Cs50,我似乎在一个小的编程作业中遇到了困难。(请不要告诉我如何解决整个问题;我只想知道如何将每个字节设置为红色) 我的代码如下: #include <stdio.h> #include <stdlib.h> #include <cs50.h> #include "bmp.h" int main(int argc, char* argv[]) { // ensure proper usage //if (argc != 3) //{

我似乎在一个小的编程作业中遇到了困难。(请不要告诉我如何解决整个问题;我只想知道如何将每个字节设置为红色)

我的代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <cs50.h>

#include "bmp.h"

int main(int argc, char* argv[])
{
    // ensure proper usage
    //if (argc != 3)
    //{
    //   printf("Usage: copy infile outfile\n");
    //    return 1;
    //}

    // remember filenames
    //char* infile = argv[1];
    //char* outfile = argv[2];
    char* infile = GetString();
    char* outfile = GetString();

    // open input file 
    FILE* inptr = fopen(infile, "r");
    if (inptr == NULL)
    {
        printf("Could not open %s.\n", infile);
        return 2;
    }

    // open output file
    FILE* outptr = fopen(outfile, "w");
    if (outptr == NULL)
    {
        fclose(inptr);
        fprintf(stderr, "Could not create %s.\n", outfile);
        return 3;
    }

    // read infile's BITMAPFILEHEADER
    BITMAPFILEHEADER bf;
    fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);

    // read infile's BITMAPINFOHEADER
    BITMAPINFOHEADER bi;
    fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);

    // ensure infile is (likely) a 24-bit uncompressed BMP 4.0
    if (bf.bfType != 0x4d42 || bf.bfOffBits != 54 || bi.biSize != 40 || 
        bi.biBitCount != 24 || bi.biCompression != 0)
    {
        fclose(outptr);
        fclose(inptr);
        fprintf(stderr, "Unsupported file format.\n");
        return 4;
    }

    // write outfile's BITMAPFILEHEADER
    fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);

    // write outfile's BITMAPINFOHEADER
    fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);

    // determine padding for scanlines
    int padding =  (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

    // iterate over infile's scanlines
    for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
    {
        // iterate over pixels in scanline
        for (int j = 0; j < bi.biWidth; j++)
        {
            // temporary storage
            RGBTRIPLE triple;

            // read RGB triple from infile
            fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
            triple.rgbtRed = 'ff'
            // write RGB triple to outfile
            fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
        }

        // skip over padding, if any
        fseek(inptr, padding, SEEK_CUR);

        // then add it back (to demonstrate how)
        for (int k = 0; k < padding; k++)
            fputc(0x00, outptr);
    }

    // close infile
    fclose(inptr);

    // close outfile
    fclose(outptr);

    // that's all folks
    return 0;
}
#包括
#包括
#包括
#包括“bmp.h”
int main(int argc,char*argv[])
{
//确保正确使用
//如果(argc!=3)
//{
//printf(“用法:复制填充输出文件\n”);
//返回1;
//}
//记住文件名
//char*infle=argv[1];
//char*outfile=argv[2];
char*infle=GetString();
char*outfile=GetString();
//打开输入文件
文件*inptr=fopen(填入“r”);
如果(inptr==NULL)
{
printf(“无法打开%s。\n”,填充);
返回2;
}
//打开输出文件
文件*outptr=fopen(输出文件,“w”);
if(outptr==NULL)
{
fclose(inptr);
fprintf(stderr,“无法创建%s.\n”,输出文件);
返回3;
}
//读取内嵌的BITMAPFILEHEADER
位图文件头;
fread(&bf,sizeof(BITMAPFILEHEADER),1,inptr);
//读取内嵌的BitMapInfo标头
BitMapInfo头bi;
fread(&bi,sizeof(BitMapInfo标头),1,inptr);
//确保infle(可能)是24位未压缩的BMP 4.0
如果(bf.bfType!=0x4d42 | | bf.bfOffBits!=54 | | bi.biSize!=40 | |
bi.biBitCount!=24 | | bi.biCompression!=0)
{
fclose(outptr);
fclose(inptr);
fprintf(stderr,“不支持的文件格式。\n”);
返回4;
}
//写入输出文件的BITMAPFILEHEADER
fwrite(&bf,sizeof(BITMAPFILEHEADER),1,outptr);
//写入输出文件的BitMapInfo标头
fwrite(&bi,sizeof(BitMapInfo标头),1,outptr);
//确定扫描线的填充
int padding=(4-(bi.biWidth*sizeof(RGBTRIPLE))%4)%4;
//迭代infle的扫描线
对于(inti=0,biHeight=abs(bi.biHeight);i
而bmp.h是

#include <stdint.h>

/**
 * Common Data Types 
 *
 * The data types in this section are essentially aliases for C/C++ 
 * primitive data types.
 *
 * Adapted from http://msdn.microsoft.com/en-us/library/cc230309(PROT.10).aspx.
 * See http://en.wikipedia.org/wiki/Stdint.h for more on stdint.h.
 */
typedef uint8_t  BYTE;
typedef uint32_t DWORD;
typedef int32_t  LONG;
typedef uint16_t WORD;

/**
 * BITMAPFILEHEADER
 *
 * The BITMAPFILEHEADER structure contains information about the type, size,
 * and layout of a file that contains a DIB [device-independent bitmap].
 *
 * Adapted from http://msdn.microsoft.com/en-us/library/dd183374(VS.85).aspx.
 */
typedef struct 
{ 
    WORD   bfType; 
    DWORD  bfSize; 
    WORD   bfReserved1; 
    WORD   bfReserved2; 
    DWORD  bfOffBits; 
} __attribute__((__packed__)) 
BITMAPFILEHEADER; 

/**
 * BITMAPINFOHEADER
 *
 * The BITMAPINFOHEADER structure contains information about the 
 * dimensions and color format of a DIB [device-independent bitmap].
 *
 * Adapted from http://msdn.microsoft.com/en-us/library/dd183376(VS.85).aspx.
 */
typedef struct
{
    DWORD  biSize; 
    LONG   biWidth; 
    LONG   biHeight; 
    WORD   biPlanes; 
    WORD   biBitCount; 
    DWORD  biCompression; 
    DWORD  biSizeImage; 
    LONG   biXPelsPerMeter; 
    LONG   biYPelsPerMeter; 
    DWORD  biClrUsed; 
    DWORD  biClrImportant; 
} __attribute__((__packed__))
BITMAPINFOHEADER; 

/**
 * RGBTRIPLE
 *
 * This structure describes a color consisting of relative intensities of
 * red, green, and blue.
 *
 * Adapted from http://msdn.microsoft.com/en-us/library/aa922590.aspx.
 */
typedef struct
{
    BYTE  rgbtBlue;
    BYTE  rgbtGreen;
    BYTE  rgbtRed;
} __attribute__((__packed__))
RGBTRIPLE;
#包括
/**
*通用数据类型
*
*本节中的数据类型本质上是C/C++的别名
*基本数据类型。
*
*改编自http://msdn.microsoft.com/en-us/library/cc230309(第10页)。
*看http://en.wikipedia.org/wiki/Stdint.h 更多关于stdint.h的信息。
*/
typedef uint8_t字节;
类型定义uint32_t DWORD;
typedef int32t长;
typedef uint16_t字;
/**
*BITMAPFILEHEADER
*
*BITMAPFILEHEADER结构包含有关类型、大小、,
*以及包含DIB[设备独立位图]的文件布局。
*
*改编自http://msdn.microsoft.com/en-us/library/dd183374(第85节)。
*/
类型定义结构
{ 
字型;
德沃德尺寸;
保留字1;
保留字2;
德沃德;
}_uuu属性_uuu((uuu压缩_uuu))
位图文件头;
/**
*位图信息头
*
*BitMapInfo标头结构包含有关
*DIB[设备独立位图]的尺寸和颜色格式。
*
*改编自http://msdn.microsoft.com/en-us/library/dd183376(第85节)。
*/
类型定义结构
{
德沃德·比西泽;
长双宽;
长双峰高度;
单词双平面;
单词双比特计数;
德沃德双压缩;
德沃德·比西泽迈格;
长双峰渗透计;
长双渗透计;
德沃德·比克尔鲁斯;
德沃德·比克勒;
}_uuu属性_uuu((uuu压缩_uuu))
位图信息头;
/**
*RGBTRIPLE
*
*此结构描述了由颜色的相对强度组成的颜色
*红色、绿色和蓝色。
*
*改编自http://msdn.microsoft.com/en-us/library/aa922590.aspx.
*/
类型定义结构
{
字节rgbtBlue;
字节rgbtGreen;
字节rgbtRed;
}_uuu属性_uuu((uuu压缩_uuu))
RGBTRIPLE;
注意:我没有GetString的代码,但请假设它将返回一个字符串作为
char*

我遇到的问题是试图将每个像素的rgbtRed值转换为极端红色;我不知道怎么做。 请仅提供我如何转换rgbtRed

更新:拼图

欢迎来到都铎大厦。你的东道主约翰·博迪先生不合时宜地走到了尽头,他是犯规的受害者。要赢得这场比赛,你必须确定谁是谁

对你来说不幸的是(尽管对博迪先生来说更不幸的是),你拥有的唯一证据是一个名为clue.BMP的24位BMP文件,如下图所示,是博迪先生在临终前在电脑上突然出现的。在这个文件的红色“噪音”中隐藏着一幅whodunit的图画


很久以前,你扔掉了童年时代的那块红色塑料,它可以帮你解开这个谜团,因此你必须以计算机科学家的身份攻击它。

删除单引号
'ff'
是一个实现定义(或未定义)的整数,
0xff
是一个十六进制常量

这句话是我应该读的问题

            triple.rgbtRed = 0xff;

要在白色图像上获得漂亮的天蓝色,请执行以下操作:

triple.rgbtBlue = 0xff;

triple.rgbtGreen = 0xff;
如果需要红色刻度,请执行以下操作:

triple.rgbtBlue =  0x00;

triple.rgbtGreen = 0x00;
如果你想要紫色的鳞片:

triple.rgbtBlue = 0xff;

triple.rgbtGreen = 0x00;

谢谢,它编译成功了,但我猜我最初的假设是错误的,使rgbt红色没有真正的帮助。。我会在时间一到就更正,但是如果可能的话,我非常感谢你对破解谜题的帮助(用谜题更新主帖)啊。非常巧妙的拼图。红色滤光片不会自动对焦