發新話題

wavelet三階轉換與反轉換問題

wavelet三階轉換與反轉換問題

請問如何做出wavelet三階轉換與反轉換呢?
由於本人是個程式很弱,這也是拿別人的code來改而已,懇請各位該如何修改呢
以下程式碼是做出一階小波轉換的source code
由於window player無法開啟輸出的圖檔 所以我用im player去開啟

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include "wavelet_program.h"

typedef long UINT32;//4bytes
typedef unsigned short int UINT16;//2bytes
typedef unsigned char BYTE;//1byte

/*float 4byte*/

#define HEADER_SIZE 14
#define INFO_SIZE 40
#define DataSize(bmp) (bmp->width*bmp->height)

//結構BMP 宣告變數區
typedef struct BMP {
    BYTE header[HEADER_SIZE];   
    BYTE info[INFO_SIZE];
    // Header
    UINT16 signature; // Magic Number = "BM" = 0x4D42
    UINT32 fileSize; // File size in bytes //imagesize+headersize+infosize
    UINT32 hreserved; // unused (=0)
    UINT32 dataOffset; // File offset to Raster Data
    // InfoHeader
    UINT32 size; // Size of InfoHeader =40
    UINT32 width; // Bitmap Width
    UINT16 height; // Bitmap Height
    UINT16 planes; // Number of Planes (=1)
    UINT16 bitsPerPixel; // Bits per Pixel, 1, 4, 8, 16, 24
    UINT32 compression; // Type of Compression, 0 = BI_RGB no compression, 1 = BI_RLE8 8bit RLE encoding, 2 = BI_RLE4 4bit RLE encoding
    UINT32 imageSize; // (compressed) Size of Image, It is valid to set this =0 if Compression = 0
    UINT32 xPixelsPerM; // horizontal resolution: Pixels/meter
    UINT32 yPixelsPerM; // vertical resolution: Pixels/meter
    UINT32 colorsUsed; // Number of actually used colors
    UINT32 colorsImportant; // Number of important colors , 0 = all
    // ColorTable : present only if Info.BitsPerPixel <= 8 colors should be ordered by importance
    BYTE blue; // Blue intensity
    BYTE green; // Green intensity
    BYTE red; // Red intensity
    BYTE creserved; // unused (=0)
    // Raster Data
        BYTE *data;
} BMP;

typedef struct Pixel {
    BYTE R;
    BYTE G;
    BYTE B;
} Pixel;

#define U16(x)    ((unsigned short) (x))
#define U32(x)    ((int) (x))
#define B2U16(bytes,offset)  (U16(bytes[offset]) | U16(bytes[offset+1]) << 8)
#define B2U32(bytes,offset)  (U32(bytes[offset]) | U32(bytes[offset+1]) << 8 | \
                   U32(bytes[offset+2]) << 16 | U32(bytes[offset+3]) << 24)

void bmpLoad(BMP *bmp, char *filename) {
   FILE *file;
   BYTE header[14];
   BYTE info[40];
   //讀取影像檔
   if ((file = fopen(filename, "rb") ) == NULL ) {
      fprintf(stderr, "Error: bmpLoad(), File open fail!\n");     
   }
   else
//讀取 filename進file
   fread(header, 1, HEADER_SIZE, file);
   fread(info, 1, INFO_SIZE, file);
   memcpy(bmp->header, header, HEADER_SIZE);
   memcpy(bmp->info, info, INFO_SIZE);
   // Header
   bmp->signature = B2U16(header,0); assert(bmp->signature == 0x4D42);
   bmp->fileSize = B2U32(header,2);
   bmp->dataOffset = B2U32(header,10);
   // InfoHeader
   bmp->size = B2U32(info,0); assert(bmp->size==40);
   bmp->width = B2U32(info,4);
   bmp->height = B2U32(info,8);
   bmp->planes = B2U16(info,12); assert(bmp->planes==1);
   bmp->bitsPerPixel = B2U16(info,14); assert(bmp->bitsPerPixel==8);
   bmp->compression = B2U32(info,16);
   bmp->imageSize = B2U32(info,20);
   bmp->xPixelsPerM = B2U32(info,24);
   bmp->yPixelsPerM = B2U32(info,28);
   bmp->colorsUsed = B2U32(info,32);
   bmp->colorsImportant = B2U32(info,36);

   bmp->data = (BYTE*)malloc(DataSize(bmp));//malloc size

   fseek(file, bmp->dataOffset, SEEK_SET);
   fread((bmp->data), 1, DataSize(bmp), file);//file寫入bmp->data
      fclose(file);
}

void bmpPrint(BMP *bmp) {
   printf("==== Header ====\n");
   printf("Signature = %04X\n", bmp->signature); // 0x4d42 = BM
   printf("FileSize = %ld \n", bmp->fileSize);
   printf("DataOffset = %ld \n", bmp->dataOffset);
   printf("==== Info ======\n");
   printf("size = %ld \n", bmp->size);
   printf("Width = %ld \n", bmp->width);
   printf("Height = %ld \n", bmp->height);
   printf("Planes = %d \n", bmp->planes);
   printf("BitsPerPixel = %d \n", bmp->bitsPerPixel);
   printf("Compression = %ld \n", bmp->compression);
   printf("ImageSize = %ld \n", bmp->imageSize);
   printf("XpixelsPerM = %ld \n", bmp->xPixelsPerM);
   printf("YpixelsPerM = %ld \n", bmp->yPixelsPerM);
   printf("ColorsUsed = %ld \n", bmp->colorsUsed);
   printf("ColorsImportant = %ld \n", bmp->colorsImportant);


}

