Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Problem to save image buffer with libpng in C


  • Please log in to reply
1 reply to this topic
BrandonHotkey
  • Members
  • 691 posts
  • Last active: Oct 21 2015 09:41 PM
  • Joined: 21 May 2012

It's great you have this section here. I am helpless and cannot find any help on C forums. I reminded you are nice ppl here so this is a chance. I am writing program to convert images using libpng.

I need to find out why my program can save image (e.g. output_hsv.jpg) after rgb2hsv conversion but then when I load it as output_hsv.jpg it cannot save it back as output_rgb.jpg.

Once again. I run my program and read_png_file(argv[1], &data) will convert rgb to hsv and save it as normal 3 channel png image. Then I run the command again and I load the new image by read_png_file(argv[1], &data) . It converts the data and I can watch the data->raw_pointers buffer. I can watch it also when I call write_png_file(argv[2], &data) but this time, the just changed data are not saved but instead of it the original hsv image is saved. I would like to know what is the case that the just changed buffer is not saved but instead the original is saved. Any solution?

main.c
http://paste.ofcode....TjxAifxaWyt9udu
data type
http://paste.ofcode....pivyLsBNg7YWdyZ
The conversion functions - no matter how conversion it does:

#include <png_conversions.h>
#include <math.h>
void png_rgb2hsv(pDATA data)
{
....some code ...
data->row_pointers[y][x]=roundf(H*0.002777*255);
data->row_pointers[y][x+1]=roundf(S*255);
data->row_pointers[y][x+2]=V;
// [b]THIS WORKS PERFECTLY! [/b]This buffer will be saved to file successfully.
}


void png_hsv2rgb(pDATA data)
{
    float R, G, B, H, S, V; // ... etc.
    int i;
    H=S=V=0;
    int c = data->channels;
    int x,y;

for (y=0; y<data->height; y++)
    {
    for (x=0; x<data->row_bytes_len; x=x+c)
        {
// some data
data->row_pointers[y][x]=R;
data->row_pointers[y][x+1]=G;
data->row_pointers[y][x+2]=B;
// [b]This is never saved to file[/b]
       }  
   }
}

// The functions should be OK, data are in buffer... But the write function does not what I expect


BrandonHotkey
  • Members
  • 691 posts
  • Last active: Oct 21 2015 09:41 PM
  • Joined: 21 May 2012

You can delete it, problem was inside the png_hsb2rgv function.