CHDK_DE Vorschauversion  Trunk Rev. 5163
 Alle Datenstrukturen Dateien Funktionen Variablen Typdefinitionen Aufzählungen Aufzählungswerte Makrodefinitionen
rawhookops.c-Dateireferenz
#include "camera_info.h"
#include "stdlib.h"
#include "conf.h"
#include "raw.h"
#include "math.h"
#include "../lib/lua/lualib.h"
#include "../lib/lua/lauxlib.h"
+ Include-Abhängigkeitsdiagramm für rawhookops.c:

gehe zum Quellcode dieser Datei

Datenstrukturen

struct  rawop_histo_t
 

Makrodefinitionen

#define CFA_R   0
 
#define CFA_G1   1
 
#define CFA_G2   2
 
#define CFA_B   3
 
#define RAWOP_HISTO_META   "rawop.histo_meta"
 

Funktionen

void set_number_field (lua_State *L, const char *name, int value)
 
static int round_d2i (double v)
 
static int rawop_get_cfa (lua_State *L)
 
static int rawop_get_cfa_offsets (lua_State *L)
 
static int rawop_get_bits_per_pixel (lua_State *L)
 
static int rawop_get_raw_neutral (lua_State *L)
 
static int rawop_get_black_level (lua_State *L)
 
static int rawop_get_white_level (lua_State *L)
 
static int rawop_get_raw_width (lua_State *L)
 
static int rawop_get_raw_height (lua_State *L)
 
static int rawop_get_active_left (lua_State *L)
 
static int rawop_get_active_top (lua_State *L)
 
static int rawop_get_active_width (lua_State *L)
 
static int rawop_get_active_height (lua_State *L)
 
static int rawop_get_jpeg_left (lua_State *L)
 
static int rawop_get_jpeg_top (lua_State *L)
 
static int rawop_get_jpeg_width (lua_State *L)
 
static int rawop_get_jpeg_height (lua_State *L)
 
static int rawop_get_pixel (lua_State *L)
 
static int rawop_set_pixel (lua_State *L)
 
static int rawop_get_pixels_rgbg (lua_State *L)
 
static int rawop_set_pixels_rgbg (lua_State *L)
 
static int rawop_fill_rect (lua_State *L)
 
static int rawop_meter (lua_State *L)
 
static int rawop_raw_to_ev (lua_State *L)
 
static int rawop_ev_to_raw (lua_State *L)
 
static int rawop_create_histogram (lua_State *L)
 
static int rawop_histo_update (lua_State *L)
 
static int rawop_histo_range (lua_State *L)
 
static int rawop_histo_total_pixels (lua_State *L)
 
static int rawop_histo_bits (lua_State *L)
 
static int rawop_histo_free (lua_State *L)
 
static int rawop_histo_gc (lua_State *L)
 
static void init_raw_params (void)
 
void rawop_update_hook_status (int active)
 
int luaopen_rawop (lua_State *L)
 

Variablen

static int raw_buffer_valid
 
static unsigned raw_neutral
 
static double log2_raw_neutral_count
 
static unsigned cfa_offsets [4][2]
 
static const char * cfa_names [] ={"r","g1","g2","b"}
 
static const luaL_Reg rawop_histo_meta_methods []
 
static const luaL_Reg rawop_histo_methods []
 
static const luaL_Reg rawop_funcs []
 

Makro-Dokumentation

#define CFA_B   3

Definiert in Zeile 31 der Datei rawhookops.c.

#define CFA_G1   1

Definiert in Zeile 29 der Datei rawhookops.c.

#define CFA_G2   2

Definiert in Zeile 30 der Datei rawhookops.c.

#define CFA_R   0

Definiert in Zeile 28 der Datei rawhookops.c.

#define RAWOP_HISTO_META   "rawop.histo_meta"

Definiert in Zeile 32 der Datei rawhookops.c.

Dokumentation der Funktionen

static void init_raw_params ( void  )
static

