DECOM pression停止其间并以零填充(黑色像素)的输出文件? [英] Decompression stops inbetween and output file filled with zeros(BLACK PIXELS)?

查看:176
本文介绍了DECOM pression停止其间并以零填充(黑色像素)的输出文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想申请DCT(离散余弦变换)的COM pression一个BMP(位图)文件。我有我在的Turbo C ++运行的C文件。这实际上不是COM pressing但我试图实现DCT和IDCT。在code是如下:

I am trying to apply DCT(discrete cosine transformation) compression on a bmp(bitmap) file. I have a c file which i am running in Turbo C++. This is not actually compressing but i was trying to implement the DCT and IDCT. The code is as follows:

/*
the image to be compressed is a bmp with 24 bpp and
with name "college4.bmp" of dimensions 200*160 ie 25*20- 8*8 blocks
o/p is college2.dat
format: 8 bit signed integers starting rowwise from 0,0 to 8,8
the coefficients order is blue,green,red
for the block no 1 then 2 and soon
*/

#include<stdlib.h>
#include<stdio.h>
#include<math.h>
#define WIDTH 25
#define HEIGHT 20

typedef struct {
    unsigned int type;
    unsigned long int filesize;
    unsigned int reserved1,reserved2;
    unsigned long int offset;
} BMPHEAD;

typedef struct {
    unsigned long int infosize;
    unsigned long int width,height;
    unsigned int planes,bitsperpixel;
    unsigned long int compression;
    unsigned long int sizeimage;
    long int xpelspermeter,ypelspermeter;
    unsigned long int colorused,colorimportant;
} INFOHEAD;

typedef struct {
    char rgbquad[4];
} colortable;

BMPHEAD bmphead;
INFOHEAD infohead;
FILE *bmp_fp1,*bmp_fp2;
int buf[WIDTH][8][8][3],buf1[WIDTH][8][8][3];
float pi=3.14159265,DCTcoeff[8][8][8][8];

void generatedctcoeff() {
    int y, i, j, x;
    for (i = 0; i < 8; i++) {
        for (j = 0; j < 8; j++) {
            for (x = 0; x < 8; x++) {
                for (y = 0; y < 8; y++) {
                    DCTcoeff[i][j][x][y] = cos(((2 * y + 1) * pi * j) / 16)
                            * cos(((2 * x + 1) * i * pi) / 16);
                }
            }
        }
    }
}

void outputtofile1() {                     // Write into college2.dat
    int i, j, x, y, blockno;              // One block at a time, buf contains pixel 
    int redcoef, greencoef, bluecoef;     // data of one row of blocks
    float gijred, gijgreen, gijblue, c, ci, cj;
    c = 1 / (sqrt(2));
    for (blockno = 0; blockno < WIDTH; blockno++) {
        for (i = 0; i < 8; i++) {
            for (j = 0; j < 8; j++) {
                gijred = 0;
                gijgreen = 0;
                gijblue = 0;
                for (x = 0; x < 8; x++) {
                    for (y = 0; y < 8; y++) {
                        gijblue = gijblue + DCTcoeff[i][j][x][y]
                                * buf[blockno][x][y][0];
                        gijgreen = gijgreen + DCTcoeff[i][j][x][y]
                                * buf[blockno][x][y][1];
                        gijred = gijred + DCTcoeff[i][j][x][y]
                                * buf[blockno][x][y][2];
                    }
                }
                ci = cj = 1.0;
                if (i == 0)
                    ci = c;
                if (j == 0)
                    cj = c;
                gijblue = ci * cj * gijblue / 4;
                gijgreen = ci * cj * gijgreen / 4;
                gijred = ci * cj * gijred / 4;
                bluecoef = (int) gijblue;
                greencoef = (int) gijgreen;
                redcoef = (int) gijred;
                fprintf(bmp_fp2, "%d %d %d ", bluecoef, greencoef, redcoef);
            }
        }
    } /* end of one block processing */
}

