C Programming Questions - General Discussion and Assistance - CHDK Forum

C Programming Questions

  • 2 Replies
  • 2819 Views
*

Offline lapser

  • *****
  • 1093
C Programming Questions
« on: 26 / November / 2012, 21:36:20 »
Advertisements
I'm trying to figure out the most efficient way to use constants in C. Here are the 2 possibilities:

#define SHOT_HISTOGRAM_XWIDTH   (CAM_ACTIVE_AREA_X2-CAM_ACTIVE_AREA_X1-SHOT_HISTOGRAM_MARGIN*2)
. . .
  hx1=(SHOT_HISTOGRAM_XWIDTH*xp1)/100 + CAM_ACTIVE_AREA_X1 + SHOT_HISTOGRAM_XMARGIN;

OR

const int xwidth=CAM_ACTIVE_AREA_X2-CAM_ACTIVE_AREA_X1-SHOT_HISTOGRAM_MARGIN*2;
. . .
  hx1=(xwidth*xp1)/100 + CAM_ACTIVE_AREA_X1 + SHOT_HISTOGRAM_XMARGIN;


I think the "#define" way might be better, depending on how the compiler optimizes, but the "const int" method is more clear. Can any of the C experts shed some light on this?

When the compiler sees:   y=x+2*2  , does it compile to:  y=x+4  ?

Which "y=..." instruction is more efficient:

const int x=2;
...
y=y+x;

OR

y=y+2;


=====

And can someone explain what the "active area" of the sensor means? Since it's a little larger than the jpg size, my guess is that the resulting jpg picture only contains pixels from the active area, after lens distortion correction?

« Last Edit: 26 / November / 2012, 21:38:29 by lapser »
EOS-M3_120f / SX50_100b / SX260_101a / G1X_100g / D20_100b
https://www.youtube.com/user/DrLapser/videos

*

Offline reyalp

  • ******
  • 14118
Re: C Programming Questions
« Reply #1 on: 26 / November / 2012, 23:45:27 »
I think the "#define" way might be better, depending on how the compiler optimizes, but the "const int" method is more clear. Can any of the C experts shed some light on this?
Generally speaking, go for what is clearest and don't worry about how the compiler optimizers.
Quote
When the compiler sees:   y=x+2*2  , does it compile to:  y=x+4  ?
Yes.
Quote
Which "y=..." instruction is more efficient:
const int x=2;
...
y=y+x;
OR
y=y+2;

1) Don't worry about it unless it's actually in a performance critical loop that is executed thousands of times.
2) I doubt the immediate (y=y+2) case will ever be slower, but whether it's faster or not depends on the compiler and other details.
Quote
And can someone explain what the "active area" of the sensor means? Since it's a little larger than the jpg size, my guess is that the resulting jpg picture only contains pixels from the active area, after lens distortion correction?
The "active area" is a parameter required by the DNG spec. It should the area that contains valid image data in the raw buffer, although exactly where this should fall is sometimes ambiguous because pixels at the very edge can contain image data but not match the levels of the rest of the image.
Don't forget what the H stands for.

*

Offline lapser

  • *****
  • 1093
Re: C Programming Questions
« Reply #2 on: 27 / November / 2012, 21:20:14 »
Thanks for all that info. Here's what I came up with:

Code: (c) [Select]
const int xwidth=CAM_ACTIVE_AREA_X2 - CAM_ACTIVE_AREA_X1;
const int ywidth=CAM_ACTIVE_AREA_Y2 - CAM_ACTIVE_AREA_Y1;

int set_histogram_area(int xp1,int yp1,int xp2, int yp2)
{
  hx1=(xwidth*xp1)/1000 + CAM_ACTIVE_AREA_X1;
  hx2=(xwidth*xp2)/1000 + CAM_ACTIVE_AREA_X1;
  hy1=(ywidth*yp1)/1000 + CAM_ACTIVE_AREA_Y1;
  hy2=(ywidth*yp2)/1000 + CAM_ACTIVE_AREA_Y1;
I wrote a hack in get_raw_pixel(x,y), which is called to build the histogram, so that it stores 0xFF bytes in the image buffer after reading the value. The points that were sampled end up in the image and you can see where the active area is. On the sx260, it just fills up the screen when viewing the image in play mode.

EOS-M3_120f / SX50_100b / SX260_101a / G1X_100g / D20_100b
https://www.youtube.com/user/DrLapser/videos

 

Related Topics


SimplePortal 2.3.6 © 2008-2014, SimplePortal