CHDK_DE Vorschauversion  Trunk Rev. 6014
 Alle Datenstrukturen Dateien Funktionen Variablen Typdefinitionen Aufzählungen Aufzählungswerte Makrodefinitionen
rawhookops.c-Dateireferenz
#include "camera_info.h"
#include "conf.h"
#include "raw.h"
#include "math.h"
#include "lualib.h"
#include "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 30 der Datei rawhookops.c.

#define CFA_G1   1

Definiert in Zeile 28 der Datei rawhookops.c.

#define CFA_G2   2

Definiert in Zeile 29 der Datei rawhookops.c.

#define CFA_R   0

Definiert in Zeile 27 der Datei rawhookops.c.

#define RAWOP_HISTO_META   "rawop.histo_meta"

Definiert in Zeile 31 der Datei rawhookops.c.

Dokumentation der Funktionen

static void init_raw_params ( void  )
static

Definiert in Zeile 741 der Datei rawhookops.c.

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

Definiert in Zeile 760 der Datei rawhookops.c.

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

Definiert in Zeile 506 der Datei rawhookops.c.

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

Definiert in Zeile 484 der Datei rawhookops.c.

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

Definiert in Zeile 355 der Datei rawhookops.c.

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

Definiert in Zeile 177 der Datei rawhookops.c.

177  {
179  return 1;
180 }
static int rawop_get_active_left ( lua_State L)
static

Definiert in Zeile 150 der Datei rawhookops.c.

150  {
152  return 1;
153 }
static int rawop_get_active_top ( lua_State L)
static

Definiert in Zeile 159 der Datei rawhookops.c.

159  {
161  return 1;
162 }
static int rawop_get_active_width ( lua_State L)
static

Definiert in Zeile 168 der Datei rawhookops.c.

168  {
170  return 1;
171 }
static int rawop_get_bits_per_pixel ( lua_State L)
static

Definiert in Zeile 75 der Datei rawhookops.c.

75  {
77  return 1;
78 }
static int rawop_get_black_level ( lua_State L)
static

Definiert in Zeile 103 der Datei rawhookops.c.

103  {
105  return 1;
106 }
static int rawop_get_cfa ( lua_State L)
static

Definiert in Zeile 44 der Datei rawhookops.c.

44  {
46  return 1;
47 }
static int rawop_get_cfa_offsets ( lua_State L)
static

Definiert in Zeile 59 der Datei rawhookops.c.

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

Definiert in Zeile 229 der Datei rawhookops.c.

229  {
231  return 1;
232 }
static int rawop_get_jpeg_left ( lua_State L)
static

Definiert in Zeile 202 der Datei rawhookops.c.

202  {
204  return 1;
205 }
static int rawop_get_jpeg_top ( lua_State L)
static

Definiert in Zeile 211 der Datei rawhookops.c.

211  {
213  return 1;
214 }
static int rawop_get_jpeg_width ( lua_State L)
static

Definiert in Zeile 220 der Datei rawhookops.c.

220  {
222  return 1;
223 }
static int rawop_get_pixel ( lua_State L)
static

Definiert in Zeile 245 der Datei rawhookops.c.

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

Definiert in Zeile 292 der Datei rawhookops.c.

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

Definiert in Zeile 130 der Datei rawhookops.c.

130  {
132  return 1;
133 }
static int rawop_get_raw_neutral ( lua_State L)
static

Definiert in Zeile 90 der Datei rawhookops.c.

90  {
92  return 1;
93 }
static int rawop_get_raw_width ( lua_State L)
static

Definiert in Zeile 121 der Datei rawhookops.c.

121  {
123  return 1;
124 }
static int rawop_get_white_level ( lua_State L)
static

Definiert in Zeile 112 der Datei rawhookops.c.

112  {
114  return 1;
115 }
static int rawop_histo_bits ( lua_State L)
static

Definiert in Zeile 662 der Datei rawhookops.c.

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

Definiert in Zeile 675 der Datei rawhookops.c.

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

Definiert in Zeile 682 der Datei rawhookops.c.

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

Definiert in Zeile 600 der Datei rawhookops.c.

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

Definiert in Zeile 649 der Datei rawhookops.c.

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

Definiert in Zeile 528 der Datei rawhookops.c.

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

Definiert in Zeile 404 der Datei rawhookops.c.

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

Definiert in Zeile 461 der Datei rawhookops.c.

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

Definiert in Zeile 267 der Datei rawhookops.c.

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

Definiert in Zeile 321 der Datei rawhookops.c.

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

Definiert in Zeile 751 der Datei rawhookops.c.

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

Definiert in Zeile 34 der Datei rawhookops.c.

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

Definiert in Zeile 1845 der Datei luascript.c.

1846 {
1847  lua_pushnumber(L, val);
1848  lua_setfield(L, -2, key);
1849 }

Variablen-Dokumentation

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

Definiert in Zeile 26 der Datei rawhookops.c.

unsigned cfa_offsets[4][2]
static

Definiert in Zeile 25 der Datei rawhookops.c.

double log2_raw_neutral_count
static

Definiert in Zeile 21 der Datei rawhookops.c.

int raw_buffer_valid
static

Definiert in Zeile 15 der Datei rawhookops.c.

unsigned raw_neutral
static

Definiert in Zeile 19 der Datei rawhookops.c.

const luaL_Reg rawop_funcs[]
static

Definiert in Zeile 701 der Datei rawhookops.c.

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

Definiert in Zeile 687 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 692 der Datei rawhookops.c.