Krita Source Code Documentation
Loading...
Searching...
No Matches
gimp_bump_map.cpp File Reference
#include "gimp_bump_map.h"
#include <QRect>
#include "kis_pixel_selection.h"
#include "kis_image.h"
#include <memory>

Go to the source code of this file.

Classes

struct  bumpmap_params_t
 

Macros

#define CLAMP(x, l, h)   qBound(l, x, h)
 
#define G_PI   M_PI
 
#define MAX(x, y)   qMax(x,y)
 
#define MIN(x, y)   qMin(x,y)
 
#define MOD(x, y)   ((x) % (y))
 

Typedefs

typedef bool gboolean
 
typedef double gdouble
 
typedef int gint
 
typedef qint32 gint32
 
typedef quint8 guchar
 

Functions

void bumpmap (KisPixelSelectionSP device, const QRect &selectionRect, const bumpmap_vals_t &bmvals)
 
void bumpmap_init_params (bumpmap_params_t *params, const bumpmap_vals_t &bmvals)
 
void bumpmap_row (const bumpmap_vals_t &bmvals, guchar *dest, gint width, const guchar *bm_row1, const guchar *bm_row2, const guchar *bm_row3, bumpmap_params_t *params)
 
void convertRow (quint8 *data, int width, const quint8 *lut)
 

Macro Definition Documentation

◆ CLAMP

#define CLAMP ( x,
l,
h )   qBound(l, x, h)

Definition at line 30 of file gimp_bump_map.cpp.

◆ G_PI

#define G_PI   M_PI

Definition at line 28 of file gimp_bump_map.cpp.

◆ MAX

#define MAX ( x,
y )   qMax(x,y)

Definition at line 31 of file gimp_bump_map.cpp.

◆ MIN

#define MIN ( x,
y )   qMin(x,y)

Definition at line 32 of file gimp_bump_map.cpp.

◆ MOD

#define MOD ( x,
y )   ((x) % (y))

Definition at line 29 of file gimp_bump_map.cpp.

Typedef Documentation

◆ gboolean

typedef bool gboolean

Definition at line 24 of file gimp_bump_map.cpp.

◆ gdouble

typedef double gdouble

Definition at line 26 of file gimp_bump_map.cpp.

◆ gint

typedef int gint

Definition at line 22 of file gimp_bump_map.cpp.

◆ gint32

typedef qint32 gint32

Definition at line 23 of file gimp_bump_map.cpp.

◆ guchar

typedef quint8 guchar

Definition at line 25 of file gimp_bump_map.cpp.

Function Documentation

◆ bumpmap()

void bumpmap ( KisPixelSelectionSP device,
const QRect & selectionRect,
const bumpmap_vals_t & bmvals )

Definition at line 64 of file gimp_bump_map.cpp.

67{
70
71 bumpmap_params_t params;
72 bumpmap_init_params (&params, bmvals);
73
74 const QRect dataRect = kisGrowRect(selectionRect, 1);
75
76 const int dataRowSize = dataRect.width() * sizeof(quint8);
77 const int selectionRowSize = selectionRect.width() * sizeof(quint8);
78 QScopedArrayPointer<quint8> dstRow(new quint8[selectionRowSize]);
79
80 std::unique_ptr<quint8[]> bmRow1(new quint8[dataRowSize]);
81 std::unique_ptr<quint8[]> bmRow2(new quint8[dataRowSize]);
82 std::unique_ptr<quint8[]> bmRow3(new quint8[dataRowSize]);
83
84 device->readBytes(bmRow1.get(), dataRect.left(), dataRect.top(), dataRect.width(), 1);
85 device->readBytes(bmRow2.get(), dataRect.left(), dataRect.top() + 1, dataRect.width(), 1);
86 device->readBytes(bmRow3.get(), dataRect.left(), dataRect.top() + 2, dataRect.width(), 1);
87
88 convertRow(bmRow1.get(), dataRect.width(), params.lut);
89 convertRow(bmRow2.get(), dataRect.width(), params.lut);
90 convertRow(bmRow3.get(), dataRect.width(), params.lut);
91
92 for (int row = selectionRect.top();
93 row < selectionRect.top() + selectionRect.height(); row++) {
94
95 bumpmap_row (bmvals, dstRow.data(), selectionRect.width(),
96 bmRow1.get() + 1, bmRow2.get() + 1, bmRow3.get() + 1,
97 &params);
98
99 device->writeBytes(dstRow.data(), selectionRect.left(), row, selectionRect.width(), 1);
100
101 bmRow1.swap(bmRow2);
102 bmRow2.swap(bmRow3);
103
104 device->readBytes(bmRow3.get(), dataRect.left(), row + 1, dataRect.width(), 1);
105 convertRow(bmRow3.get(), dataRect.width(), params.lut);
106 }
107}
void readBytes(quint8 *data, qint32 x, qint32 y, qint32 w, qint32 h) const
void writeBytes(const quint8 *data, qint32 x, qint32 y, qint32 w, qint32 h)
void bumpmap_init_params(bumpmap_params_t *params, const bumpmap_vals_t &bmvals)
void bumpmap_row(const bumpmap_vals_t &bmvals, guchar *dest, gint width, const guchar *bm_row1, const guchar *bm_row2, const guchar *bm_row3, bumpmap_params_t *params)
void convertRow(quint8 *data, int width, const quint8 *lut)
#define KIS_ASSERT_RECOVER_RETURN(cond)
Definition kis_assert.h:75
T kisGrowRect(const T &rect, U offset)
Definition kis_global.h:186