Definiert in Zeile 742 der Datei rawhookops.c.

742  {
743  // empirical guestimate
744  // average pixel value of a neutral subject shot with canon AE, as a fraction of usable dynamic range
745  // found to be reasonably close on d10, elph130, a540, g1x and more.
746  double raw_neutral_count = (double)(camera_sensor.white_level - camera_sensor.black_level)/(6.669);
747  log2_raw_neutral_count = log2(raw_neutral_count);
748  raw_neutral = round_d2i(raw_neutral_count) + camera_sensor.black_level;
749 }
int luaopen_rawop ( lua_State L)

Definiert in Zeile 761 der Datei rawhookops.c.

761  {
762  // initialize globals
763  raw_buffer_valid = 0;
764 
765  int i;
766  int g1=1;
767  for(i=0; i<4; i++) {
768  int c = (camera_sensor.cfa_pattern >> 8*i) & 0xFF;
769  int ci=0;
770  switch(c) {
771  case 0:
772  ci=0;
773  break;
774  case 1:
775  if(g1) {
776  ci=1;
777  g1=0;
778  } else {
779  ci=2;
780  }
781  break;
782  case 2:
783  ci=3;
784  break;
785  }
786  cfa_offsets[ci][0] = i&1;
787  cfa_offsets[ci][1] = (i&2)>>1;
788  }
789 
790  // TODO - maybe this should only be done in update_hook_status, since any changeable values
791  // will only be known at that point.
792  init_raw_params();
793 
796  /* use a table of methods for the __index method */
797  lua_newtable(L);
799  lua_setfield(L,-2,"__index");
800  lua_pop(L,1);
801 
802  /* global lib*/
803  lua_newtable(L);
804  luaL_register(L, "rawop", rawop_funcs);
805  return 1;
806 }
static int rawop_create_histogram ( lua_State L)
static

Definiert in Zeile 507 der Datei rawhookops.c.

507  {
509  if(!h) {
510  return luaL_error(L,"failed to create userdata");
511  }
512  h->total_pixels = 0;
513  h->data = NULL;
515  lua_setmetatable(L, -2);
516  return 1;
517 }
static int rawop_ev_to_raw ( lua_State L)
static

Definiert in Zeile 485 der Datei rawhookops.c.

485  {
486  int v=luaL_checknumber(L,1);
487  int scale=luaL_optnumber(L,2,96);
488  // TODO not clear if this should be clamped to valid raw ranges?
490  return 1;
491 }
static int rawop_fill_rect ( lua_State L)
static

Definiert in Zeile 356 der Datei rawhookops.c.

356  {
357  if(!raw_buffer_valid) {
358  return luaL_error(L,"raw data not available");
359  }
360  unsigned int xstart=luaL_checknumber(L,1);
361  unsigned int ystart=luaL_checknumber(L,2);
362  unsigned int width=luaL_checknumber(L,3);
363  unsigned int height=luaL_checknumber(L,4);
364  unsigned short val=luaL_checknumber(L,5);
365  unsigned int xstep=luaL_optnumber(L,6,1);
366  unsigned int ystep=luaL_optnumber(L,7,xstep);
367  unsigned int xmax = xstart + width;
368  unsigned int ymax = ystart + height;
369  if(xstart >= (unsigned)camera_sensor.raw_rowpix || ystart >= (unsigned)camera_sensor.raw_rows) {
370  return 0;
371  }
372  if(xmax > (unsigned)camera_sensor.raw_rowpix) {
373  xmax = (unsigned)camera_sensor.raw_rowpix;
374  }
375  if(ymax > (unsigned)camera_sensor.raw_rows) {
376  ymax = (unsigned)camera_sensor.raw_rows;
377  }
378  int x,y;
379  for(y=ystart; y<ymax; y+=ystep) {
380  for(x=xstart; x<xmax; x+=xstep) {
381  set_raw_pixel(x,y,val);
382  }
383  }
384  return 0;
385 }
static int rawop_get_active_height ( lua_State L)
static

