Skip to content

Commit

Permalink
Kernel: Simplified C++ image manipulation.
Browse files Browse the repository at this point in the history
  • Loading branch information
phuang1024 committed Sep 12, 2021
1 parent 7b0d1cb commit 79a3485
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 40 deletions.
14 changes: 7 additions & 7 deletions src/pvkernel/addons/glare/glare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//

#include <cmath>
#include <algorithm>
#include <cmath>
#include <iostream>
#include "../../utils.hpp"
#include "../../random.hpp"

Expand Down Expand Up @@ -56,12 +57,11 @@ extern "C" void glare(UCH* img, const int width, const int height, CD intensity,
CD angle_dist = std::min(abs(angle-23), abs(angle-54));
CD angle_fac = dbounds(map_range(angle_dist, 0, 5, 0.96, 1));

CD fac = dbounds(angle_fac * dist_fac); // 0 = full white, 1 = no white
CD real_fac = 1 - ((1-fac)*curr_intensity); // Account for intensity
UCH original[3], modified[3];
img_getc(img, width, x, y, original);
img_mix(modified, white, original, real_fac);
img_setc(img, width, x, y, modified[0], modified[1], modified[2]);
CD fac = dbounds(1 - (angle_fac*dist_fac)); // 0 = full white, 1 = no white
CD real_fac = fac * curr_intensity; // Account for intensity
if (real_fac < 0)
std::cout << real_fac << std::endl;
img_mixadd(img, width, x, y, real_fac, white);
}
}
}
Expand Down
28 changes: 8 additions & 20 deletions src/pvkernel/addons/particles/particles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@ extern "C" void ptcl_render(UCH* img, const int width, const int height, const c

if (ptcls[i].age < MAX_AGE && img_bounds(width, height, x, y)) {
// Use an inverse quadratic interp to make it fade slowly, and suddenly go away.
// color_main = color of the main particle
// color_border = color of the side particles.
// color_main = color of the main pixel
// color_border = color of the side pixels.
// color_streak = color of the streak.
const double value_main = (1-pow(ptcls[i].age/MAX_AGE, 2)) * Random::uniform(0.95, 1.05);
const double value_border = dbounds(map_range(value_main, 0.6, 1, 0, 1), 0, 1);
Expand All @@ -227,22 +227,14 @@ extern "C" void ptcl_render(UCH* img, const int width, const int height, const c
const UCH color_border[3] = {(UCH)(r*value_border), (UCH)(g*value_border), (UCH)(b*value_border)};
const UCH color_streak[3] = {(UCH)(r*value_streak), (UCH)((double)value_streak/1.08*g), (UCH)((double)value_streak/1.06*b)};

UCH original[3], modified[3];
img_getc(img, width, x, y, original);
img_mix(modified, original, color_main, intensity);
img_addc(img, width, x, y, modified);

// Render pixels surrounding particle
// Render pixels surrounding particle (also including)
img_mixadd(img, width, x, y, intensity, color_main);
if (value_border > 0) {
for (int dx = -1; dx <= 1; dx++) {
for (int dy = -1; dy <= 1; dy++) {
const int nx = x+dx, ny = y+dy;
if (img_bounds(width, height, nx, ny)) {
UCH original[3], modified[3];
img_getc(img, width, nx, ny, original);
img_mix(modified, original, color_streak, intensity/3.0);
img_addc(img, width, nx, ny, modified);
}
if (img_bounds(width, height, nx, ny))
img_mixadd(img, width, nx, ny, intensity/3.0, color_border);
}
}
}
Expand Down Expand Up @@ -286,12 +278,8 @@ extern "C" void ptcl_render(UCH* img, const int width, const int height, const c
const float x_fac = dbounds((fabs(1-tx) - (1-curr_x_size)) / curr_x_size);
const float final_fac = x_fac * y_fac;

if (final_fac > 0) {
UCH original[3], modified[3];
img_getc(img, width, nx, ny, original);
img_mix(modified, original, color_border, final_fac/5.0);
img_addc(img, width, nx, ny, modified);
}
if (final_fac > 0)
img_mixadd(img, width, nx, ny, final_fac/5.0, color_streak);
}
}
}
Expand Down
22 changes: 9 additions & 13 deletions src/pvkernel/addons/smoke/smoke.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
#include "../../utils.hpp"

constexpr double AIR_RESIST = 0.95;
constexpr int MAX_AGE = 5;
constexpr int DIFF_DIST = 4;
constexpr int DIFF_STR = 1;
constexpr int MAX_AGE = 6;
constexpr int DIFF_DIST = 4;
constexpr int DIFF_STR = 1;


struct SmokePtcl {
Expand Down Expand Up @@ -213,23 +213,19 @@ extern "C" void smoke_render(UCH* img, const int width, const int height, const
const int x = (int)ptcls[i].x, y = (int)ptcls[i].y;

if (ptcls[i].age < MAX_AGE && img_bounds(width, height, x, y)) {
// REMEMBER TO CHANGE MAX_AGE CONSTANT IF YOU ARE ADJUSTING THIS
CD value = dbounds(map_range(ptcls[i].age, 3, 5, 1, 0));
// Between 3 and MAX_AGE secs, the brightness will go from 1 to 0
CD value = dbounds(map_range(ptcls[i].age, 3, MAX_AGE, 1, 0));
const UCH color[3] = {(UCH)(r*value), (UCH)(g*value), (UCH)(b*value)};

UCH original[3], modified[3];
img_getc(img, width, x, y, original);
img_mix(modified, original, color, intensity/Random::uniform(9, 11));
img_addc(img, width, x, y, modified);
CD fac = intensity / Random::uniform(9, 11);
img_mixadd(img, width, x, y, fac, color);

for (int dx = -2; dx <= 2; dx++) {
for (int dy = -2; dy <= 2; dy++) {
const int nx = x+dx, ny = y+dy;
if (img_bounds(width, height, nx, ny)) {
UCH original[3], modified[3];
img_getc(img, width, nx, ny, original);
img_mix(modified, original, color, intensity/Random::uniform(28, 32));
img_addc(img, width, nx, ny, modified);
CD fac = intensity / Random::uniform(28, 32);
img_mixadd(img, width, nx, ny, fac, color);
}
}
}
Expand Down
13 changes: 13 additions & 0 deletions src/pvkernel/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,16 @@ MODS void img_mix(UCH* dest, const UCH* c1, const UCH* c2, CD fac) {
for (int i = 0; i < 3; i++)
dest[i] = ibounds(c1[i]*(1-fac) + c2[i]*fac, 0, 255);
}

MODS void img_mixadd(UCH* img, const int width, const int x, const int y, CD fac, const UCH r,
const UCH g, const UCH b) {
UCH original[3], modified[3];
const UCH input[3] = {r, g, b};
img_getc(img, width, x, y, original);
img_mix(modified, original, input, fac);
img_setc(img, width, x, y, modified[0], modified[1], modified[2]);
}

MODS void img_mixadd(UCH* img, const int width, const int x, const int y, CD fac, const UCH input[3]) {
img_mixadd(img, width, x, y, fac, input[0], input[1], input[2]);
}
13 changes: 13 additions & 0 deletions src/pvkernel/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,16 @@ MODS void img_getc(UCH* img, const int width, const int x, const int y, UCH* col
* @param dest destination color.
*/
MODS void img_mix(UCH* dest, const UCH* c1, const UCH* c2, CD fac);

/**
* Add color to image after mixing with current color.
* Current color is the color that is currently on the image.
* @param fac 1 is full input color, 0 is full current color
*/
MODS void img_mixadd(UCH* img, const int width, const int x, const int y, CD fac, const UCH r,
const UCH g, const UCH b);

/**
* Overload for img_mixadd which takes UCH[3] for color.
*/
MODS void img_mixadd(UCH* img, const int width, const int x, const int y, CD fac, const UCH input[3]);

0 comments on commit 79a3485

Please sign in to comment.