Thread: read part of png file

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    6

    read part of png file

    Good mornning everyone

    I'm trying to read a part of a PNG image file using Libpng and VC++ 2008 but unfortunately while executing the program an error " access violation memory" occured
    Code:
     // Initialisation
    png_bytep row_pointers;
    png_structp png_ptr ;
    ..................
    .....................
    BYTE * buffer = (BYTE *)malloc((size*sizeof(BYTE)); //size : is less than image lenght 
    row_pointers = (png_bytep)(buffer +size);
     png_read_image (png_ptr, row_pointers); //the problem occured here
    ...............
    ..............
    Please help me and thanks in advance

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Did you read the manual?
    http://www.libpng.org/pub/png/libpng-manual.txt

    Here's what they have as an example.
    Code:
       if (height > PNG_UINT_32_MAX/png_sizeof(png_byte))
          png_error (png_ptr,
              "Image is too tall to process in memory");
    
       if (width > PNG_UINT_32_MAX/pixel_size)
          png_error (png_ptr,
              "Image is too wide to process in memory");
    
       row_pointers = png_malloc(png_ptr,
           height*png_sizeof(png_bytep));
    
       for (int i=0; i<height, i++)
          row_pointers[i]=NULL;  /* security precaution */
    
       for (int i=0; i<height, i++)
          row_pointers[i]=png_malloc(png_ptr,
              width*pixel_size);
    
       png_set_rows(png_ptr, info_ptr, &row_pointers);
    Your row_pointers are basically garbage.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    6
    Thank you Salem for your reply , but when I put
    Code:
     row_pointers = png_malloc(png_ptr,       height*png_sizeof(png_bytep));
    an error produce "Impossible to convert 'png_voidp' to 'png_byte **' "
    and for
    Code:
    row_pointers[i]=png_malloc(png_ptr,          width*pixel_size);
    "Impossible to convert 'png_voidp' to 'png_byte *'

    I declare row_pointers as : png_byte ** row_pointers = NULL;

    Could anyone help me
    thanks in advance

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well that sounds like it's because you're using C++ to compile C code.

    Code:
    # compile as C
    $ gcc bar.c -lpng
    
    # compile as C++
    $ gcc -x c++ bar.c -lpng
    bar.c: In function ‘int main()’:
    bar.c:8: error: invalid conversion from ‘void*’ to ‘png_byte*’
    You could force the conversion with a cast, or perhaps just name your source files as say prog.c, if in fact you're intending to write C code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    6
    Thank you so much Salem it works now

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    6
    I think that this problem will drive me crazy ...... when I try to read the data from row_pointers, it seem to be completely different from the image data
    png_set_rows (png_ptr, info_ptr, row_pointers);
    for(int p = 0; p < row_Lenght; p++)
    cout << row_pointers[0][p] << endl;
    Where is the problem ???? Thanks again

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Have you created (using your favourite image editing software) a small set of test PNG files which are say 10x10 all red pixels?

    Saying "my data is junk" whilst neither posting your image, or your "garbage" results doesn't really help anyone.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Oct 2011
    Posts
    6
    First of all I'm sorry for taking this long time to put my comment .... Your are right Salem, after checking the first 10 byte of my whole image (To do the check I red all the image using png_read_image " (png_ptr, row_pointers) " the result was "13, 13, 13, 13, 13, 13, 13, 13, 13, 14 " but when I used "png_set_rows ()" to read only part of the image, the first 10 byte was "205, 205, 205, 205, 205, 205, 205, 205, 205, 205" and actually all the byte have this same value ... I think that when I used " png_set_rows () " it don't read the value of the image. So maybe I need to add a function to do it ???
    I really appreciate your help

    PS : I'm not using png_read_image() because an access violation error appear
    Last edited by fleurdelys; 10-23-2011 at 03:27 AM.

  9. #9
    Registered User
    Join Date
    Oct 2011
    Posts
    6
    I've just used the function " png_read_rows(png_ptr, row_pointers, NULL, 1) ; " but the result for the first 10 byte was " 24, 25, 25, 25, 25, 25, 26, 26, 26, 26 " ......

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading a part of a file
    By Xleniz in forum C++ Programming
    Replies: 3
    Last Post: 11-18-2010, 03:21 PM
  2. Read part of file
    By Martin Kovac in forum C Programming
    Replies: 1
    Last Post: 04-13-2009, 01:51 PM
  3. File Server Help - Part 2
    By lautarox in forum C Programming
    Replies: 13
    Last Post: 11-07-2008, 11:40 PM
  4. Reading from file.But only part of it.
    By baniakjr in forum C++ Programming
    Replies: 6
    Last Post: 11-09-2006, 06:23 AM
  5. Deleting part of a file
    By Invincible in forum C++ Programming
    Replies: 5
    Last Post: 05-23-2002, 02:08 AM