
/*
 * CINELERRA
 * Copyright (C) 1997-2011 Adam Williams <broadcast at earthling dot net>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#ifndef HISTOGRAM_H
#define HISTOGRAM_H


#include "histogram.inc"
#include "histogramconfig.h"
#include "histogramwindow.inc"
#include "loadbalance.h"
#include "bccolors.h"
#include "pluginvclient.h"


class HistogramMain : public PluginVClient
{
public:
	HistogramMain(PluginServer *server);
	~HistogramMain();

	int process_buffer(VFrame *frame,
		int64_t start_position,
		double frame_rate);
	int is_realtime();
	void save_data(KeyFrame *keyframe);
	void read_data(KeyFrame *keyframe);
	void update_gui();
	void render_gui(void *data);
	void do_render_gui(HistogramWindow *gui);
	int calculate_use_opengl();
	int handle_opengl();

	PLUGIN_CLASS_MEMBERS(HistogramConfig)

// Interpolate quantized transfer table to linear output
	float calculate_level(float input, int mode, int do_value);
// Convert input to smoothed output by looking up in smooth table.
	float calculate_curve(float input);
// Calculate automatic settings
	void calculate_automatic(VFrame *data);
// Calculate histogram.
// Value is only calculated for preview.
	void calculate_histogram(VFrame *data, int do_value);
// Calculate the linear, smoothed, lookup curves
	void tabulate_curve(int **table, int idx, int use_value, int len);
	void tabulate_curve(int **table, int use_value, int len=-1);


	VFrame *input, *output;
	HistogramEngine *engine;

	int need_reconfigure;
	int *lookup[HISTOGRAM_MODES];
// No value applied to this
	int *preview_lookup[HISTOGRAM_MODES];
	int64_t *accum[HISTOGRAM_MODES];
// Input point being dragged or edited
	int current_point;
// Current channel being viewed
	int mode;
	int dragging_point;
	int point_x_offset;
	int point_y_offset;
	int w, h;
	int parade;
	int64_t last_position;
	int sum_frames, frames;
};


class HistogramPackage : public LoadPackage
{
public:
	HistogramPackage();
	int start, end;
};

class HistogramUnit : public LoadClient
{
public:
	HistogramUnit(HistogramEngine *server, HistogramMain *plugin);
	~HistogramUnit();
	void process_package(LoadPackage *package);
	HistogramEngine *server;
	HistogramMain *plugin;
	int64_t *accum[HISTOGRAM_MODES];
};

class HistogramEngine : public LoadServer
{
public:
	HistogramEngine(HistogramMain *plugin,
		int total_clients,
		int total_packages);
	void process_packages(int operation, VFrame *data, int do_value);
	void init_packages();
	LoadClient* new_client();
	LoadPackage* new_package();
	HistogramMain *plugin;
	int total_size;


	int operation;
	enum
	{
		HISTOGRAM,
		APPLY
	};
	VFrame *data;
	int do_value;
};











#endif