Definiert in Zeile 178 der Datei rawhookops.c.

178  {
180  return 1;
181 }
static int rawop_get_active_left ( lua_State L)
static

Definiert in Zeile 151 der Datei rawhookops.c.

151  {
153  return 1;
154 }
static int rawop_get_active_top ( lua_State L)
static

Definiert in Zeile 160 der Datei rawhookops.c.

160  {
162  return 1;
163 }
static int rawop_get_active_width ( lua_State L)
static

Definiert in Zeile 169 der Datei rawhookops.c.

169  {
171  return 1;
172 }
static int rawop_get_bits_per_pixel ( lua_State L)
static

Definiert in Zeile 76 der Datei rawhookops.c.

76  {
78  return 1;
79 }
static int rawop_get_black_level ( lua_State L)
static

Definiert in Zeile 104 der Datei rawhookops.c.

104  {
106  return 1;
107 }
static int rawop_get_cfa ( lua_State L)
static

Definiert in Zeile 45 der Datei rawhookops.c.

45  {
47  return 1;
48 }
static int rawop_get_cfa_offsets ( lua_State L)
static

Definiert in Zeile 60 der Datei rawhookops.c.

60  {
61  lua_createtable(L, 0, 4);
62  int i;
63  for(i=0;i<4;i++) {
64  lua_createtable(L, 0, 2);
65  set_number_field(L,"x",cfa_offsets[i][0]);
66  set_number_field(L,"y",cfa_offsets[i][1]);
67  lua_setfield(L, -2, cfa_names[i]);
68  }
69  return 1;
70 }
static int rawop_get_jpeg_height ( lua_State L)
static

Definiert in Zeile 230 der Datei rawhookops.c.

230  {
232  return 1;
233 }
static int rawop_get_jpeg_left ( lua_State L)
static

Definiert in Zeile 203 der Datei rawhookops.c.

203  {
205  return 1;
206 }
static int rawop_get_jpeg_top ( lua_State L)
static

Definiert in Zeile 212 der Datei rawhookops.c.

212  {
214  return 1;
215 }
static int rawop_get_jpeg_width ( lua_State L)
static

Definiert in Zeile 221 der Datei rawhookops.c.

221  {
223  return 1;
224 }
static int rawop_get_pixel ( lua_State L)
static

Definiert in Zeile 246 der Datei rawhookops.c.

246  {
247  if(!raw_buffer_valid) {
248  return luaL_error(L,"raw data not available");
249  }
250  unsigned x=luaL_checknumber(L,1);
251  unsigned y=luaL_checknumber(L,2);
252  // TODO return nil for out of bounds?
253  // might not want to check, or return 0, or error()?
254  if(x >= (unsigned)camera_sensor.raw_rowpix || y >= (unsigned)camera_sensor.raw_rows) {
255  return 0;
256  }
258  return 1;
259 }
static int rawop_get_pixels_rgbg ( lua_State L)
static

Definiert in Zeile 293 der Datei rawhookops.c.

293  {
294  if(!raw_buffer_valid) {
295  return luaL_error(L,"raw data not available");
296  }
297  unsigned int x=luaL_checknumber(L,1);
298  unsigned int y=luaL_checknumber(L,2);
299 
300  x &= 0xFFFFFFFE;
301  y &= 0xFFFFFFFE;
302 
303  if(x >= (unsigned)camera_sensor.raw_rowpix || y >= (unsigned)camera_sensor.raw_rows) {
304  return 0;
305  }
310  return 4;
311 }
static int rawop_get_raw_height ( lua_State L)
static

Definiert in Zeile 131 der Datei rawhookops.c.

131  {
133  return 1;
134 }
static int rawop_get_raw_neutral ( lua_State L)
static

Definiert in Zeile 91 der Datei rawhookops.c.

