【问题标题】:C Resizing BMP File by Factor of N, what am I doing wrong?C按N因子调整BMP文件大小,我做错了什么?
【发布时间】:2017-08-25 23:34:11
【问题描述】:

C 新手。使用我的代码调整 24 位未压缩位图的大小时遇到​​问题。我正在尝试将这个图像放大 n 倍,我觉得我已经接近让它正常工作但我输出的图像仍然不正确。

我可以发布我用于测试的输入 24 位未压缩 BMP (small.bmp) 的图片、我的程序输出的图片 (resized.bmp) 以及 small.bmp 的正确图片按一个因子缩放4 应该看起来像,如果这会有所帮助。随便问问。

resize.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 scale infile outfile\n");
        return 1;
    }

    int n = atoi(argv[1]);
    // remember filenames
    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;
    }
    int oldpadding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
    bi.biWidth = bi.biWidth * n;
    bi.biHeight = bi.biHeight * n;

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

bi.biSizeImage = ((sizeof(RGBTRIPLE) * bi.biWidth) + padding) * abs(bi.biHeight);
bf.bfSize = bi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
// write outfile's BITMAPFILEHEADER
fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);

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


    // iterate over infile's scanlines
    for (int i = 0, biHeight = abs(oldHeight); i < biHeight; i++)
    {

        // store scanline in an array pixel by pixel for vertical scaling.
        RGBTRIPLE scanline[bi.biWidth - 1];

        // iterate over pixels in scanline
        for (int j = 0; j < bi.biWidth; j++)
        {
            //check if we've hit padding in original bmp.
            if (j % oldpadding == 0 && j != 0) 
            {
                //skip the padding.
                fseek(inptr, oldpadding, SEEK_CUR);
            }

            // temporary storage
            RGBTRIPLE triple;

            fread(&triple, sizeof(RGBTRIPLE), 1, inptr);

            for (int h = 0; h < n; h++)
            {
                //scale horizontally, save each scanline pixel to our array.
                scanline[j] = triple;
            }

        }

        for (int x = 0; x < n; x++)
        {
            //write scanlines n - 1 times.
            for (int y = 0; y < bi.biWidth; y++)
            {
                    fwrite(&scanline[y], sizeof(RGBTRIPLE), 1, outptr);
            }

            //write padding if any for current scanline.
            for (int z = 0; z < padding; z++)
            {
                fputc(0x00, outptr);
            }
        }

    }

    // close infile
    fclose(inptr);

    // close outfile
    fclose(outptr);

    // success
    return 0;
}

bmp.h

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

#include <stdint.h>

/**
 * Common Data Types 
 *
 * The data types in this section are essentially aliases for C/C++ 
 * primitive data types.
 *
 * Adapted from https://msdn.microsoft.com/en-us/library/cc230309.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 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; 

/**
 * BITMAPINFOHEADER
 *
 * The BITMAPINFOHEADER structure contains information about the 
 * dimensions and color format of a DIB [device-independent bitmap].
 *
 * Adapted from 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; 

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

【问题讨论】:

  • 删除了一些我用来测试的不必要的行。
  • SO: this question 似乎也有同样的错误(虽然它更简单一点)。扫描一行时:j &lt; bi.biWidth 应该是j &lt; bi.biWidth / n。您应该为两个轴迭代一个图像(在本例中为源)(不确定是否还有更多问题)。另外:您不是在检查函数返回码是否有错误,而且您一次读取三倍(效率低下),您可以(至少)读取一行。
  • @CristiFati 将条件更改为您提到的内容,仍然不是正确的输出。在这种情况下,效率

标签: c image algorithm resize


【解决方案1】:

您的代码混合了两种缩放技术:1) 在数组中缩放然后将其写出,2) 以原始大小读取数组并在写出时进行缩放。但是,您对第一种技术的实现实际上并没有进行任何缩放:

for (int h = 0; h < n; h++)
{
    //scale horizontally, save each scanline pixel to our array.
    scanline[j] = triple;
}

因为你不增加 j,你只是将相同的三元组覆盖到相同的位置 n 次。它根本不做任何缩放。我建议放弃第一种技术,只关注第二种。首先,修复你的数组声明:

RGBTRIPLE scanline[bi.biWidth];

那么当你读入的时候,只需将三元组写入正确的位置,没有任何循环:

scanline[j] = triple;

当你把它写出来时,你的循环是错误的。您想循环遍历像素并将每个像素写出 n 次,而不是遍历图像 n 次并写出所有像素(这将重复图像 n 次,而不是将其缩放 n 次)。然后将这一切包装在一个循环中,以将每一行输出 n 次。

for (int y = 0; y < n; y++) // repeat each row n times
{
    for (int x = 0; x < bi.biWidth; x++) // iterate over pixels
    {
         for (int r = 0; r < n; r++) // repeat each pixel n times
             fwrite(&scanline[x], sizeof(RGBTRIPLE), 1, outptr);
    }
    // write padding if any for current scanline.
    for (int z = 0; z < padding; z++)
    {
        fputc(0x00, outptr);
    }
}

这是缩放图像的基本算法。您可以通过执行块 I/O 读取和写入而不是在循环中调用 fwrite 来提高速度,但正如您在评论中指出的那样,正确性 > 效率。您可能还有一些填充问题,我还没有解决。

【讨论】:

  • 感谢您的详细回复。我还发现了我没有增加 j 的事实,只是我忽略了一个愚蠢的事情。我想你有时必须通过失败来学习:P
猜你喜欢
  • 2012-05-01
  • 2010-10-16
  • 2017-05-15
  • 1970-01-01
  • 2012-06-27
  • 2015-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多