调整图像大小不受支持的文件格式 [英] Resize image unsupported file format

查看:74
本文介绍了调整图像大小不受支持的文件格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该用整数n(1和100之间)调整bmp的大小。

我目前没有任何编译错误或内存泄漏。

输出是错误的。



我会得到空白的bmp图像(虽然已调整大小)。

我也得到:



I am supposed to resize a bmp by the integer n ( betw 1 and 100).
I currently don't have any compilation errors or memory leaks.
The output would be wrong.

I would get blank bmp images (that are resized, though).
I also get:

~/workspace/pset4/bmp/ $ ./resize 3 small.bmp large.bmp
*** Error in `./resize': double free or corruption (top): 0x000000000125c250 ***
Aborted







非常感谢,c



我尝试了什么: < br $>





Thanks a lot,c

What I have tried:

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

         
         if ( ( n < 0 ) || ( n > 100 ) )
         {
           
            printf(" n must be a positive floating integer from 0.0 up till 100.0\n");
            return 1;
          
         }
         
    }

    // remember filenames
    char* infile = argv[2];
    char* outfile = argv[3];
    
     
    // open input file 
    FILE* input = fopen(infile, "r");
    
    if (input == NULL)
    {
        printf("Could not open %s.\n", infile);
        return 2;
    }

    // open output file
    FILE* output = fopen(outfile, "w");
    
    if (output == NULL)
    {
        fclose(input);
        fprintf(stderr, "Could not create %s.\n", outfile);
        return 3;
    }
    
     
     
    // read infile's BITMAPFILEHEADER
    BITMAPFILEHEADER bf;
    fread(&bf, sizeof(BITMAPFILEHEADER), 1, input);
 
     // read infile's BITMAPINFOHEADER
    BITMAPINFOHEADER bi;
    fread(&bi, sizeof(BITMAPINFOHEADER), 1, input);

    // 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(output);
        fclose(input);
        fprintf(stderr, "Unsupported file format.\n");
        return 4;
    }
    
    int originalwidth = bi.biWidth;
    int originalheight = bi.biHeight;
    int originalpadding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
 
    bi.biWidth = bi.biWidth * n;
    bi.biHeight = bi.biHeight * n;
       
    int newpadding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
     bi.biSizeImage = bi.biHeight * ( 3 * bi.biWidth + newpadding );
    bf.bfSize = bf.bfOffBits + bi.biSizeImage;    

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

    // write outfile's BITMAPINFOHEADER
    fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, output);
 
    // iterate over infile's scanlines
    for (int i = 0; i < abs(originalheight); i++)
    {
        int numberoflines = 0;
        // iterate over pixels in scanline
        for (int j = 0; j < abs(originalwidth); j++)
        {
             // temporary storage
            RGBTRIPLE triple;

            // read RGB triple from infile
            fread(&triple, sizeof(RGBTRIPLE), 1, input);
            
             
            // write RGB triple to outfile n times for horizontal resize
            for (int a = 0; a < n; a++)
            {
                
                fwrite(&triple, sizeof(RGBTRIPLE), 1, output);
                fclose(output);
                
                
                //temporary = malloc(sizeof(RGBTRIPLE));
                //temporary = fopen("tempmem.txt", "w");
                //fwrite(&triple, sizeof(RGBTRIPLE), 1, temporary);
                
            }
            
            //for (int b = 0; b < n-1; b++)
            //{
                //fwrite(temporary, sizeof(RGBTRIPLE),1, output);
            //}
                
        }

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

        // then add it back (to demonstrate how)
        for (int k = 0; k < newpadding; k++)
        {
            fputc(0x00, output);
        }
        
        if ( numberoflines < n - 1)
        {
            fseek( input, -1 * ( 3 * originalwidth + originalpadding ), SEEK_CUR);
        }
        
        numberoflines++;

    }

    // close infile
    
    //fclose( temporary);
    fclose(input);
   
    // close outfile
    fclose(output);

    // that's all folks
    return 0;
}

推荐答案

./ resize 3 small.bmp large.bmp
***错误。 / resize':double free或corruption(top):0x000000000125c250 ***
Aborted
./resize 3 small.bmp large.bmp *** Error in `./resize': double free or corruption (top): 0x000000000125c250 *** Aborted