91  {
93  return 1;
94 }
static int rawop_get_raw_width ( lua_State L)
static

Definiert in Zeile 122 der Datei rawhookops.c.

122  {
124  return 1;
125 }
static int rawop_get_white_level ( lua_State L)
static

Definiert in Zeile 113 der Datei rawhookops.c.

113  {
115  return 1;
116 }
static int rawop_histo_bits ( lua_State L)
static

Definiert in Zeile 663 der Datei rawhookops.c.

663  {
665  if(!h->data) {
666  return luaL_error(L,"no data");
667  }
668  lua_pushnumber(L,h->bits);
669  return 1;
670 }
static int rawop_histo_free ( lua_State L)
static

Definiert in Zeile 676 der Datei rawhookops.c.

676  {
678  free(h->data);
679  h->data=NULL;
680  return 0;
681 }
static int rawop_histo_gc ( lua_State L)
static

Definiert in Zeile 683 der Datei rawhookops.c.

683  {
684  rawop_histo_free(L);
685  return 0;
686 }
static int rawop_histo_range ( lua_State L)
static

Definiert in Zeile 601 der Datei rawhookops.c.

601  {
603  if(!h->data) {
604  return luaL_error(L,"no data");
605  }
606  unsigned minval=luaL_checknumber(L,2);
607  unsigned maxval=luaL_checknumber(L,3);
608  int scale=1000;
609  if(lua_gettop(L) >= 4 && !lua_isnil(L,4)) {
610  const char *s=lua_tostring(L,4);
611  if(!s || strcmp(s,"count") != 0) {
612  scale=lua_tonumber(L,4);
613  // scale could be 0 from error passing 0, but neither is valid
614  if(!scale) {
615  return luaL_error(L,"invalid format");
616  }
617  } else {
618  scale=0;
619  }
620  }
621  if(maxval >= h->entries || minval > maxval) {
622  return luaL_error(L,"invalid range");
623  }
624  // TODO error?
625  if(!h->total_pixels) {
626  return luaL_error(L,"no pixels");
627 // lua_pushnumber(L,0);
628 // return 1;
629  }
630 
631  unsigned count=0;
632  int i;
633  for(i=minval;i<=maxval;i++) {
634  count+=h->data[i];
635  }
636  // TODO full raw buffer count*1000 could overflow 32 bit int
637  // could check / work around using ints but probably not worth it
638  if(scale) {
639  lua_pushnumber(L,round_d2i((scale*(double)count)/(double)h->total_pixels));
640  } else {
641  lua_pushnumber(L,count);
642  }
643  return 1;
644 }
static int rawop_histo_total_pixels ( lua_State L)
static

Definiert in Zeile 650 der Datei rawhookops.c.

650  {
652  if(!h->data) {
653  return luaL_error(L,"no data");
654  }
656  return 1;
657 }
static int rawop_histo_update ( lua_State L)
static

Definiert in Zeile 529 der Datei rawhookops.c.

