You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
SimpleSkyCam/histogram.cc

161 lines
3.5 KiB

/***************************************************************************************
*
* histogram.cc is part of SimpleSkyCam.
*
*****************************************************************************************/
#include "simpleskycam.h"
#include "histogram.h"
#include "gui.h"
#include "configuration.h"
#include "video.h"
GtkWidget *histogram_da = NULL;
int histogram[3][256]; // 6* hist_width min, max, min , max.....
int histogram_max; // max vlaue
extern GtkBuilder *_builder_; // work around for threads
void cb_histogramda_draw(GtkWidget *area, cairo_t *cr, int w, int h, gpointer data) {
int clientw, clienth, px, py2;
GdkRGBA color;
char txt[255];
if (histogram_da == NULL) // should only be called once
histogram_da = GTK_WIDGET(gtk_builder_get_object (GTK_BUILDER(_builder_), "histogram-da"));
clienth = gtk_widget_get_allocated_height(histogram_da);
clientw = gtk_widget_get_allocated_width(histogram_da);
//
// put the drawing area on the screen
cairo_paint(cr);
cairo_fill (cr);
if (histogram_max == 0) return;
for (int chan = 0; chan < 3; chan++) {
switch (chan) {
case 0:
color.blue = 0.0;
color.red = 1.0;
color.green = 0.0;
color.alpha = 0.5;
break;
case 1:
color.blue = 0.0;
color.red = 0.0;
color.green = 1.0;
color.alpha = 0.5;
break;
case 2:
color.blue = 1.0;
color.red = 0.0;
color.green = 0.0;
color.alpha = 0.5;
break;
}
gdk_cairo_set_source_rgba(cr, &color);
for (int i = 0; i < 256; i++) {
px = 16 + ((clientw-32) * i) / 256;
py2 = (clienth-16) - ((clienth-32) * histogram[chan][i]) / histogram_max;
if (i == 0) cairo_move_to(cr, px, py2);
else cairo_line_to(cr, px, py2);
}
cairo_stroke(cr);
}
// draw some text
snprintf (txt, 255, "%d", histogram_max);
for (int i = 0; i < 5; i++) {
int dx, dy;
switch (i) {
case 0:
cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
dx = -1;
dy = 0;
break;
case 1:
dx = +1;
dy = 0;
break;
case 2:
dx = 0;
dy = -1;
break;
case 3:
dx = 0;
dy = +1;
break;
case 4:
cairo_set_source_rgb(cr, 0.1, 0.1, 0.1);
dx = 0;
dy = 0;
break;
}
cairo_select_font_face (cr, "sans-serif", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
cairo_set_font_size (cr, 12);
cairo_move_to (cr, 10+dx, 15+dy);
cairo_show_text(cr, txt);
cairo_stroke(cr);
}
};
/*
* test function
*/
void histogram_update(VideoFrame *vf) {
// save some time
static int to = 0;
if ((++to) % 8 == 0) return;
int chan, i, x, y;
if (vf == NULL) return;
if (vf->w <= 0 ) return;
for (chan = 0; chan < 3; chan++) for (i = 0; i < 256; i++)
histogram[chan][i] = 0;
for (i = 0, x = 0; x < vf->w; x++) {
for (y = 0; y < vf->h; y++) {
for (chan = 0; chan < 3; chan++) {
histogram[chan][vf->data[i++]]++;
}
}
}
if (histogram_da)
gdk_window_invalidate_rect(gtk_widget_get_window(histogram_da), NULL, true);
for (histogram_max = 0, chan = 0; chan < 3; chan++) for (i = 0; i < 256; i++)
if (histogram_max < histogram[chan][i]) histogram_max = histogram[chan][i];
};
void cb_histogramda_motion (GtkWidget *widget, GdkEvent *event, gpointer data) {
// printf ("%s:%d %s\n", __FILE__, __LINE__, __FUNCTION__);
};
void cb_histogramda_btnpress (GtkWidget *widget, gpointer data) {
printf ("%s:%d %s\n", __FILE__, __LINE__, __FUNCTION__);
};
void cb_histogramda_btnrelease (GtkWidget *widget, gpointer data) {
printf ("%s:%d %s\n", __FILE__, __LINE__, __FUNCTION__);
};