void compressimage() {
    int rowcount,x,y;
    bmp_fp1=fopen("college4.bmp","r");
    bmp_fp2=fopen("college2.dat","w");
    printf("generating coefficients...\n");
    generatedctcoeff();
    if(bmp_fp1==NULL) {
        printf("can't open");
        return;
    }
    printf("compressing....\n");
    fread(&bmphead,1,sizeof(bmphead),bmp_fp1);
    fread(&infohead,1,sizeof(infohead),bmp_fp1);
    fseek(bmp_fp1,bmphead.offset,SEEK_SET);
    for(rowcount=0;rowcount<HEIGHT;rowcount++) {
        for(y=0;y<8;y++) {
            for(x=0;x<infohead.width;x++) {
                buf[x/8][x%8][y][0]=(int)fgetc(bmp_fp1);
                buf[x/8][x%8][y][1]=(int)fgetc(bmp_fp1);
                buf[x/8][x%8][y][2]=(int)fgetc(bmp_fp1);
            }
        }
        outputtofile1();         //output contents of buf after dct to file
    }
    fclose(bmp_fp1);
    fclose(bmp_fp2);
}

void outputtofile2() {                                 //output buf to college3.bmp
    int i, j, x, y, blockno;                        // buf now contains coefficients
    float pxyred, pxygreen, pxyblue, c, ci, cj;     // a temp buffer buf1 used to 
    c = 1 / (sqrt(2));                              // store one row of block of
    for (blockno = 0; blockno < WIDTH; blockno++) { // decoded pixel values
        for (x = 0; x < 8; x++)
            for (y = 0; y < 8; y++) {
                pxyred = 0;
                pxygreen = 0;
                pxyblue = 0;
                for (j = 0; j < 8; j++) {
                    cj = 1.0;
                    if (j == 0)
                        cj = c;
                    for (i = 0; i < 8; i++) {
                        ci = 1.0;
                        if (i == 0)
                            ci = c;
                        pxyblue = pxyblue + ci * cj * DCTcoeff[i][j][y][x] * buf[blockno][i][j][0];
                        pxygreen = pxygreen + ci * cj
                        * DCTcoeff[i][j][y][x] * buf[blockno][i][j][1];
                        pxyred = pxyred + ci * cj * DCTcoeff[i][j][y][x] * buf[blockno][i][j][2];
                    }
                }
                pxyblue /= 4;
                pxygreen /= 4;
                pxyred /= 4;
                buf1[blockno][y][x][0] = pxyblue;
                buf1[blockno][y][x][1] = pxygreen;
                buf1[blockno][y][x][2] = pxyred;
            }
    }
    for (y = 0; y < 8; y++) {
        for (blockno = 0; blockno < WIDTH; blockno++)
            for (x = 0; x < 8; x++) {
                fprintf(bmp_fp2, "%c%c%c", (char) buf1[blockno][x][y][0],
                        (char) buf1[blockno][x][y][1],
                        (char) buf1[blockno][x][y][2]);
            }
    }
}

void uncompressimage() {
    int blue,green,red,rowcount,colcount,i,j;
    bmp_fp1=fopen("college2.dat","r");
    bmp_fp2=fopen("college3.bmp","w");
    printf("generating coefficients...\n");
    generatedctcoeff();
    if (bmp_fp1==NULL) {
        printf("open failed");
        return;
    }
    printf("uncompressing....\n");
    bmphead.type=0x4d42;
    bmphead.filesize=30518;
    bmphead.reserved1=0;
    bmphead.reserved2=0;
    bmphead.offset=sizeof(bmphead)+sizeof(infohead);
    infohead.infosize=sizeof(infohead);
    infohead.width=200;
    infohead.height=160;
    infohead.planes=1;
    infohead.bitsperpixel=24;
    infohead.compression=0;
    infohead.sizeimage=0;
    infohead.xpelspermeter=3780;
    infohead.ypelspermeter=3780;
    infohead.colorused=0;
    infohead.colorimportant=0;
    fwrite(&bmphead,sizeof(BMPHEAD),1,bmp_fp2);
    fwrite(&infohead,sizeof(INFOHEAD),1,bmp_fp2);
    for(rowcount=0;rowcount<HEIGHT;rowcount++) {
        for(colcount=0;colcount<WIDTH;colcount++) {
            for(i=0;i<8;i++) {
                for(j=0;j<8;j++) {
                    fscanf(bmp_fp1,"%d",&blue);
                    fscanf(bmp_fp1,"%d",&green);
                    fscanf(bmp_fp1,"%d",&red);
                    buf[colcount][i][j][0]=blue;
                    buf[colcount][i][j][1]=green;
                    buf[colcount][i][j][2]=red;
                }
            }
        }
        outputtofile2();
    }
    fclose(bmp_fp1);
    fclose(bmp_fp2);
}