529  {
530  if(!raw_buffer_valid) {
531  return luaL_error(L,"raw data not available");
532  }
534 
535  unsigned xstart=luaL_checknumber(L,2);
536  unsigned ystart=luaL_checknumber(L,3);
537  unsigned width=luaL_checknumber(L,4);
538  unsigned height=luaL_checknumber(L,5);
539  unsigned xstep=luaL_checknumber(L,6);
540  unsigned ystep=luaL_checknumber(L,7);
542  if(bits > camera_sensor.bits_per_pixel || bits < 1) {
543  return luaL_error(L,"invalid bit depth");
544  }
545  unsigned shift = camera_sensor.bits_per_pixel - bits;
546  unsigned entries = 1 << bits;
547 
548  h->total_pixels=0;
549  if(xstart >= (unsigned)camera_sensor.raw_rowpix
550  || ystart >= (unsigned)camera_sensor.raw_rows
551  || xstep == 0 || ystep == 0
552  || width == 0 || height == 0) {
553  luaL_error(L,"invalid update area");
554  return 0;
555  }
556  unsigned xmax=xstart+width;
557  unsigned ymax=ystart+height;
558  if(xmax > (unsigned)camera_sensor.raw_rowpix) {
559  xmax = (unsigned)camera_sensor.raw_rowpix;
560  }
561  if(ymax > (unsigned)camera_sensor.raw_rows) {
562  ymax = (unsigned)camera_sensor.raw_rows;
563  }
564  // total with clipping and rounding accounted for
565  h->total_pixels=((1+(xmax - xstart - 1)/xstep))*((1+(ymax - ystart - 1)/ystep));
566 
567  // TODO shorts or ints based on total pixels
568  if(h->entries != entries || !h->data) {
569  free(h->data);
570  h->data = malloc(entries*sizeof(unsigned));
571  if(!h->data) {
572  h->total_pixels=0;
573  return luaL_error(L,"insufficient memory");
574  }
575  }
576  h->entries = entries;
577  h->bits = bits;
578  memset(h->data,0,h->entries*sizeof(unsigned));
579 
580  unsigned x,y;
581  if(shift) {
582  for(y=ystart;y<ymax;y+=ystep) {
583  for(x=xstart;x<xmax;x+=xstep) {
584  h->data[get_raw_pixel(x,y)>>shift]++;
585  }
586  }
587  } else {
588  for(y=ystart;y<ymax;y+=ystep) {
589  for(x=xstart;x<xmax;x+=xstep) {
590  h->data[get_raw_pixel(x,y)]++;
591  }
592  }
593  }
594  return 0;
595 }
static int rawop_meter ( lua_State L)
static

Definiert in Zeile 405 der Datei rawhookops.c.

405  {
406  if(!raw_buffer_valid) {
407  return luaL_error(L,"raw data not available");
408  }
409  unsigned x1=luaL_checknumber(L,1);
410  unsigned y1=luaL_checknumber(L,2);
411  unsigned x_count=luaL_checknumber(L,3);
412  unsigned y_count=luaL_checknumber(L,4);
413  unsigned x_step=luaL_checknumber(L,5);
414  unsigned y_step=luaL_checknumber(L,6);
415 
416  // no pixels
417  if(!x_count || !y_count) {
418  return 0;
419  }
420 
421  unsigned x_max = x1 + x_step * x_count;
422  unsigned y_max = y1 + y_step * y_count;
423  // x out of range
424  if(x_max > (unsigned)camera_sensor.raw_rowpix) {
425  return 0;
426  }
427  // y out of range
428  if(y_max > (unsigned)camera_sensor.raw_rows) {
429  return 0;
430  }
431  // overflow possible (int max)/total < max value
432  if(x_count*y_count > (unsigned)0xFFFFFFFF >> camera_sensor.bits_per_pixel) {
433  return 0;
434  }
435  unsigned t=0;
436  unsigned x,y;
437  for(y = y1; y < y_max; y += y_step) {
438  for(x = x1; x < x_max; x += x_step) {
439  t+=get_raw_pixel(x,y);
440  }
441  }
442  lua_pushnumber(L,t/(x_count*y_count));
443  return 1;
444 }
static int rawop_raw_to_ev ( lua_State L)
static

Definiert in Zeile 462 der Datei rawhookops.c.

462  {
463  int v=luaL_checknumber(L,1);
464  int scale=luaL_optnumber(L,2,96);
465  // TODO not clear what we should return, minimum real value for now
466  if( v <= camera_sensor.black_level) {
468  }
469  int r=round_d2i((double)scale*(log2(v - camera_sensor.black_level) - log2_raw_neutral_count));
470  lua_pushnumber(L,r);
471  return 1;
472 }
static int rawop_set_pixel ( lua_State L)
static

Definiert in Zeile 268 der Datei rawhookops.c.

