saving as png problem

  • Tun

    Tun - 2014-03-04

    I saved some raw ARPG images as png files,and got some problem, most of the new png files display gray and others are fully ok.How could this happen?Did anyone come to the same problem, then how to solve it.Thx advance.Below is my code:

    int TexW;
    int TexH;
    int x =0;
    int y =0; 
    bool bSuccess = false;
    bitmap =FreeImage_Allocate(58, 59, 32, 8, 8, 8);


    TexW = FreeImage_GetWidth(bitmap);
    TexH = FreeImage_GetHeight(bitmap);
    for(y = TexH-1 ;y>=0; y--)
        BYTE *bits = FreeImage_GetScanLine(bitmap, TexH-1-y);
        for(x =0 ;x < TexW; x++)
            bits[0] =mpixels[(y*TexW+x)*4+2];
            bits[1] =mpixels[(y*TexW+x)*4+1];
            bits[2] =mpixels[(y*TexW+x)*4+0];
            bits[3] =mpixels[(y*TexW+x)*4+3];


    bSuccess = FreeImage_Save(FIF_PNG, bitmap, imgSavePath, PNG_DEFAULT);


    I wished I use the FreeImage Lib correctly.

    Last edit: Tun 2014-03-15
  • Hervé Drolon

    Hervé Drolon - 2014-03-08

    What is the type used for y & x ? int or unsigned int ?
    note that using y-- in a for() loop with an unsigned may cause bugs difficult to find.

  • Tun

    Tun - 2014-03-15

    Thanks for your replay.
    Both X and Y are int.
    My problem is :
    i want to save some gpu textures as png files using FreeImage lib, but part of them were saved incorrectly:
    like the one in attachment.

    the file : image.texture is the raw texture data (width : 58 height : 59 4-byte per pixel)
    the file : right.png is the result rendered by my programe
    the file : wrong.png is the result saved as png by using FreeImage

    Last edit: Tun 2014-03-16
  • Tun

    Tun - 2014-03-15

    You can see that ,seems like it stopped encoding from a fixed position.

  • Hervé Drolon

    Hervé Drolon - 2014-03-15

    You says your image has a bitdepth of 4-bit, while your mpixels array assume a bitdepth of 4 bytes (i.e. 32-bit). Did you made the 4-bit to 32-bit conversion while loading the "image.texture" file ?

  • Tun

    Tun - 2014-03-16

    Thx for soon reply.

    Sorry,it's 4-byte,not 4-bit.

    The file "image.texture" is a raw RGBA texture image with 4-byte per pixel.I copy its data to a FreeImage's bitmap :

    ReadLen = fread(mpixels, nFileLen, 1, fp); // read image.texture data
    bitmap =FreeImage_Allocate(58, 59, 32, 8, 8, 8); // create a bitmap
    for (...) // fill bitmap pixels
    // Get each line from bitmap
    BYTE *bits = FreeImage_GetScanLine(bitmap, TexH-1-y);
    for (...) {
    bits[0] =mpixels[(y * TexW + x)*4+2]; // B
    bits[1] =mpixels[(y * TexW + x)*4+1]; // G
    bits[2] =mpixels[(y * TexW + x)*4+0]; // R
    bits[3] =mpixels[(y * TexW + x)*4+3]; // A

    // save the bitmap as a png file
    // I think something wrong happened in this function.
    // Because some raw textures could be saved as png correctly.
    bSuccess = FreeImage_Save(FIF_PNG, bitmap, imgSavePath, PNG_DEFAULT);

    Last edit: Tun 2014-03-16
  • Johnny Petersen

    Johnny Petersen - 2014-03-16

    Both right.png and wrong.png are coded BitDepth 8 and ColorType 2. In PNG terms, ColorType 2 can be 24-BPP or 48 BPP, but here since they are BitDepth 8, they can only be 24 BPP. As such, perhaps you should try to call FreeImage_ConvertTo24Bits before FreeImage_Save.

  • Tun

    Tun - 2014-03-17

    I think i've found the point.

    There's nothing wrong with FreeImage Lib.

    While fread the image.textue by using arg "rb" instead of "r" ,everything is ok now.

    Thank guys all the same.


Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.

No, thanks