int main() {
    printf("opening files...\n");
    compressimage();
    printf("opening files...again\n");
    uncompressimage();
    printf("successful decompression\nenter any key\n");
    return 0;
}

下面的图像,我使用的输入

Here is the image i am using as input

(IM srry网站转换的BMP PNG成,你可以将它转换回BMP使用它)
这里是所生成的图像

(im srry the site converted the bmp into png. You may convert it back to bmp to use it) Here is the image that is generated:

这被创建的文件college3.bmp是大小200x160和93.8 KB,但直到图像季度它的系数有正确去codeD,但后来该文件是用黑色填充像素。我已经采取了O / P的截图,因为它是说没有一个有效的bmp,​​同时上传。自2004年2月,我坐在这个问题。如果任何人都可以说,我这里有一个bug我会非常感激。我分析了输出文件,发现在那里的像素开始是黑色的地方一个EOF权利。我读了主题的一些其他问题,发现转换系数CI,CJ已经使用不当。虽然编码我也糊涂了与指数X,Y,i和j。所以我希望这个问题我将在几天内解决。

The file college3.bmp that gets created is of size 200x160 and of 93.8 kB but till quarter of the image it has decoded the coefficients correctly but later the file is filled with black pixels. I have taken a screenshot of the o/p as it was saying not a valid bmp while uploading. I am sitting on this problem since feb,2004. If anyone can say me where there is a bug i would be very thankful. I have analysed the output file and found an EOF right at the place where the pixels are starting to be black. I read some other questions on the topic and found that the conversion factors ci,cj have been used improperly. While coding i had also got confused with the indices x,y,i and j. So i hope this problem i will solve in a few days.

推荐答案

显然,在上述code中的问题是你如何打开文件。

Apparently, the problem in the above code is in how you open your files.

这是应该在你的code是什么(请注意,明确指定打开模式,二进制和文本):

This is what should be in your code (note the explicitly specified open modes, binary and text):

void compressimage() {
...
    bmp_fp1=fopen("college4.bmp","rb");
    bmp_fp2=fopen("college2.dat","wt");
...
}

void uncompressimage() {
...
    bmp_fp1=fopen("college2.dat","rt");
    bmp_fp2=fopen("college3.bmp","wb");
...
}

这一点,并稍微改动结构定义:

With that and slightly altered structure definitions:

#pragma pack(push,1)

typedef struct {
    unsigned short int type;
    unsigned long int filesize;
    unsigned short int reserved1,reserved2;
    unsigned long int offset;
} BMPHEAD;

typedef struct {
    unsigned long int infosize;
    unsigned long int width,height;
    unsigned short int planes,bitsperpixel;
    unsigned long int compression;
    unsigned long int sizeimage;
    long int xpelspermeter,ypelspermeter;
    unsigned long int colorused,colorimportant;
} INFOHEAD;

typedef struct {
    char rgbquad[4];
} colortable;

#pragma pack(pop)

我能成功编译使用3个不同的编译器(涡轮C ++,开在Watcom,GCC)程序并得到所需的输出图像。

I'm able to compile your program successfully using 3 different compilers (Turbo C++, Open Watcom, gcc) and get the desired output picture.

这篇关于DECOM pression停止其间并以零填充(黑色像素)的输出文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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