c - 指向 RGB 结构的指针数组始终指向第一个像素

标签 c pointers struct

bmp.h

// BMP-related data types based on Microsoft's own

#include <stdint.h>

// aliases for C/C++ primitive data types
// https://msdn.microsoft.com/en-us/library/cc230309.aspx
typedef uint8_t  BYTE;
typedef uint32_t DWORD;
typedef int32_t  LONG;
typedef uint16_t WORD;

// information about the type, size, and layout of a file
// https://msdn.microsoft.com/en-us/library/dd183374(v=vs.85).aspx
typedef struct
{
    WORD bfType;
    DWORD bfSize;
    WORD bfReserved1;
    WORD bfReserved2;
    DWORD bfOffBits;
} __attribute__((__packed__))
BITMAPFILEHEADER;

// information about the dimensions and color format
// https://msdn.microsoft.com/en-us/library/dd183376(v=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;

// relative intensities of red, green, and blue
// https://msdn.microsoft.com/en-us/library/dd162939(v=vs.85).aspx
typedef struct
{
    BYTE rgbtBlue;
    BYTE rgbtGreen;
    BYTE rgbtRed;
} __attribute__((__packed__))
RGBTRIPLE;

调整大小.c

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

#include "bmp.h"

int main(int argc, char *argv[])
{
    // ensure proper usage
    if (argc != 4)
    {
        fprintf(stderr, "Usage: ./resize n infile outfile\n");
        return 1;
    }

// remember factor and filenames
int factor = atoi(argv[1]);
char *infile = argv[2];
char *outfile = argv[3];

// open input file
FILE *inptr = fopen(infile, "r");
if (inptr == NULL)
{
    fprintf(stderr, "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;
}

// update outfile's BITMAPINFOHEADER
BITMAPINFOHEADER obi = bi;
obi.biWidth *= factor;
obi.biHeight *= factor;

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

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

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

// determine outfile's padding
int out_padding = (4 - (obi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

// remember pixels in an array
RGBTRIPLE * array[obi.biWidth];

// iterate over infile's scanlines
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{
    array[0] = NULL;
    int idx = 0;

    printf("----------------------\nReading from file.\n----------------------\n");
    // 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);

        // write to array n times
        for(int k = 0; k < factor; k++)
        {
            array[idx] = &triple;
            printf("scanline n°%d, pixel n°%d, array[%d] = %p, blue: %i, green: %i, red: %i\n", i, j, k, array[k], triple.rgbtBlue, triple.rgbtGreen, triple.rgbtRed);
            ++idx;
        }
    }
    printf("\nNow writing to outfile...\n\n");
    // for n times
    for (int x = 0; x < factor; x++)
    {
        // write array to outfile
        for(int y = 0; y < obi.biWidth; y++)
        {
            fwrite(array[y], sizeof(RGBTRIPLE), 1, outptr);
            printf("scanline n°%d, array[%d] = %p\n", i, y, array[y]);
        }

        // write padding to outfile
        for(int k = 0; k < out_padding; k++)
        {
            fputc(0x00, outptr);
        }
    }

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

// close infile
fclose(inptr);

// close outfile
fclose(outptr);

// success
return 0;
}

input file

output file

我正在尝试用 C 语言编写一个程序来调整 BMP 文件的大小。该程序采用三个参数(一个小 BMP 文件、要从该小 BMP 创建的大 BMP 文件的名称以及一个用于递增该小 BMP 的因子)。

为此,我将要重复 n 次的每个像素扫描线存储在一个指针数组中,我将使用该指针数组写入新的输出文件。我的问题是,虽然 RGB 值在到达白色像素时发生变化,但每个指针都有相同的地址。因此,输出文件只是一个绿色框,而不是中心有白色像素的绿色框。我不明白为什么,有人可以解释为什么地址总是相同的

最佳答案

RGBTRIPLE * array[obi.biWidth];

按照注释中的建议将其更改为 RGBTRIPLE array[obi.biWidth];RGBTRIPLE *array = malloc(...)

array[idx] = &triple;更改为array[idx] = Triple;

for(int y = 0; y < obi.biWidth; y++)
{
    fwrite(array[y], sizeof(RGBTRIPLE), 1, outptr);
}

您可以写入整个数组,而不是一次写入一个元素:

RGBTRIPLE *array = malloc(obi.biWidth * sizeof(RGBTRIPLE));
for(int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{
    for(int j = 0; j < bi.biWidth; j++)
    {
        RGBTRIPLE triple;
        fread(&triple, sizeof(RGBTRIPLE), 1, inptr);

        for(int k = 0; k < factor; k++)
            array[j * factor + k] = triple;
    }

    for(int x = 0; x < factor; x++)
    {
        fwrite(array, sizeof(RGBTRIPLE), obi.biWidth, outptr);

        for(int k = 0; k < out_padding; k++)
            fputc(0x00, outptr);
    }

    fseek(inptr, padding, SEEK_CUR);
}
free(array);

您还应该更改 BITMAPFILEHEADERbfSize 的值:

int out_padding = (4 - (obi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
bf.bfSize = 54 + (obi.biWidth * 3 + out_padding) * obi.biHeight;//total file size
fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);

关于c - 指向 RGB 结构的指针数组始终指向第一个像素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51348700/

相关文章:

pointers - Pointer Arithmetic on pointers to pointers 等

c - 为什么程序返回访问冲突?

pointers - 返回类型与 struct golang 相同

c++ - 如何将整个结构设置为空?

c - arp请求和回复使用c socket编程

在汇编中从 32 位值转换为 8 位值,反之亦然,给出段错误

c - 在可变参数函数的包装器中中继可选参数

c - 使用管道将两个矩阵相乘

C数组地址

c++ - 为什么结构的 sizeof 不等于每个成员的 sizeof 之和?