References bumpmap_init_params(), bumpmap_row(), convertRow(), KIS_ASSERT_RECOVER_RETURN, kisGrowRect(), bumpmap_params_t::lut, KisPaintDevice::readBytes(), KisPaintDevice::writeBytes(), bumpmap_vals_t::xofs, and bumpmap_vals_t::yofs.

◆ bumpmap_init_params()

void bumpmap_init_params ( bumpmap_params_t * params,
const bumpmap_vals_t & bmvals )

Definition at line 109 of file gimp_bump_map.cpp.

110{
111 /* Convert to radians */
112 const gdouble azimuth = G_PI * bmvals.azimuth / 180.0;
113 const gdouble elevation = G_PI * bmvals.elevation / 180.0;
114
115 gint lz, nz;
116 gint i;
117
118 /* Calculate the light vector */
119 params->lx = cos (azimuth) * cos (elevation) * 255.0;
120 params->ly = sin (azimuth) * cos (elevation) * 255.0;
121 lz = sin (elevation) * 255.0;
122
123 /* Calculate constant Z component of surface normal */
124 /* (depth may be 0 if non-interactive) */
125 nz = (6 * 255) / qMax (bmvals.depth, 1);
126 params->nz2 = nz * nz;
127 params->nzlz = nz * lz;
128
129 /* Optimize for vertical normals */
130 params->background = lz;
131
132 /* Calculate darkness compensation factor */
133 params->compensation = sin(elevation);
134
135 /* Create look-up table for map type */
136 for (i = 0; i < 256; i++)
137 {
138 gdouble n;
139
140 switch (bmvals.type)
141 {
142 case SPHERICAL:
143 n = i / 255.0 - 1.0;
144 params->lut[i] = (int) (255.0 * sqrt(1.0 - n * n) + 0.5);
145 break;
146
147 case SINUSOIDAL:
148 n = i / 255.0;
149 params->lut[i] = (int) (255.0 *
150 (sin((-G_PI / 2.0) + G_PI * n) + 1.0) /
151 2.0 + 0.5);
152 break;
153
154 case LINEAR:
155 default:
156 params->lut[i] = i;
157 }
158
159 if (bmvals.invert)
160 params->lut[i] = 255 - params->lut[i];
161 }
162}
#define G_PI
int gint
double gdouble
@ SINUSOIDAL
@ SPHERICAL
@ LINEAR
Definition nugrid.h:26

References bumpmap_vals_t::azimuth, bumpmap_params_t::background, bumpmap_params_t::compensation, bumpmap_vals_t::depth, bumpmap_vals_t::elevation, G_PI, bumpmap_vals_t::invert, LINEAR, bumpmap_params_t::lut, bumpmap_params_t::lx, bumpmap_params_t::ly, bumpmap_params_t::nz2, bumpmap_params_t::nzlz, SINUSOIDAL, SPHERICAL, and bumpmap_vals_t::type.

◆ bumpmap_row()

void bumpmap_row ( const bumpmap_vals_t & bmvals,
guchar * dest,
gint width,
const guchar * bm_row1,
const guchar * bm_row2,
const guchar * bm_row3,
bumpmap_params_t * params )

Definition at line 165 of file gimp_bump_map.cpp.

172{
173 gint xofs1, xofs2;
174 gint x;
175
176 for (x = 0; x < width; x++) {
177 gint xofs3;
178 gint shade;
179 gint nx, ny;
180
181 /* Calculate surface normal from bump map */
182
183 xofs2 = x;
184 xofs1 = xofs2 - 1;
185 xofs3 = xofs2 + 1;
186
187 nx = (bm_row1[xofs1] + bm_row2[xofs1] + bm_row3[xofs1] -
188 bm_row1[xofs3] - bm_row2[xofs3] - bm_row3[xofs3]);
189 ny = (bm_row3[xofs1] + bm_row3[xofs2] + bm_row3[xofs3] -
190 bm_row1[xofs1] - bm_row1[xofs2] - bm_row1[xofs3]);
191
192 /* Shade */
193
194 if ((nx == 0) && (ny == 0)) {
195 shade = params->background;
196 } else {
197 gint ndotl = nx * params->lx + ny * params->ly + params->nzlz;
198
199 if (ndotl < 0) {
200 shade = params->compensation * bmvals.ambient;
201 } else {
202 shade = ndotl / sqrt (nx * nx + ny * ny + params->nz2);
203
204 shade = shade + MAX(0.0, (255 * params->compensation - shade)) *
205 bmvals.ambient / 255;
206 }
207 }
208
209 /* Paint */
210
211 if (bmvals.compensate) {
212 int result = shade / params->compensation;
213 *dest++ = MIN(255, result);
214 } else {
215 *dest++ = shade;
216 }
217 }
218}
#define MIN(x, y)
#define MAX(x, y)

References bumpmap_vals_t::ambient, bumpmap_params_t::background, bumpmap_vals_t::compensate, bumpmap_params_t::compensation, bumpmap_params_t::lx, bumpmap_params_t::ly, MAX, MIN, bumpmap_params_t::nz2, and bumpmap_params_t::nzlz.

◆ convertRow()

void convertRow ( quint8 * data,
int width,
const quint8 * lut )

Definition at line 56 of file gimp_bump_map.cpp.

57{
58 for (int i = 0; i < width; i++) {
59 *data = lut[*data];
60 data++;
61 }
62}