Convert image to RGB float work on them- Save

Help
2010-06-08
2012-10-31
  • Daemon Reddius
    Daemon Reddius
    2010-06-08

    Hi this is the software i'm trying to create. Here's the source.

    I want to extract float values from a image file (if conversion is not
    possible
    exit the program) well it seems to work ok except 1 thing. Saving the image
    will save only half of the image expanded to fit all the original image.

    This is the source plus the two image. Compiling that and run will use the
    first image to generate the second image. I'm not working yet on this image
    so i expect that the final image will be the same. not a different image.

    what is wrong with this program?

    code:

    //This software is only test software and it come as is in the public domain
    
    #include <FreeImage.h>
    #include <iostream>
    
    using namespace std;
    
    void ErrorHandler(FREE_IMAGE_FORMAT fif, const char *message){
        cout<<endl<<"***";
        if(fif != FIF_UNKNOWN) {
            cout<<FreeImage_GetFormatFromFIF(fif)<<cout<<"Format\n";
        }
        cout<<message;
        cout<<"***\n";
    }
    
    typedef struct {
        float r;
        float g;
        float b;
    } fpixel;
    
    //global var. Image table
    
    fpixel pixel[1024][1024]; //able to store a picture up to 1024*1024 size
    
    int main(){
        FreeImage_SetOutputMessage(ErrorHandler);
        FREE_IMAGE_FORMAT fif;
        FIBITMAP *dib=0,*dib2=0;
        unsigned x,y,w,e;
    
        fif = FreeImage_GetFileType("redsquare.bmp", 0);
        if(fif == FIF_UNKNOWN)
            fif = FreeImage_GetFIFFromFilename("redsquare.bmp");
    
        if((fif != FIF_UNKNOWN) && FreeImage_FIFSupportsReading(fif))
                dib = FreeImage_Load(fif, "redsquare.bmp", 0);
        else{
            FreeImage_Unload(dib);
            FreeImage_Unload(dib2); 
            return -1;
        }
    
        dib2=FreeImage_ConvertToRGBF(dib);//KEEP the original
    
        e=FreeImage_GetHeight(dib2);//get dim of picture
        w=FreeImage_GetWidth(dib2);
    
        FREE_IMAGE_TYPE image_type = FreeImage_GetImageType(dib2); //get type to verify the conversion is successfull
    
        switch(image_type) {
                case FIT_RGBF:
                    cout<<"FIT_RGBF"<<endl;
                    for(y = 0; y < e; y++) {
                        FIRGBF *bits = (FIRGBF *)FreeImage_GetScanLine(dib2, y);
                        for(x = 0; x < w; x++) {   //memorize colors in a table
                            pixel[x][y].r = bits[x].red;  
                            pixel[x][y].g = bits[x].green;
                            pixel[x][y].b = bits[x].blue;
                        }
                    }
                    FreeImage_Unload(dib);
                    FreeImage_Unload(dib2);             
                    break;
                default:
                    FreeImage_Unload(dib);
                    FreeImage_Unload(dib2); 
                    return -2;
                    break;
        }
        cout<<"loaded"<<endl;
        //Work with image (final values are float ranged into 0:1 interval
    
        dib=FreeImage_AllocateT(FIT_RGBF, w, e, 32); //i tried here with any values. 4,8,16,24,32,48,64
    
        for(y = 0; y < e; y++) {
            FIRGBF *bits = (FIRGBF *)FreeImage_GetScanLine(dib,y);
            for(x = 0; x < w; x++) {
                bits[x].red = pixel[x][y].r;  
                bits[x].green = pixel[x][y].g;
                bits[x].blue = pixel[x][y].b;
    
            }
        }
        cout<<"transfered to struct"<<endl;
    
        if(FreeImage_Save(FIF_PNG,dib,"FINALIMAGE.png",PNG_Z_NO_COMPRESSION))
        {
            cout<<"Saved successfull(?)"<<endl; 
        }
        else
            return -3;
    
        cout<<"all successfull(?)"<<endl;
        return 0;       
    }
    

    start image

    final image (theorically must be the same of start image)

     
  • Daemon Reddius
    Daemon Reddius
    2010-06-08

    i posted the wronk link to image. here is the right images

    start image:

    final image:

     
  • For starters, you are saving rgb float as png.

    Look at docs p96 (p104) Table to see what format saves what