List Info

Thread: Using libpng to write a png file




Using libpng to write a png file
user name
2006-05-23 17:03:21
Hi all,

I am trying to understand how to use libpng to write out a png file. Here is my script:

int dm_write_pngfile(char *filename,  ;           ;           ;           ;           ;           ;           ;           ;           ;           ;    
         ;           ; unsigned char *byte_image,         ;           ;           ;           ;           ;           ;           ;           ;        
         ;           ; int nx, int ny, char *error_string)     ;           ;           ;           ;           ;           ;           ;           ;   
{ int i;       ;           ;           ;           ;           ;           ;           ;           ;           ;           ;           ;           ;     
  FILE *fp = fopen(filename,"wb");  ;           ;           ;           ;           ;           ;           ;           ;           ;           ;      
  if (!fp) return(DM_FILEIO_FAILURE);   ;           ;           ;           ;           ;           ;           ;           ;           ;           ;  
         ;           ;           ;           ;           ;           ;           ;           ;           ;           ;           ;           ;           ;
  png_structp png_ptr = png_create_write_struct         ;           ;           ;           ;           ;           ;           ;           ;        
        (PNG_LIBPNG_VER_STRING, (png_voidp)error_string,NULL,NULL);       ;           ;           ;           ;           ;           ;           
  if (!png_ptr) return(DM_FILEIO_FAILURE);   ;           ;           ;           ;           ;           ;           ;           ;           ;        
         ;           ;           ;           ;           ;           ;           ;           ;           ;           ;           ;           ;           ;
  png_infop info_ptr = png_create_info_struct(png_ptr);  ;           ;           ;           ;           ;           ;           ;           ;       
  if (!info_ptr) {         ;           ;           ;           ;           ;           ;           ;           ;           ;           ;           ;    
    png_destroy_write_struct(&png_ptr,(png_infopp)NULL);         ;           ;           ;           ;           ;           ;           ;          
    return(DM_FILEIO_FAILURE);   ;           ;           ;           ;           ;           ;           ;           ;           ;           ;         
  }         ;           ;           ;           ;           ;           ;           ;           ;           ;           ;           ;           ;        
  if (setjmp(png_jmpbuf(png_ptr))) {         ;           ;           ;           ;           ;           ;           ;           ;           ;        
    png_destroy_write_struct(&png_ptr, &info_ptr);   ;           ;           ;           ;           ;           ;           ;           ;           ;
    fclose(fp);           ;           ;           ;           ;           ;           ;           ;           ;           ;           ;           ;     
    return(DM_FILEIO_FAILURE);   ;           ;           ;           ;           ;           ;           ;           ;           ;           ;         
  }         ;           ;           ;           ;           ;           ;           ;           ;           ;           ;           ;           ;        
  png_init_io(png_ptr, fp);   ;           ;           ;           ;           ;           ;           ;           ;           ;           ;           
  png_bytep *row_ptr;    ;           ;           ;           ;           ;           ;           ;           ;           ;           ;           ;      
  row_ptr = (png_bytep*)malloc(ny*sizeof(png_bytep));      ;           ;           ;           ;           ;           ;           ;           ;     
 for (i=0; i<ny; i++) row_ptr[i]=(png_byte*)malloc(nx*sizeof(png_byte));&nbsp; &nbsp;
 for (i=0; i<ny; i++) row_ptr[i]= byte_image+i*nx*sizeof(png_byte); &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp;  png_set_IHDR(png_ptr,info_ptr,nx,ny,1,PNG_COLOR_TYPE_GRAY, &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;  
 &nbsp; &nbsp; &nbsp;  PNG_INTERLACE_NONE,PNG_COMPRESSION_TYPE_BASE, &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp;
 &nbsp; &nbsp; &nbsp;  PNG_FILTER_TYPE_BASE); &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   
 &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp;   png_set_rows(png_ptr,info_ptr,row_ptr);&nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp;
  png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);&nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ;
  png_write_end(png_ptr, NULL);&nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   
  png_destroy_write_struct(&png_ptr, &info_ptr);   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp;
  return(DM_FILEIO_SUCCESS);   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ;
} &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp;

I created a simple 2D gaussian array, converted it into unsigned array. But this script didn't write it out correctly. It wrote out a black png image with two bright lines on the right. I checked the gaussian array and printed out the row_ptr after assign the addresses of each row, and they are all right. So the problem occurs when I using png_set_IHDR, png_set_rows or png_write_png. Does anyone have any suggestion on it? Thanks a lot!

Huijie


Be a chatter box. Enjoy free PC-to-PC calls with Yahoo! Messenger with Voice.
Using libpng to write a png file
user name
2006-05-23 17:54:17

--- miao huijie <camellia888yahoo.com> wrote:

