1#include "image_drawable.h"
3#include <cgv/media/image/image_reader.h>
4#include <cgv/media/image/image_writer.h>
5#include <cgv/media/axis_aligned_box.h>
6#include <cgv/base/import.h>
7#include <cgv/math/ftransform.h>
8#include <cgv/render/attribute_array_binding.h>
9#include <cgv_gl/gl/gl_tools.h>
10#include <cgv_gl/gl/gl.h>
22image_drawable::image_drawable() : min_value(0,0,0,0), max_value(1,1,1,1), gamma4(1,1,1,1)
41 pan_tilt =
vec2(0.0f);
43 k1 = k2 = k3 = k4 = k5 = k6 = p1 = p2 = 0.0f;
44 checker_lambda = 0.0f;
51 if (tex_ids.size() > 1 && start_time != -2) {
53 if (start_time == -1) {
56 for (
unsigned i = 0; i<current_image; ++i) {
57 start_time -= durations[i];
58 anim_time += durations[i];
63 if (dt > anim_time+durations[current_image]) {
64 while (dt > anim_time+durations[current_image]) {
65 anim_time += durations[current_image];
67 if (current_image >= tex_ids.size()) {
69 start_time += anim_time;
89 std::vector<vertex> V;
90 V.push_back({
vec2(-1.0f,-1.0f),
vec2(0.0f,0.0f) });
91 V.push_back({
vec2( 1.0f,-1.0f),
vec2(1.0f,0.0f) });
92 V.push_back({
vec2( 1.0f, 1.0f),
vec2(1.0f,1.0f) });
93 V.push_back({
vec2(-1.0f, 1.0f),
vec2(0.0f,1.0f) });
96 aab.set_attribute_array(ctx, 0, cgv::render::get_element_type(V.front().position), vbo, 0, V.size(),
sizeof(vertex));
97 aab.enable_array(ctx, 0);
98 aab.set_attribute_array(ctx, 1, cgv::render::get_element_type(V.front().texcoord), vbo, offsetof(vertex, texcoord), V.size(),
sizeof(vertex));
99 aab.enable_array(ctx, 1);
100 return prog.build_program(ctx,
"image.glpr");
109 if (!tex_ids.empty())
110 glDeleteTextures(GLsizei(tex_ids.size()), &tex_ids.front());
127 file_name = _file_name;
135 for (
unsigned i=0; i<_files.size(); ++i) {
137 true, &aspect, &use_blending);
140 tex_ids.push_back(tex_id);
141 durations.push_back(0.04f);
142 std::cout <<
"read image " << _file_name+
"/"+_files[i] << std::endl;
144 if (!_files.empty()) {
147 ir.
open(_file_name+
"/"+_files[0]);
156 file_name = _file_name;
172 if (files.size() > nr)
173 nr = (unsigned)files.size();
175 for (
unsigned i=0; i<nr; ++i) {
176 std::string input_file_name = file_name;
177 if (i < files.size()) {
178 input_file_name +=
'/';
179 input_file_name += files[i];
182 std::vector<data_format> palette_formats;
183 std::vector<data_view> palettes;
188 if (!ir.
open(input_file_name)) {
192 palettes.resize(palette_formats.size());
205 for (
unsigned i=0; i<nr; ++i) {
207 for (
unsigned j=0; j<palette_formats.size(); ++j) {
217 unsigned char* src_ptr = dv.
get_ptr<
unsigned char>() +
221 unsigned char* dst_ptr = dv_out->
get_ptr<
unsigned char>();
222 for (
int j=0; j<h; ++j) {
228 if (!iw.
write_image(*dv_out, (std::vector<const_data_view>*)(&palettes))) {
243 ctx.push_modelview_matrix();
244 ctx.mul_modelview_matrix(cgv::math::scale4<double>(aspect, 1, 1));
246 ctx.push_modelview_matrix();
247 ctx.mul_modelview_matrix(cgv::math::translate4<double>(-1, -1, 0) * cgv::math::scale4<double>(2.0 /
W, 2.0 / H, 1));
248 float x = (float)
selection.get_min_pnt()(0);
249 float y = (float)
selection.get_min_pnt()(1);
250 float w = (float)
selection.get_extent()(0);
251 float h = (float)
selection.get_extent()(1);
253 P.push_back(
vec2(x, y));
254 P.push_back(
vec2(x + w, y));
255 P.push_back(
vec2(x + w, y + h));
256 P.push_back(
vec2(x, y + h));
259 ctx.set_color(
rgb(1, 0, 0));
262 glDrawArrays(GL_LINE_LOOP, 0, 4);
265 ctx.pop_modelview_matrix();
268 bool mix_enabled =
false;
269 if (tex_ids.size() > 0) {
270 glActiveTexture(GL_TEXTURE0);
271 glBindTexture(GL_TEXTURE_2D, tex_ids[current_image]);
272 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, spherical ? GL_REPEAT : GL_CLAMP_TO_EDGE);
273 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, spherical ? GL_REPEAT : GL_CLAMP_TO_EDGE);
274 if (use_mixing && mix_with != -1 && mix_with < tex_ids.size()) {
275 glActiveTexture(GL_TEXTURE1);
276 glBindTexture(GL_TEXTURE_2D, tex_ids[mix_with]);
278 glActiveTexture(GL_TEXTURE0);
280 if (start_time == -2)
285 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
288 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
289 glDisable(GL_CULL_FACE);
290 ctx.mul_modelview_matrix(cgv::math::scale4<double>(1, -1, 1));
291 if (prog.is_linked()) {
293 prog.set_uniform(ctx,
"min_value", min_value);
294 prog.set_uniform(ctx,
"max_value", max_value);
295 prog.set_uniform(ctx,
"gamma4", gamma4);
296 prog.set_uniform(ctx,
"image", 0);
297 prog.set_uniform(ctx,
"checker_lambda", checker_lambda);
298 prog.set_uniform(ctx,
"cx", cx);
299 prog.set_uniform(ctx,
"cy", cy);
300 prog.set_uniform(ctx,
"k1", k1);
301 prog.set_uniform(ctx,
"k2", k2);
302 prog.set_uniform(ctx,
"k3", k3);
303 prog.set_uniform(ctx,
"k4", k4);
304 prog.set_uniform(ctx,
"k5", k5);
305 prog.set_uniform(ctx,
"k6", k6);
306 prog.set_uniform(ctx,
"p1", p1);
307 prog.set_uniform(ctx,
"p2", p2);
308 prog.set_uniform(ctx,
"s", s);
309 prog.set_uniform(ctx,
"use_texture", tex_ids.size() > 0);
310 prog.set_uniform(ctx,
"tessellation_level", tess_level);
311 prog.set_uniform(ctx,
"use_mixing", use_mixing);
312 prog.set_uniform(ctx,
"mix_with", 1);
313 prog.set_uniform(ctx,
"mix_param", mix_param);
314 prog.set_uniform(ctx,
"spherical", spherical);
315 prog.set_uniform(ctx,
"pan_tilt", pan_tilt);
316 prog.set_uniform(ctx,
"scale",
vec2(
float(aspect * scale), scale));
318 glPatchParameteri(GL_PATCH_VERTICES, 4);
319 glDrawArrays(GL_PATCHES, 0, 4);
324 ctx.tesselate_unit_square();
329 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
330 glEnable(GL_CULL_FACE);
331 ctx.pop_modelview_matrix();
size_t get_step_size(unsigned int dim) const
return the step size in bytes in the i-th dimension
cgv::type::func::transfer_const< P, S * >::type get_ptr() const
return a data pointer to type S
the data view gives access to a data array of one, two, three or four dimensions.
static bool enable_global_array(const context &ctx, int loc)
enable attribute array of given location
static bool set_global_attribute_array(const context &ctx, int loc, const vertex_buffer &vbo, type_descriptor td, size_t size, size_t offset, unsigned stride=0)
point array of vertex attribute at location loc to vertex buffer array array stored in CPU memory; in...
static bool disable_global_array(const context &ctx, int loc)
disable attribute array of given location
base class for all drawables, which is independent of the used rendering API.
void post_redraw()
posts a redraw event to the current context if one is available
void clear(context &)
destruct textures and shader program
image_drawable()
construct base image drawable
bool init(context &)
construct shader program
void timer_event(double t, double dt)
timer event that can be used for animation, connect this to some animation trigger
bool save_images(const std::string &output_file_name)
function to save all images
bool read_images(const std::string &_file_name, const std::vector< std::string > &_files)
function to read a list of images
bool read_image(const std::string &_file_name)
function to read a single image
bool show_selection
whether to show the selection rectangle
ibox2 selection
extent selection box
void draw(context &)
draw the image
a shader program combines several shader code fragments to a complete definition of the shading pipel...
bool enable(context &ctx)
enable the shader program
the base namespace holds the base hierarchy, support for plugin registration and signals
namespace for data management components
namespace for opengl specific GPU programming
bool read_image_to_textures(const std::string &file_name, std::vector< unsigned > &tex_ids, std::vector< float > &durations, bool mipmaps, double *aspect_ptr, bool *has_alpha_ptr)
read several images from one image file that can contain an animation
unsigned int read_image_to_texture(const std::string &file_name, bool mipmaps, double *aspect_ptr, bool *has_alpha_ptr)
read the given image file into a texture and return the texture id or -1 in case of failure.
namespace for api independent GPU programming
namespace that holds tools that dont fit any other namespace
this header is dependency free
cgv::media::color< float, cgv::media::RGB > rgb
declare rgb color type with 32 bit components
cgv::math::fvec< int32_t, 2 > ivec2
declare type of 2d 32 bit integer vectors
cgv::math::fvec< float, 2 > vec2
declare type of 2d single precision floating point vectors