2015-03-30 16:00:43 +00:00
|
|
|
/*
|
|
|
|
* \brief Functor for converting pixel formats by applying dithering
|
|
|
|
* \author Norman Feske
|
|
|
|
* \date 2014-09-10
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2017-02-20 12:23:52 +00:00
|
|
|
* Copyright (C) 2014-2017 Genode Labs GmbH
|
2015-03-30 16:00:43 +00:00
|
|
|
*
|
|
|
|
* This file is part of the Genode OS framework, which is distributed
|
2017-02-20 12:23:52 +00:00
|
|
|
* under the terms of the GNU Affero General Public License version 3.
|
2015-03-30 16:00:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _DITHER_PAINTER_H_
|
|
|
|
#define _DITHER_PAINTER_H_
|
|
|
|
|
|
|
|
#include <util/dither_matrix.h>
|
|
|
|
#include <os/surface.h>
|
|
|
|
|
|
|
|
|
|
|
|
struct Dither_painter
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Surface and texture must have the same size
|
|
|
|
*/
|
|
|
|
template <typename DST_PT, typename SRC_PT>
|
|
|
|
static inline void paint(Genode::Surface<DST_PT> &surface,
|
|
|
|
Genode::Texture<SRC_PT> const &texture)
|
|
|
|
{
|
|
|
|
if (surface.size() != texture.size()) return;
|
2018-11-06 13:41:27 +00:00
|
|
|
if (!texture.pixel() || !texture.alpha()) return;
|
2015-03-30 16:00:43 +00:00
|
|
|
|
|
|
|
Genode::Surface_base::Rect const clipped = surface.clip();
|
|
|
|
|
|
|
|
if (!clipped.valid()) return;
|
|
|
|
|
Make util/geometry.h C++20 friendly
- Move header to base/include to make it applicable for base types
like 'Affinity' down the road.
- Represent 'Rect' as typle of point and area, which is the most
common form of initialization, creates in valid 'Rect' by default.
- Turn Point, Area, and Rect into compound types, making x, y, w, h, at,
area accessible without a method call
- 'Rect::Compound' function for constructing a 'Rect' from two points,
replacing a former constructor
- Use result type 'Rect::Cut_remainder' instead of out parameters.
Fixes #5239
2024-06-05 10:40:13 +00:00
|
|
|
unsigned const offset = surface.size().w*clipped.y1() + clipped.x1();
|
2015-03-30 16:00:43 +00:00
|
|
|
|
|
|
|
DST_PT *dst, *dst_line = surface.addr() + offset;
|
|
|
|
SRC_PT const *src_pixel, *src_pixel_line = texture.pixel() + offset;
|
|
|
|
unsigned char const *src_alpha, *src_alpha_line = texture.alpha() + offset;
|
|
|
|
|
Make util/geometry.h C++20 friendly
- Move header to base/include to make it applicable for base types
like 'Affinity' down the road.
- Represent 'Rect' as typle of point and area, which is the most
common form of initialization, creates in valid 'Rect' by default.
- Turn Point, Area, and Rect into compound types, making x, y, w, h, at,
area accessible without a method call
- 'Rect::Compound' function for constructing a 'Rect' from two points,
replacing a former constructor
- Use result type 'Rect::Cut_remainder' instead of out parameters.
Fixes #5239
2024-06-05 10:40:13 +00:00
|
|
|
unsigned const line_len = surface.size().w;
|
2015-03-30 16:00:43 +00:00
|
|
|
|
|
|
|
for (int y = clipped.y1(), h = clipped.h() ; h--; y++) {
|
|
|
|
|
|
|
|
src_pixel = src_pixel_line;
|
|
|
|
src_alpha = src_alpha_line;
|
|
|
|
dst = dst_line;
|
|
|
|
|
|
|
|
for (int x = clipped.x1(), w = clipped.w(); w--; x++) {
|
|
|
|
|
|
|
|
int const v = Genode::Dither_matrix::value(x, y) >> 4;
|
|
|
|
|
|
|
|
SRC_PT const pixel = *src_pixel++;
|
|
|
|
unsigned char const alpha = *src_alpha++;
|
|
|
|
|
|
|
|
int const r = pixel.r() - v;
|
|
|
|
int const g = pixel.g() - v;
|
|
|
|
int const b = pixel.b() - v;
|
|
|
|
int const a = alpha ? (int)alpha - v : 0;
|
|
|
|
|
|
|
|
using Genode::min;
|
|
|
|
using Genode::max;
|
|
|
|
|
|
|
|
*dst++ = DST_PT(max(0, r), max(0, g), max(0, b), max(0, a));
|
|
|
|
}
|
|
|
|
|
|
|
|
src_pixel_line += line_len;
|
|
|
|
src_alpha_line += line_len;
|
|
|
|
dst_line += line_len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* _DITHER_PAINTER_H_ */
|