Struct in gui.c for viewport objects but had side effects - General Discussion and Assistance - CHDK Forum

Struct in gui.c for viewport objects but had side effects

  • 0 Replies
  • 1011 Views
*

Offline Lebeau

  • ***
  • 187
Struct in gui.c for viewport objects but had side effects
« on: 28 / July / 2011, 23:13:45 »
Advertisements
I declare a structure and methods within gui.h like
Code: [Select]
struct
{
int aspect_ratio; // check change
// public
unsigned short cols; // nb of cols
unsigned short rows; // nb or rows
// private (??) used to compute addr (row,col)
unsigned char * start_ptr; // point to the ( row, col ) == ( 0, 0 )
unsigned short height; // in lines
unsigned short empty_rows; // in unused lines
unsigned short width; // in bytes
unsigned short empty_cols; // in unused bytes
}
viewport;

void viewport_init ( );
unsigned char * get_viewport_addr ( unsigned int col, unsigned int row );
unsigned short get_viewport_luma ( unsigned int col, unsigned int row );
unsigned short get_viewport_color ( unsigned int col, unsigned int row );


and define methods in gui.c like
Code: [Select]
void viewport_init ( ) {
int aspect_ratio;
if ( ( aspect_ratio = shooting_get_prop(PROPCASE_ASPECT_RATIO) ) != viewport.aspect_ratio ) {
viewport.aspect_ratio = aspect_ratio;
viewport.cols = vid_get_viewport_width ( ); // nb of columns, in triplets
viewport.rows = vid_get_viewport_height ( ); // nb of rows, in lines
viewport.start_ptr = ( ( mode_get () & MODE_MASK ) == MODE_PLAY )
? vid_get_viewport_fb_d ()
: ( ( kbd_is_key_pressed ( KEY_SHOOT_HALF ) )
? vid_get_viewport_fb ()
: vid_get_viewport_live_fb () );
if ( viewport.start_ptr == NULL )
viewport.start_ptr = vid_get_viewport_fb ();
viewport.empty_rows = vid_get_viewport_yoffset ( ); // upon camera aspect ratio, in lines
viewport.height = viewport.rows + 2 * viewport.empty_rows; // nb of rows, in lines
viewport.empty_cols = vid_get_viewport_xoffset ( ) * 3; // upon camera aspect ratio, in bytes
viewport.width = vid_get_viewport_buffer_width ( ) * 3; // upon camera viewport buffer, in bytes
viewport.start_ptr += ( viewport.empty_rows * viewport.width ) + viewport.empty_cols; // point to (row,col) == (0,0)
}
    return;
}

unsigned char * get_viewport_addr ( unsigned int col, unsigned int row ) {
row = ( row % viewport.rows );
col = ( col % viewport.cols );
    return ( viewport.start_ptr + ( row * viewport.width ) + ( ( col >> 1 ) * 6 ) ); // point to (row, col >> 1) sixplet
}

unsigned short get_viewport_luma ( unsigned int col, unsigned int row ) {
static const unsigned char col2pos [4] = { 1, 3, 5, 4 }; // convert Y position into byte position (Cb, Y1, Cr, Y2, Y4, Y3)

unsigned char * addr = get_viewport_addr ( col, row );
unsigned int even_col = ( col % 2 ) << 1;
    return ( addr [ col2pos [even_col] ] << 8 ) | addr [ col2pos [even_col+1] ]; // ( Y1 << 8 | Y2 ) or ( Y3 << 8 | Y4 );
}

unsigned short get_viewport_color ( unsigned int col, unsigned int row ) {
unsigned char * addr = get_viewport_addr ( col, row );
    return ( addr [0] << 8 ) | addr [2]; // Cb << 8 | Cr;
}


Calling viewport_init each time caller need to use viewport's methods, I had some problems writing dng files. After about 5 or 6 photos, it seams that thumbnail size become zero. The thumbnail width and height are expressed in term of viewport.cols and viewport.rows.

Well, may be I have another bug but is it possible that something related with compilation options (since viewport attributes are far from the caller) could cause bizarreous behavior?

I roll back to direct call ( like vid_get_viewport_width () ) instead of sub struct use and integrate viewport_init content into get_viewport_addr() like
Code: [Select]
unsigned char * get_viewport_addr ( unsigned int col, unsigned int row ) {
static int previous_aspect_ratio = -1;
static unsigned char * start_ptr = NULL;
static unsigned int buffer_width;
int aspect_ratio;

col = col % vid_get_viewport_width ( );
row = row % vid_get_viewport_height ( );

if ( ( aspect_ratio = shooting_get_prop ( PROPCASE_ASPECT_RATIO ) ) != previous_aspect_ratio ) {
start_ptr = ( ( mode_get () & MODE_MASK ) == MODE_PLAY )
? vid_get_viewport_fb_d ()
: ( ( kbd_is_key_pressed ( KEY_SHOOT_HALF ) )
? vid_get_viewport_fb ()
: vid_get_viewport_live_fb () );
if ( start_ptr == NULL )
start_ptr = vid_get_viewport_fb ();
buffer_width = ( vid_get_viewport_buffer_width ( ) * 3 );
previous_aspect_ratio = aspect_ratio;
};
    return ( start_ptr + ( row * buffer_width ) + ( ( col >> 1 ) * 6 ) ); // point to (row, col >> 1) sixplet
}

And every thing work well now.

What did I missed ?!?!?!?!?

 

Related Topics