> Hi all,
> 
> I am trying to understand how to use libpng to write
> out a png file. Here is my script:
> 
> int dm_write_pngfile(char *filename,                
>                                                     
>                                     
>                      unsigned char *byte_image,     
>                                                     
>                                     
>                      int nx, int ny, char
> *error_string)                                      
>                                                
> { int i;                                            
>                                                     
>                                     
>   FILE *fp = fopen(filename,"wb");         
        
>                                                     
>                                     
>   if (!fp) return(DM_FILEIO_FAILURE);               
>                                                     
>                                     
>                                                     
>                                                     
>                                     
>   png_structp png_ptr = png_create_write_struct     
>                                                     
>                                     
>         (PNG_LIBPNG_VER_STRING,
> (png_voidp)error_string,NULL,NULL);                 
>                                                     
>     
>   if (!png_ptr) return(DM_FILEIO_FAILURE);          
>                                                     
>                                     
>                                                     
>                                                     
>                                     
>   png_infop info_ptr =
> png_create_info_struct(png_ptr);                    
>                                                     
>              
>   if (!info_ptr) {                                  
>                                                     
>                                     
>    
>
png_destroy_write_struct(&png_ptr,(png_infopp)NULL);
>                                                     
>                                 
>     return(DM_FILEIO_FAILURE);                      
>                                                     
>                                     
>   }                                                 
>                                                     
>                                     
>   if (setjmp(png_jmpbuf(png_ptr))) {                
>                                                     
>                                     
>     png_destroy_write_struct(&png_ptr,
&info_ptr);  
>                                                     
>                                     
>     fclose(fp);                                     
>                                                     
>                                     
>     return(DM_FILEIO_FAILURE);                      
>                                                     
>                                     
>   }                                                 
>                                                     
>                                     
>   png_init_io(png_ptr, fp);                         
>                                                     
>                                     
>   png_bytep *row_ptr;                               
>                                                     
>                                     
>   row_ptr =
> (png_bytep*)malloc(ny*sizeof(png_bytep));           
>                                                     
>                         
>  for (i=0; i<ny; i++)
> row_ptr[i]=(png_byte*)malloc(nx*sizeof(png_byte));  

This for loop should not be performed. Your row_ptr
array is going to point into the memory that you
passed in, so you don't have to allocate space for
each row.

Also (you may be simplifying your code for our
benefit), you should always check the return of memory
allocation calls.

>  
>  for (i=0; i<ny; i++) row_ptr[i]=
> byte_image+i*nx*sizeof(png_byte);                   
>                                                     
>                                                     
>          
>
png_set_IHDR(png_ptr,info_ptr,nx,ny,1,PNG_COLOR_TYPE_GRAY,


That fifth argument to png_set_IHDR() is the bitdepth.
You set 1 bit, which means that the output image will
be able to represent two values 0 and 1. 1 is valid,
but you probably want to use something larger. Valid
values are: 1, 2, 4, 8, 16.

The short and correct answer is probably set this to 8
and that will solve your problem.

But, I'm not positive, but libpng may assume that your
source image is the same bit depth that you selected
for the output file which makes sense. So if you
source image is 8 bit grayscale and you select 4 bit
output depth, you may have to use png_set_shift().
I've only used libpng for 8 bit color images though.


>                                                     
>                             
>        
> PNG_INTERLACE_NONE,PNG_COMPRESSION_TYPE_BASE,       
>                                                     
>                             
>         PNG_FILTER_TYPE_BASE);                      
>                                                     
>                                     
>                                                     
>                                       
> png_set_rows(png_ptr,info_ptr,row_ptr);             
>                                                     
>                                   
>   png_write_png(png_ptr, info_ptr,
> PNG_TRANSFORM_IDENTITY, NULL);                      
>                                                     
>  
>   png_write_end(png_ptr, NULL);                     
>                                                     
>                                     
>   png_destroy_write_struct(&png_ptr,
&info_ptr);    
>                                                     
>                                     
>   return(DM_FILEIO_SUCCESS);                        
>                                                     
>                                     
> }              
> 
> I created a simple 2D gaussian array, converted it
> into unsigned array. But this script didn't write it
> out correctly. It wrote out a black png image with
> two bright lines on the right. I checked the
> gaussian array and printed out the row_ptr after
> assign the addresses of each row, and they are all
> right. So the problem occurs when I using
> png_set_IHDR, png_set_rows or png_write_png. Does
> anyone have any suggestion on it? Thanks a lot!
> 
> Huijie
> 
> 		
> ---------------------------------
> Be a chatter box. Enjoy free PC-to-PC calls  with
> Yahoo! Messenger with Voice.


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection
around 
http://mail.yahoo.com 