非常感谢,c



我尝试过:






Thanks a lot,c

What I have tried:

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

         
         if ( ( n < 0 ) || ( n > 100 ) )
         {
           
            printf(" n must be a positive floating integer from 0.0 up till 100.0\n");
            return 1;
          
         }
         
    }

    // remember filenames
    char* infile = argv[2];
    char* outfile = argv[3];
    
     
    // open input file 
    FILE* input = fopen(infile, "r");
    
    if (input == NULL)
    {
        printf("Could not open %s.\n", infile);
        return 2;
    }

    // open output file
    FILE* output = fopen(outfile, "w");
    
    if (output == NULL)
    {
        fclose(input);
        fprintf(stderr, "Could not create %s.\n", outfile);
        return 3;
    }
    
     
     
    // read infile's BITMAPFILEHEADER
    BITMAPFILEHEADER bf;
    fread(&bf, sizeof(BITMAPFILEHEADER), 1, input);
 
     // read infile's BITMAPINFOHEADER
    BITMAPINFOHEADER bi;
    fread(&bi, sizeof(BITMAPINFOHEADER), 1, input);

    // 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(output);
        fclose(input);
        fprintf(stderr, "Unsupported file format.\n");
        return 4;
    }
    
    int originalwidth = bi.biWidth;
    int originalheight = bi.biHeight;
    int originalpadding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
 
    bi.biWidth = bi.biWidth * n;
    bi.biHeight = bi.biHeight * n;
       
    int newpadding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
     bi.biSizeImage = bi.biHeight * ( 3 * bi.biWidth + newpadding );
    bf.bfSize = bf.bfOffBits + bi.biSizeImage;    

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

    // write outfile's BITMAPINFOHEADER
    fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, output);
 
    // iterate over infile's scanlines
    for (int i = 0; i < abs(originalheight); i++)
    {
        int numberoflines = 0;
        // iterate over pixels in scanline
        for (int j = 0; j < abs(originalwidth); j++)
        {
             // temporary storage
            RGBTRIPLE triple;

            // read RGB triple from infile
            fread(&triple, sizeof(RGBTRIPLE), 1, input);
            
             
            // write RGB triple to outfile n times for horizontal resize
            for (int a = 0; a < n; a++)
            {
                
                fwrite(&triple, sizeof(RGBTRIPLE), 1, output);
                fclose(output);
                
                
                //temporary = malloc(sizeof(RGBTRIPLE));
                //temporary = fopen("tempmem.txt", "w");
                //fwrite(&triple, sizeof(RGBTRIPLE), 1, temporary);
                
            }
            
            //for (int b = 0; b < n-1; b++)
            //{
                //fwrite(temporary, sizeof(RGBTRIPLE),1, output);
            //}
                
        }

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

        // then add it back (to demonstrate how)
        for (int k = 0; k < newpadding; k++)
        {
            fputc(0x00, output);
        }
        
        if ( numberoflines < n - 1)
        {
            fseek( input, -1 * ( 3 * originalwidth + originalpadding ), SEEK_CUR);
        }
        
        numberoflines++;

    }

    // close infile
    
    //fclose( temporary);
    fclose(input);
   
    // close outfile
    fclose(output);

    // that's all folks
    return 0;
}


我假设你使用的是Windows。然后应该以二进制模式打开文件以禁用换行符:

I assume that you are using Windows. Then the files should be opened in binary mode to disable newline translation:
FILE* input = fopen(infile, "rb");
FILE* output = fopen(outfile, "wb");





这里你应该使用输入的填充:



Here you should use the padding of the input:

// skip over padding, if any
// Your code:
//fseek( input, newpadding, SEEK_CUR);
// Should be:
fseek( input, originalpadding, SEEK_CUR);





但是最重要的错误导致错误消息在这里:



But the most important error resulting in the error message is here:

// write RGB triple to outfile n times for horizontal resize
for (int a = 0; a < n; a++)
{
    fwrite(&triple, sizeof(RGBTRIPLE), 1, output);
    fclose(output);
}

您正在关闭输出文件!


这篇关于调整图像大小不受支持的文件格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