4#include "image_reader.h"
5#include "image_writer.h"
6#include <cgv/math/functions.h>
24 template <
typename T> T* get_ptr() {
return dv.
get_ptr<T>(); }
25 template <
typename T>
const T* get_ptr()
const {
return dv.
get_ptr<T>(); }
26 bool read(
const std::string& file_name)
29 if (ir.
open(file_name)) {
36 bool write(
const std::string& file_name) {
48 for (
unsigned y = 0; y < (h - 1) / 2; ++y) {
49 std::swap_ranges(dv.
get_ptr<
char>() + y*row_size, dv.
get_ptr<
char>() + (y + 1)*row_size, dv.
get_ptr<
char>() + (h - y - 1)*row_size);
55 double c[4] = { 0,0,0,0 };
57 for (
unsigned y = 0; y < n_y; ++y) {
58 for (
unsigned x = 0; x < n_x; ++x) {
59 for (
unsigned ci = 0; ci < n_c; ++ci)
60 c[ci] += get<double>(ci, src_ptr + entry_size*x + row_size*y);
63 double scale = 1.0 / (n_x*n_y);
64 for (
unsigned ci = 0; ci < n_c; ++ci)
65 set<double>(ci, dst_ptr, scale*c[ci]);
70 int w = (int)I.get_width();
71 int h = (int)I.get_height();
86 size_t size = entry_size * w * h;
87 memcpy(dst_ptr, src_ptr, size);
92 int w = (int)I.get_width();
93 int h = (int)I.get_height();
96 float x_scale = float(size_x) / wf;
97 float y_scale = float(size_y) / hf;
114 for(
unsigned j = 0; j < size_y; ++j) {
115 float y_start = y_end;
116 y_end = (j + 1) / y_scale;
118 if(y_end >= hf) y_end = hf - 0.000001f;
122 for(
unsigned i = 0; i < size_x; ++i) {
123 float x_start = x_end;
124 x_end = (i + 1) / x_scale;
126 if(x_end >= wf) x_end = wf - 0.000001f;
129 double c[4] = { 0.0, 0.0, 0.0, 0.0 };
131 for(
int y = (
int)y_start; y <= (int)y_end; ++y) {
132 float y_portion = 1.0f;
134 if(y == (
int)y_start) y_portion -= y_start - y;
135 if(y == (
int)y_end) y_portion -= y + 1.0f - y_end;
137 for(
int x = (
int)x_start; x <= (int)x_end; ++x) {
138 float x_portion = 1.0f;
139 if(x == (
int)x_start) x_portion -= x_start - x;
140 if(x == (
int)x_end) x_portion -= x + 1.0f - x_end;
142 double scaling = y_portion * x_portion;
145 for(
unsigned ci = 0; ci < n_c; ++ci)
146 c[ci] += get<double>(ci, src_ptr + entry_size * (x + w * y)) * scaling;
149 for(
unsigned ci = 0; ci < n_c; ++ci)
150 set<double>(ci, dst_ptr + entry_size * (i + (
unsigned)size_x*j), c[ci] / sum);
157 downscale((
unsigned)I.get_width() / fx, (
unsigned)I.get_height() / fy, I);
162 int w = (int)I.get_width();
163 int h = (int)I.get_height();
183 float step_x = 1.0f / float(size_x);
184 float step_y = 1.0f / float(size_y);
186 for(
unsigned j = 0; j < size_y; ++j) {
187 float ty = (float(j) + 0.5f) * step_y;
188 float fiy = ty * float(h) - 0.5f;
189 unsigned iy = unsigned(fiy);
190 ty = cgv::math::clamp(fiy - iy, 0.0f, 1.0f);
197 for(
unsigned i = 0; i < size_x; ++i) {
198 float tx = (float(i) + 0.5f) * step_x;
199 float fix = tx * float(w) - 0.5f;
200 unsigned ix = unsigned(fix);
201 tx = cgv::math::clamp(fix - ix, 0.0f, 1.0f);
208 unsigned idx00 = entry_size * (ix + w * iy);
209 unsigned idx10 = entry_size * (ix + 1 + w * iy);
210 unsigned idx01 = entry_size * (ix + w * (iy + 1));
211 unsigned idx11 = entry_size * (ix + 1 + w * (iy + 1));
213 double c[4] = { 0.0, 0.0, 0.0, 0.0 };
215 for(
unsigned ci = 0; ci < n_c; ++ci) {
216 double y0 = (1.0f - tx) * get<double>(ci, src_ptr + idx00) + tx * get<double>(ci, src_ptr + idx10);
217 double y1 = (1.0f - tx) * get<double>(ci, src_ptr + idx01) + tx * get<double>(ci, src_ptr + idx11);
219 c[ci] = (1.0f - ty) * y0 + ty * y1;
222 for(
unsigned ci = 0; ci < n_c; ++ci)
223 set<double>(ci, dst_ptr + entry_size * (i + (
unsigned)size_x*j), c[ci]);
230 unsigned w = (int)I.get_width();
231 unsigned h = (int)I.get_height();
237 bool shrink_x = size_x <= w;
238 bool shrink_y = size_y <= h;
240 if(w == size_x && h == size_y) {
245 if(shrink_x && shrink_y) {
247 }
else if(!shrink_x && !shrink_y) {
251 temp.downscale(shrink_x ? size_x : w, shrink_y ? size_y : h, I);
261#include <cgv/config/lib_end.h>
complete implementation of method actions that only call one method when entering a node
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.