268  {
269  if(!raw_buffer_valid) {
270  return luaL_error(L,"raw data not available");
271  }
272  unsigned int x=luaL_checknumber(L,1);
273  unsigned int y=luaL_checknumber(L,2);
274  unsigned short v=luaL_checknumber(L,3);
275  // TODO
276  // might want to or error()?
277  if(x >= (unsigned)camera_sensor.raw_rowpix || y >= (unsigned)camera_sensor.raw_rows) {
278  return 0;
279  }
280  // TODO could check v
281  set_raw_pixel(x,y,v);
282  return 0;
283 }
static int rawop_set_pixels_rgbg ( lua_State L)
static

Definiert in Zeile 322 der Datei rawhookops.c.

322  {
323  if(!raw_buffer_valid) {
324  return luaL_error(L,"raw data not available");
325  }
326  unsigned int x=luaL_checknumber(L,1);
327  unsigned int y=luaL_checknumber(L,2);
328  unsigned short r=luaL_checknumber(L,3);
329  unsigned short g1=luaL_checknumber(L,4);
330  unsigned short b=luaL_checknumber(L,5);
331  unsigned short g2=luaL_optnumber(L,6,g1);
332 
333  x &= 0xFFFFFFFE;
334  y &= 0xFFFFFFFE;
335 
336  if(x >= (unsigned)camera_sensor.raw_rowpix - 1 || y >= (unsigned)camera_sensor.raw_rows - 1) {
337  return 0;
338  }
339  set_raw_pixel(x+cfa_offsets[CFA_R][0],y+cfa_offsets[CFA_R][1],r);
340  set_raw_pixel(x+cfa_offsets[CFA_G1][0],y+cfa_offsets[CFA_G1][1],g1);
341  set_raw_pixel(x+cfa_offsets[CFA_B][0],y+cfa_offsets[CFA_B][1],b);
342  set_raw_pixel(x+cfa_offsets[CFA_G2][0],y+cfa_offsets[CFA_G2][1],g2);
343  return 0;
344 }
void rawop_update_hook_status ( int  active)

Definiert in Zeile 752 der Datei rawhookops.c.

752  {
753  if(active) {
755  init_raw_params();
756  } else {
757  raw_buffer_valid = 0;
758  }
759 }
static int round_d2i ( double  v)
static

Definiert in Zeile 35 der Datei rawhookops.c.

35  {
36  if(v<0.0) {
37  return (int)(v - 0.5);
38  }
39  return (int)(v + 0.5);
40 }
void set_number_field ( lua_State L,
const char *  name,
int  value 
)

Definiert in Zeile 1783 der Datei luascript.c.

1784 {
1785  lua_pushnumber(L, val);
1786  lua_setfield(L, -2, key);
1787 }

Variablen-Dokumentation

const char* cfa_names[] ={"r","g1","g2","b"}
static

Definiert in Zeile 27 der Datei rawhookops.c.

unsigned cfa_offsets[4][2]
static

Definiert in Zeile 26 der Datei rawhookops.c.

double log2_raw_neutral_count
static

Definiert in Zeile 22 der Datei rawhookops.c.

int raw_buffer_valid
static

Definiert in Zeile 16 der Datei rawhookops.c.

unsigned raw_neutral
static

Definiert in Zeile 20 der Datei rawhookops.c.

const luaL_Reg rawop_funcs[]
static

Definiert in Zeile 702 der Datei rawhookops.c.

const luaL_Reg rawop_histo_meta_methods[]
static
Initialisierung:
= {
{"__gc", rawop_histo_gc},
}

Definiert in Zeile 688 der Datei rawhookops.c.

const luaL_Reg rawop_histo_methods[]
static
Initialisierung:
= {
{"update", rawop_histo_update},
{"range", rawop_histo_range},
{"total_pixels", rawop_histo_total_pixels},
{"bits", rawop_histo_bits},
{"free", rawop_histo_free},
}

Definiert in Zeile 693 der Datei rawhookops.c.