void bmpSetPixel(BMP *bmp, int x, int y, BYTE R, BYTE G, BYTE B) {
    Pixel *pixel;
    int idx = y * bmp->width + x;
    pixel = (Pixel*) &bmp->data[idx * sizeof(Pixel)];//pixel一點即帶RGB三數據,RGB資訊進入data
    pixel->R = R;
    pixel->G = G;
    pixel->B = B;
}

void bmpSetBox(BMP *bmp, int x, int y, int w, int h, BYTE R, BYTE G, BYTE B) {
    int i,j;
    for (i=0; i< 0+w; i++)
        for (j=0; j<h; j++)
            bmpSetPixel(bmp, x+i, y+j, R, G, B);//一點一點儲存RGB資訊進去bmpsetpixel
}

int bmpSave(BMP *bmp, char *fileout, unsigned char *wavetrans) {
    FILE *file;
        //寫入影像檔
    if ((file = fopen(fileout, "wb") ) == NULL ) {
        fprintf(stderr, "Error: bmpSave(), File create fail!\n");
        exit(0);
    }//fileout存到file裡

       
/*   fwrite(bmp->header, 1, HEADER_SIZE, file);
    fwrite(bmp->info, 1, INFO_SIZE, file);
        fseek(file, bmp->dataOffset, SEEK_SET);*/

        fwrite(wavetrans, sizeof(unsigned char), DataSize(bmp), file);//wavetrans寫入file
    fclose(file);
}
void onetotwo(BMP* bmp, char* filename, BYTE** wavein)
{
        FILE *file;
        unsigned char *bmptrans;
        bmptrans= new unsigned char[bmp->width*bmp->height];

        if ((file = fopen(filename, "rb") ) == NULL ) {
        fprintf(stderr, "error!");
        exit(0);
    }
        else       

   fseek(file, bmp->dataOffset, SEEK_SET);
   fread(bmptrans, 1, DataSize(bmp), file);

        for(int j=0;j<bmp->height;j++)
                for(int i=0;i<bmp->width;i++)
                        wavein[j]=bmptrans[j*bmp->width+i];


        fclose(file);
}

void twotoone(BMP* bmp, BYTE** waveout, unsigned char *wavetrans)
{
       

        for(int j=0;j<bmp->height;j++)
                for(int i=0;i<bmp->width;i++)
                        {
                                if(waveout[j]>255) wavetrans[j*bmp->width+i]=255;
                                else if(waveout[j]<0) wavetrans[j*bmp->height+i]=0;
                                else  
                                        wavetrans[j*bmp->height+i]=waveout[j];

                }
return;

}



int main( int argc, char *argv[]) {
    BMP bmp;
        Pixel pixel;
        char *filename="lenagray.bmp";//input
        char *fileout="lenaout.bmp";//output

                BYTE** wavehalf;
                BYTE** waveout;
                BYTE** wavein;

                               
                unsigned char *wavetrans;
                        unsigned char *bmptrans;


        bmpLoad(&bmp, filename);
    bmpSetBox(&bmp, 0, 0, 20, 20, 0x66, 0x33, 0x99);//RGB形成

                wavein=new BYTE*[bmp.width];
        for(int i =0;i<bmp.width;i++)
                wavein=new BYTE[bmp.height];
        onetotwo( &bmp,  filename, wavein);
       
       

        /*        pixel= new BYTE **[pixel.R];
                for (int i = 0; i < pixel.R; i++) {
                        pixel = new BYTE *[pixel.G];
                        for (int j = 0; j < pixel.G; j++) {
                                pixel[j] = new BYTE[pixel.B];
}
}*/



        wavehalf=new BYTE*[(bmp.width)];
        for(int i=0; i<bmp.width;i++)
                wavehalf=new BYTE[bmp.height];

        waveout=new BYTE*[(bmp.width)];
                for(int i=0; i<bmp.width;i++)
                waveout=new BYTE[bmp.height];

        wavetrans= new unsigned char [bmp.height*bmp.width];



transrows(wavehalf,wavein ,bmp.width ,bmp.height);
transcols(waveout, wavehalf, bmp.width, bmp.height);




     bmpPrint(&bmp);

        twotoone(&bmp,waveout,wavetrans);
           system("pause");

        bmpSave(&bmp, fileout, wavetrans);//儲存
    return 0;
}

wavelet_program.cpp 與 wavelet_program.h,由於文章長度限制這邊就不加進來了

wavelet_program.cpp當中包含了synthrows 和synthcols(BYTE** dest, BYTE** sour, unsigned int w, unsigned int h)。

TOP

發新話題

本站所有圖文均屬網友發表,僅代表作者的觀點與本站無關,如有侵權請通知版主會盡快刪除。