-------------------------------------------------------
All the advantages of Linux Managed Hosting--Without the
Cost and Risk!
Fully trained technicians. The highest number of Red Hat
certifications in
the hosting industry. Fanatical Support. Click to learn more
http://sel.as-us.falkag.net/
sel?cmd=lnk&kid=107521&bid=248729&dat=121642
_______________________________________________
png-mng-implement mailing list
png-mng-implementlists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/png-m
ng-implement
Using libpng to write a png file
user name
2006-05-23 18:44:25
Thanks a lot!
It was the bitdepth causing the problem. Thanks again!

Huijie

brandon casey <drafnelyahoo.com&gt; wrote:


--- miao huijie yahoo.com> wrote:

&gt; Hi all,
>
> I am trying to understand how to use libpng to write
> out a png file. Here is my script:
&gt;
> int dm_write_pngfile(char *filename,
>
>
> unsigned char *byte_image,
>
>
> int nx, int ny, char
> *error_string)
>
> { int i;
>
>
> FILE *fp = fopen(filename,"wb");
>
>
> if (!fp) return(DM_FILEIO_FAILURE);
>
>
>
>
>
> png_structp png_ptr = png_create_write_struct
>
>
> (PNG_LIBPNG_VER_STRING,
> (png_voidp)error_string,NULL,NULL);
>
>
> if (!png_ptr) return(DM_FILEIO_FAILURE);
>
>
>
>
>
> png_infop info_ptr =
> png_create_info_struct(png_ptr);
>
>
> if (!info_ptr) {
>
>
>
> png_destroy_write_struct(&png_ptr,(png_infopp)NULL);
>
>
> return(DM_FILEIO_FAILURE);
>
>
> }
>
>
> if (setjmp(png_jmpbuf(png_ptr))) {
>
>
> png_destroy_write_struct(&png_ptr, &info_ptr);
>
>
> fclose(fp);
>
>
> return(DM_FILEIO_FAILURE);
>
>
> }
>
>
> png_init_io(png_ptr, fp);
>
>
> png_bytep *row_ptr;
>
>
> row_ptr =
> (png_bytep*)malloc(ny*sizeof(png_bytep));
>
>
> for (i=0; i
> row_ptr[i]=(png_byte*)malloc(nx*sizeof(png_byte));

This for loop should not be performed. Your row_ptr
array is going to point into the memory that you
passed in, so you don't have to allocate space for
each row.

Also (you may be simplifying your code for our
benefit), you should always check the return of memory
allocation calls.

&gt;
> for (i=0; i> byte_image+i*nx*sizeof(png_byte);
>
>
>
>
png_set_IHDR(png_ptr,info_ptr,nx,ny,1,PNG_COLOR_TYPE_GRAY,


That fifth argument to png_set_IHDR() is the bitdepth.
You set 1 bit, which means that the output image will
be able to represent two values 0 and 1. 1 is valid,
but you probably want to use something larger. Valid
values are: 1, 2, 4, 8, 16.

The short and correct answer is probably set this to 8
and that will solve your problem.

But, I'm not positive, but libpng may assume that your
source image is the same bit depth that you selected
for the output file which makes sense. So if you
source image is 8 bit grayscale and you select 4 bit
output depth, you may have to use png_set_shift().
I've only used libpng for 8 bit color images though.


>
>
>
> PNG_INTERLACE_NONE,PNG_COMPRESSION_TYPE_BASE,
>
>
> PNG_FILTER_TYPE_BASE);
>
>
>
>
> png_set_rows(png_ptr,info_ptr,row_ptr);
>
>
> png_write_png(png_ptr, info_ptr,
> PNG_TRANSFORM_IDENTITY, NULL);
>
>
> png_write_end(png_ptr, NULL);
>
>
> png_destroy_write_struct(&png_ptr, &info_ptr);
>
>
> return(DM_FILEIO_SUCCESS);
>
>
> }
>
> I created a simple 2D gaussian array, converted it
> into unsigned array. But this script didn't write it
> out correctly. It wrote out a black png image with
> two bright lines on the right. I checked the
> gaussian array and printed out the row_ptr after
> assign the addresses of each row, and they are all
> right. So the problem occurs when I using
> png_set_IHDR, png_set_rows or png_write_png. Does
> anyone have any suggestion on it? Thanks a lot!
>
> Huijie
>;
>
> ---------------------------------
> Be a chatter box. Enjoy free PC-to-PC calls with
> Yahoo! Messenger with Voice.


__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com


-------------------------------------------------------
All the advantages of Linux Managed Hosting--Without the Cost and Risk!
Fully trained technicians. The highest number of Red Hat certifications in
the hosting industry. Fanatical Support. Click to learn more
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=107521&bid=248729&amp;dat=121642
_______________________________________________
png-mng-implement mailing list
png-mng-implementlists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/png-mng-implement
yahoo.com>


Yahoo! Messenger with Voice. Make PC-to-Phone Calls to the US (and 30+ countries) for 2¢/min or less.
[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )