Skip to content

Commit

Permalink
Don't convert sampled color back and forth
Browse files Browse the repository at this point in the history
It got sampled as a float, turned into a 15 bit color only to be
converted back into a float. Now it just stays a float.
  • Loading branch information
askmeaboutlo0m committed Jul 29, 2023
1 parent 86efb3c commit 81ca8b1
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 28 deletions.
14 changes: 7 additions & 7 deletions extern/drawdance/libengine/dpengine/brush_engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -596,13 +596,13 @@ static void get_color_mypaint_pigment(MyPaintSurface2 *self, float x, float y,
DP_LayerContent *lc = be->lc;
if (lc) {
int diameter = DP_min_int(DP_float_to_int(radius * 2.0f + 0.5f), 255);
DP_UPixel15 color = DP_layer_content_sample_color_at(
DP_UPixelFloat color = DP_layer_content_sample_color_at(
lc, be->stamp_buffer, DP_float_to_int(x + 0.5f),
DP_float_to_int(y + 0.5f), diameter, &be->last_diameter);
*color_r = DP_channel15_to_float(color.r);
*color_g = DP_channel15_to_float(color.g);
*color_b = DP_channel15_to_float(color.b);
*color_a = DP_channel15_to_float(color.a);
*color_r = color.r;
*color_g = color.g;
*color_b = color.b;
*color_a = color.a;
}
else {
*color_r = 0.0f;
Expand Down Expand Up @@ -940,9 +940,9 @@ static DP_UPixelFloat sample_classic_smudge(DP_BrushEngine *be,
float y, float pressure)
{
int diameter = get_classic_smudge_diameter(cb, pressure);
return DP_upixel15_to_float(DP_layer_content_sample_color_at(
return DP_layer_content_sample_color_at(
lc, be->stamp_buffer, DP_float_to_int(x), DP_float_to_int(y), diameter,
&be->last_diameter));
&be->last_diameter);
}

static void update_classic_smudge(DP_BrushEngine *be, DP_ClassicBrush *cb,
Expand Down
26 changes: 13 additions & 13 deletions extern/drawdance/libengine/dpengine/layer_content.c
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ bool DP_layer_content_pick_at(DP_LayerContent *lc, int x, int y,
return false;
}

static DP_UPixel15 sample_dab_color(DP_LayerContent *lc, DP_BrushStamp stamp)
static DP_UPixelFloat sample_dab_color(DP_LayerContent *lc, DP_BrushStamp stamp)
{
uint16_t *weights = stamp.data;
int diameter = stamp.diameter;
Expand Down Expand Up @@ -405,7 +405,7 @@ static DP_UPixel15 sample_dab_color(DP_LayerContent *lc, DP_BrushStamp stamp)
float required_alpha =
DP_int_to_float(DP_square_int(diameter) * 30) / (float)DP_BIT15;
if (alpha < required_alpha) {
return DP_upixel15_zero();
return DP_upixel_float_zero();
}

// Calculate final average
Expand All @@ -419,23 +419,23 @@ static DP_UPixel15 sample_dab_color(DP_LayerContent *lc, DP_BrushStamp stamp)
green = CLAMP(green / alpha, 0.0f, 1.0f);
blue = CLAMP(blue / alpha, 0.0f, 1.0f);

return (DP_UPixel15){
.b = DP_channel_float_to_15(blue),
.g = DP_channel_float_to_15(green),
.r = DP_channel_float_to_15(red),
.a = DP_channel_float_to_15(alpha),
return (DP_UPixelFloat){
.b = blue,
.g = green,
.r = red,
.a = alpha,
};
}

DP_UPixel15 DP_layer_content_sample_color_at(DP_LayerContent *lc,
uint16_t *stamp_buffer, int x,
int y, int diameter,
int *in_out_last_diameter)
DP_UPixelFloat DP_layer_content_sample_color_at(DP_LayerContent *lc,
uint16_t *stamp_buffer, int x,
int y, int diameter,
int *in_out_last_diameter)
{
if (x >= 0 && y >= 0 && x < lc->width && y < lc->height) {
if (diameter < 2) {
DP_Pixel15 pixel = DP_layer_content_pixel_at(lc, x, y);
return DP_pixel15_unpremultiply(pixel);
return DP_upixel15_to_float(DP_pixel15_unpremultiply(pixel));
}
else {
int last_diameter;
Expand All @@ -452,7 +452,7 @@ DP_UPixel15 DP_layer_content_sample_color_at(DP_LayerContent *lc,
}
}
else {
return DP_upixel15_zero();
return DP_upixel_float_zero();
}
}

Expand Down
8 changes: 4 additions & 4 deletions extern/drawdance/libengine/dpengine/layer_content.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ DP_Pixel15 DP_layer_content_pixel_at(DP_LayerContent *lc, int x, int y);
bool DP_layer_content_pick_at(DP_LayerContent *lc, int x, int y,
unsigned int *out_context_id);

DP_UPixel15 DP_layer_content_sample_color_at(DP_LayerContent *lc,
uint16_t *stamp_buffer, int x,
int y, int diameter,
int *in_out_last_diameter);
DP_UPixelFloat DP_layer_content_sample_color_at(DP_LayerContent *lc,
uint16_t *stamp_buffer, int x,
int y, int diameter,
int *in_out_last_diameter);

DP_LayerList *DP_layer_content_sub_contents_noinc(DP_LayerContent *lc);

Expand Down
7 changes: 7 additions & 0 deletions extern/drawdance/libengine/dpengine/pixels.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ DP_UPixel15 DP_upixel8_to_15(DP_UPixel8 pixel);
DP_UPixel8 DP_upixel15_to_8(DP_UPixel15 pixel);
DP_UPixelFloat DP_upixel8_to_float(DP_UPixel8 pixel);
DP_UPixelFloat DP_upixel15_to_float(DP_UPixel15 pixel);
DP_UPixelFloat DP_upixel15_to_float_round(DP_UPixel15 pixel);
DP_UPixel8 DP_upixel_float_to_8(DP_UPixelFloat pixel);

void DP_pixels8_to_15(DP_Pixel15 *dst, const DP_Pixel8 *src, int count);
Expand Down Expand Up @@ -132,6 +133,12 @@ DP_INLINE DP_UPixel15 DP_upixel15_from_color(uint32_t color)
return DP_upixel8_to_15(pixel);
}

DP_INLINE DP_UPixelFloat DP_upixel_float_zero(void)
{
DP_UPixelFloat pixel = {0.0f, 0.0f, 0.0f, 0.0f};
return pixel;
}

DP_INLINE DP_UPixelFloat DP_upixel_float_from_color(uint32_t color)
{
DP_UPixel8 pixel = {color};
Expand Down
8 changes: 4 additions & 4 deletions src/libclient/drawdance/layercontent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ bool LayerContent::isNull() const
QColor LayerContent::sampleColorAt(
uint16_t *stampBuffer, int x, int y, int diameter, int &lastDiameter) const
{
DP_UPixel15 pixel = DP_layer_content_sample_color_at(
DP_UPixelFloat pixel = DP_layer_content_sample_color_at(
m_data, stampBuffer, x, y, diameter, &lastDiameter);
return QColor{
DP_channel15_to_8(pixel.r), DP_channel15_to_8(pixel.g),
DP_channel15_to_8(pixel.b), DP_channel15_to_8(pixel.a)};
QColor color;
color.setRgbF(pixel.r, pixel.g, pixel.b, pixel.a);
return color;
}

QImage LayerContent::toImage(const QRect &rect) const
Expand Down

0 comments on commit 81ca8b1

Please sign in to comment.