Thread: How to fix pointer?

  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    235

    Question How to fix pointer?

    This is my function:

    Code:
    int JPEG_read_file_rgb2hsv( unsigned char ** image_buffer, struct jpeg_decompress_struct * cDecompresInfo)
    {
      JSAMPARRAY rows_buffer;  // Output row rows_buffer
      int row_stride_len;
    
      // @TODO: row_stride_len
      row_stride_len = 512;//*cDecompresInfo.output_width * cDecompresInfo.output_components;
      rows_buffer = (*(*cDecompresInfo).mem->alloc_sarray)
                    ((j_common_ptr) &*cDecompresInfo, JPOOL_IMAGE, row_stride_len, 1);
    
      // 6) Initiate image_buffer
      size_t counter=0;
      size_t raw_size = row_stride_len;
      printf("raw size:%d\n",row_stride_len*
                ((*cDecompresInfo).output_height));
      *image_buffer = (unsigned char*) malloc( row_stride_len*
                                                ((*cDecompresInfo).output_height) );
    
      /******************************
                RGB 2 HSV
      ******************************/
    
      float R, G, B, H, S, V,min, max, delta;
      H=S=V=0;
      int t;
    
      while ((*cDecompresInfo).output_scanline < (*cDecompresInfo).output_height) {
        (void) jpeg_read_scanlines(&*cDecompresInfo, rows_buffer, 1);
        for( t=0; t<row_stride_len ; t=t+3)
            {
               R=(float) rows_buffer[0][t];
               G=(float) rows_buffer[0][t+1];
               B=(float) rows_buffer[0][t+2];
               min = (B < G) ? ((R < B) ? R : B) : (R < G ? R : G);
               max = (B > G) ? ((R > B) ? R : B) : (R > G ? R : G);
               V = max;
               delta = max - min;
               if( max != 0 )
                    S = delta / max;
               else {
                    S = 0;
                    H = -1;
                    continue;
                }
    
                if( R == max )
                    H = ( G - B ) / delta;        // between yellow & magenta
                else if( G == max )
                    H = 2 + ( B - R ) / delta;    // between cyan & yellow
                else
                    H = 4 + ( R - G ) / delta;    // between magenta & cyan
    
                H *= 60;                // degrees
                if( H < 0 )
                    H += 360;
    // Must perform the conversion to 3 bytes data
    // Is it exact?
                (*image_buffer)[counter+t]=(unsigned char)((H*100)/255);
                (*image_buffer)[counter+t+1]=(unsigned char)(S*100);
                (*image_buffer)[counter+t+2]=(unsigned char)V;
                }
        // memcpy(image_buffer+counter, rows_buffer[0], raw_size);
        counter += row_stride_len;
        // copyScanlineToBuffer(image_buffer, rows_buffer[0], row_stride_len); // args: & pointer & sample count
      }
      image_buffer -=row_stride_len;
    
      // Save global values for later use
      image_width=(*cDecompresInfo).image_width;
      image_height=(*cDecompresInfo).image_height;
    
      (void) jpeg_finish_decompress(&cDecompresInfo); // Finish decompression, no errors possible
      jpeg_destroy_decompress(&cDecompresInfo); // 8) Release JPEG decompression object
      // @TODO: infile
      // fclose(infile);
      return 1;
    }
    I am completing this function. Originally it used global variables, but I remade it to use a pointer to struct: struct jpeg_decompress_struct * cDecompresInfo;

    Code:
    (void) jpeg_finish_decompress(&cDecompresInfo);
    now it crashes on the line, where the pointer is not fixed yet:
    Code:
    (void) jpeg_finish_decompress(&cDecompresInfo);
    It's not clear how to correct. I tried &*cDecompresInfo or &(*cDecompresInfo) none of them works.
    jpeg_finish_decompress want the struct to be dereferenced, but I have it via pointer. How to correct?
    Last edited by barracuda; 01-31-2015 at 05:54 AM.

  2. #2
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Quote Originally Posted by barracuda View Post
    It's not clear how to correct. I tried &*cDecompresInfo or &(*cDecompresInfo) none of them works.
    jpeg_finish_decompress want the struct to be dereferenced, but I have it via pointer. How to correct?
    jpeg_destroy_decompress takes a j_decompress_ptr. So I'm going to assume you should use
    Code:
    jpeg_finish_decompress(cDecompresInfo);
    Also, you have a lot of
    Code:
    (*cDecompresInfo).
    which is messy to read. You should replace all of those with
    Code:
    cDecompresInfo->

  3. #3
    Registered User
    Join Date
    Sep 2014
    Posts
    235
    I also tried jpeg_finish_decompress(cDecompresInfo); but this crashes permanently.


    Edit:
    I added
    Code:
    fclose(infile);
    But the problem with crashing
    Code:
    (void) jpeg_finish_decompress(cDecompresInfo);
    stays

    Note:
    Original working code had:
    Code:
     jpeg_finish_decompress(&cInfo)
    Last edited by barracuda; 01-31-2015 at 11:24 AM.

  4. #4
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Quote Originally Posted by barracuda View Post
    I also tried jpeg_finish_decompress(cDecompresInfo); but this crashes permanently.
    I can't tell you why, without seeing where cDecompresInfo comes from. Your function performs cleanup but not the initialization. This is a very bad design, you need to open & close on the same abstraction level.

  5. #5
    Registered User
    Join Date
    Sep 2014
    Posts
    235
    OMG I got luck! I have found this:

    https://github.com/freebasic/fbc/blo...inc/jpeglib.bi
    See it for definitions of the types.
    Code:
    type j_common_ptr as jpeg_common_struct ptr
    type j_compress_ptr as jpeg_compress_struct ptr
    type j_decompress_ptr as jpeg_decompress_struct ptr
    Now I changed the function headers like that:
    Code:
    int JPEG_read_file_rgb2hsv( FILE ** infile, unsigned char ** image_buffer, j_decompress_ptr cDecompresInfo)
    I don't know why, now it crashes also here:
    Code:
    (void) jpeg_read_header(&*cDecompresInfo, TRUE);
    in
    Code:
    bool JPEG_prepareInfo(char * filename, FILE ** infile, j_decompress_ptr cDecompresInfo)

  6. #6
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Fix your code, barracuda. Take jpeg_destroy_decompress out of that function, or put the preperation code in it.

  7. #7
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Code:
      JSAMPARRAY rows_buffer;  // Please dont hide arrays behind a type, or encapsulate it within a structure then
    ...
      rows_buffer = (*(*cDecompresInfo).mem->alloc_sarray)  ((j_common_ptr) &*cDecompresInfo, JPOOL_IMAGE, row_stride_len, 1); /* Is this supposed to be a pointer function call?? */

  8. #8
    Registered User
    Join Date
    Sep 2014
    Posts
    235
    I don't know how to fix it. The declaration with j_decompress_ptr did not work at all. I am returning back to

    Code:
    int  JPEG_read_file_rgb2hsv( FILE ** infile, unsigned char ** image_buffer,  struct jpeg_decompress_struct * cDecompresInfo)
    Yarin,
    why it does not work with pointer?
    (void) jpeg_start_decompress(&*cDecompresInfo);
    When I would remove it, so the object would stay in memory.

    I call the function like so:
    Code:
    JPEG_prepareInfo(filename1, &fsource, &cinfo);
    JPEG_read_file_rgb2hsv(&image_buffer, &fsource, &cinfo);
    JPEG_write_file(filename2, 95, &image_buffer, &cinfo);
    Main thing is to read data from file and then write data to file. In read the cinfo is filled with header data. I need the cinfo data in write function. That's why I have the cinfo in scope of main(). If I would move the code from JPEG_prepareInfo to JPEG_read_file_rgb2hsv so I would need to use global cinfo. That looks odd. I wanted to write code without global.

    nonpuz:
    this is alternative to the original code
    Code:
    struct jpeg_decompress_struct cinfo;
    ...
    buffer = (*cinfo.mem->alloc_sarray)
    But I changed
    struct jpeg_decompress_struct

    for
    struct jpeg_decompress_struct *

    Note:
    *image_buffer contains something what I did not see there before:
    (unsigned char *) 0x7c949564 <ntdll!LdrFindEntryForAddress+11013> "\201\377d\241\030"
    it looks like bad address? This is just three lines before I try to destroy cDecompresInfo
    Last edited by barracuda; 01-31-2015 at 01:52 PM.

  9. #9
    Registered User
    Join Date
    Sep 2014
    Posts
    235
    I have fixed some mistakes
    Paste ofCode
    but the problem with
    Code:
    jpeg_destroy_decompress(cDecompresInfo);
    remains unsolved

  10. #10
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Quote Originally Posted by barracuda View Post
    I have fixed some mistakes
    Yet you still haven't put the cleanup where is belongs.

    BTW, what do you think &* does?

  11. #11
    Registered User
    Join Date
    Sep 2014
    Posts
    235
    So verdict is that it is not possible to do that for any reason. I will do as you said. Wait.. Now I realized that I could destroy the object out of the scope of reading function. I can destroy it in main!
    Last edited by barracuda; 02-01-2015 at 03:58 AM.

  12. #12
    Registered User
    Join Date
    Sep 2014
    Posts
    235
    So I fixed some errors, move some function calls to main:
    Paste ofCode
    but it crashes again on the
    jpeg_finish_decompress()
    or
    jpeg_finish_compress()

    This is current version:
    Paste ofCode

    I really don't understand why it crashes when it is in the same scope. Do you have some final solution?

  13. #13
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Quote Originally Posted by barracuda View Post
    So I fixed some errors, move some function calls to main:
    Paste ofCode
    but it crashes again on the
    jpeg_finish_decompress()
    or
    jpeg_finish_compress()

    This is current version:
    Paste ofCode

    I really don't understand why it crashes when it is in the same scope. Do you have some final solution?
    In main, you clean up cinfo after you're done reading with it. But then you proceed to write with it. Cleanup should only happen once, and only after you're done with it.

  14. #14
    Registered User
    Join Date
    Sep 2014
    Posts
    235
    Sharp eye. Sp I move it after JPEG_write_file(). But it still crashes here. I'm clueless.

    Code:
    if (isOpen){
            JPEG_write_file(filename2, 95, &image_buffer, &cinfo);
            jpeg_finish_compress(&cinfo);
            jpeg_destroy_compress(&cinfo);
    }
    I clear cinfo (finish compress) in main (failed) and local cinfo in JPEG_write_file() (success).

  15. #15
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Quote Originally Posted by barracuda View Post
    Sharp eye. Sp I move it after JPEG_write_file(). But it still crashes here. I'm clueless.

    Code:
    if (isOpen){
            JPEG_write_file(filename2, 95, &image_buffer, &cinfo);
            jpeg_finish_compress(&cinfo);
            jpeg_destroy_compress(&cinfo);
    }
    I clear cinfo (finish compress) in main (failed) and local cinfo in JPEG_write_file() (success).
    Post your new main

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-27-2014, 12:10 AM
  2. Replies: 3
    Last Post: 10-30-2009, 04:41 PM
  3. Replies: 9
    Last Post: 06-13-2009, 02:31 AM
  4. Replies: 1
    Last Post: 03-24-2008, 10:16 AM
  5. Replies: 4
    Last Post: 08-27-2007, 11:51 PM