diff --git a/CHANGELOG.md b/CHANGELOG.md index f3309b34..43799a4d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,12 +39,19 @@ New: - A hidden layer, or an effect turned off, keeps a faint eye in the lists, to show where to click to turn it on again. - Dose every effect with its setting Mix, from 0 % (the image as it is) to 100 % (the effect alone), on its page of the inspector, by OSC, MCP and links at `effect//mix`, and in the project. - A shader file can show groups of its settings folded at first, with the key `GROUPS` of its header (see `docs/extensions/isf.md`). +- An effect can read its image with `IMG_NORM_PIXEL_SEAMLESS`, which leaves no grey seam where its position jumps from one place of the image to another, as between two tiles, on a generator or a source with an effect (see `docs/extensions/isf.md`). - Color Adjustment sets the levels of the image, black point, white point, gamma, output black and output white, and the gain of red, green and blue, in two groups folded at first. A project saved before looks the same. The levels follow those of Romain Dura, and the gains RGB EQ by VIDVOX: `CREDITS.md`, in the folder `shaders`, gives their licences. - Add the effect Invert: the negative of the image, or only its light and dark while the colours stay, or only what is above a threshold, as a solarized photograph; each of red, green and blue can be left as it is. - Add the effect Colorize: two colours, one for the dark and one for the light, and the shades between, from the luminance, the strongest channel or the mean of the channels. - Add the effect Threshold: two colours, for what is darker and what is lighter than a threshold, with a soft edge if wanted; either side can be made transparent, to cut out the image. - Add the effect Posterize: a few levels of each colour, in flat bands, with soft edges or a dithering pattern if wanted, or only the brightness in levels. - Add the effect Luma Key: the dark parts of the image become transparent, or the light ones, fading between a low and a high threshold, to show what is behind; on a source, every shape that uses it. +- Add the effect Kaleidoscope: a slice of the image repeated around a centre, each copy beside its mirror image, from 2 to 32 copies; the slice taken, the whole and the zoom can be turned or changed. On a layer, it uses only the part of the image the layer shows. It starts from Kaleidoscope of VIDVOX (MIT License). +- Add the effect Mirror: one half of the image kept and its mirror image in place of the other, left and right, top and bottom, or both, each from either side, with four switches that a key or a pad can turn on and off; the mirrors can be moved and turned. It starts from Mirror of VIDVOX (MIT License). +- Add the effect Transform: the image moved, scaled and turned, and in a group folded at first, tiled in columns and rows, every other tile in a mirror, rows slid as bricks or slanted, without a seam between the tiles; another folded group stretches the image, sets the point it turns around and what shows beyond its edges. As it is added, it changes nothing. On a layer, it uses only the part of the image the layer shows. It starts from Zoom, Rotate and Quad Tile of VIDVOX (MIT License). +- Add the effect Pixelate: the image in blocks of one colour, 32 across its width at first, a number that a single slider changes; in a group folded at first, round blocks or diamonds, wide or tall blocks, and a gap between them, of a colour that can be transparent. On a layer, it uses only the part of the image the layer shows. It starts from Pixellate of VIDVOX (MIT License). +- Add the effect RGB Shift: the red, the green and the blue of the image moved apart, leaving coloured fringes along its edges, by 1 % of its height at first; the slider goes to 5 %, and a typed value, OSC or MCP to 50 %. The colours move along an angle or away from the centre, as through a lens; in a group folded at first, each moves by its own share of the distance, either way. On a layer, it uses only the part of the image the layer shows. It starts from Chromatic Aberration of VIDVOX (MIT License). +- Add the effect Edges: white lines on black where the brightness of the image changes, about 3 pixels thick on an image of 1080 lines, with a strength; in a group folded at first, the thickness, a threshold that hides the grain of a video, the colour of the lines or the colours of the image, and black, the image or nothing between them. On a layer, it uses only the part of the image the layer shows. It starts from Edges of VIDVOX (MIT License). - The menu Add Effect shows the effects by family, Color and Keying for now, from the first of the `CATEGORIES` of their file; `list_effect_types` of MCP gives the category of each (`category`). - Control every setting of a source or a layer by OSC at an address that follows its path: `/mapmap/layer//`, for example `/mapmap/layer/3/output/vertices/5`. The new MCP tool `list_settings` gives the settings of a source or a layer with their type, bounds and options, and `set_property` takes a path. - Show the output in an ordinary window, to work without a projector or keep an eye on the output: View > Output > Output in a Window (Ctrl+Shift+F), which can be moved and resized; the image keeps its proportions. Output Fills a Screen (Ctrl+F) covers exactly the chosen screen with a window without a frame. On macOS, it is no longer the full screen of the system: no animation, no desktop of its own, and the window covers the menu bar and the Dock. Ctrl+F and Escape go back to the window or to hidden; Escape does not close the window. The output button of the toolbar has a menu with the same commands and the list of screens, which replaces View > Output screen (#209). diff --git a/docs/extensions/isf.md b/docs/extensions/isf.md index de1144e6..d0ae0106 100644 --- a/docs/extensions/isf.md +++ b/docs/extensions/isf.md @@ -14,7 +14,7 @@ MapMap reads the files `*.fs` of the folder `shaders` installed with it, once, a A file missing from the folder is read from the copy built into MapMap. Editing a file, a default value for instance, changes the generator at the next start. On macOS, editing a file inside the application breaks its signature: keep a copy of the original. -Each shader becomes a generator in File > Add Generator, named after its file: `color-wheel.fs` gives "Color Wheel". Its identifier is `mapmap.shaders:` followed by the file name, as in `mapmap.shaders:checker.fs`. The prefix `mapmap.` is reserved for what is shipped with MapMap. A project writes the identifier as the type of the source, with `shader_hash`, a hash of the file; opening a project saved with another version of the file writes a warning in the log. A project that names a shader MapMap does not have keeps the source as it is, with a "missing" image, and saves it unchanged. +Each shader becomes a generator in File > Add Generator, named after its file: `color-wheel.fs` gives "Color Wheel", and the names of colour models are written in capitals: `rgb-shift.fs` gives "RGB Shift". Its identifier is `mapmap.shaders:` followed by the file name, as in `mapmap.shaders:checker.fs`. The prefix `mapmap.` is reserved for what is shipped with MapMap. A project writes the identifier as the type of the source, with `shader_hash`, a hash of the file; opening a project saved with another version of the file writes a warning in the log. A project that names a shader MapMap does not have keeps the source as it is, with a "missing" image, and saves it unchanged. A file that MapMap cannot read is refused with a message in the log (Window > Console) and does not affect the other files. @@ -68,6 +68,12 @@ What is refused, with a message in the log: | Threshold | Two colours, for what is darker and what is lighter than a threshold, either of which may be transparent; a soft edge; the luminance, one channel or the strongest compared | Written for MapMap | | Posterize | A few levels of each colour, in flat bands; soft edges between them; a dithering pattern that suggests the shades between; or only the brightness in levels | Posterize by VIDVOX, Luminance Posterize by zoidberg (MIT License) | | Luma Key | The dark parts made transparent, or the light ones, fading between two thresholds; the luminance, one channel or the strongest compared. On a source, the shapes that use it are see-through there | Written for MapMap | +| Kaleidoscope | A slice of the image repeated around a centre, each copy beside its mirror image; the slice taken and the whole can be turned, and zoomed. Beyond the edges, the image is read again in a mirror. On a layer, only the part the layer shows is used | Kaleidoscope by VIDVOX (MIT License) | +| Mirror | One half of the image kept and its mirror image in place of the other: left and right, top and bottom, or both, each from either side; the mirrors moved or turned. Beyond the edges, the image is read again in a mirror | Mirror by VIDVOX (MIT License) | +| Transform | The image moved, scaled, turned and stretched around a pivot, and tiled in columns and rows, every other tile in a mirror if wanted, rows slid as bricks or slanted. Beyond the edges, nothing, the edge pixels, the image repeated or the image in a mirror. On a layer, only the part the layer shows is used. It reads with `IMG_NORM_PIXEL_SEAMLESS` | Zoom, Rotate and Quad Tile by VIDVOX (MIT License) | +| Pixelate | The image in blocks of one colour, counted across its width, each showing the colour at its centre: square, round or diamonds, wide or tall, with a gap of a colour, transparent if wanted, between them. The rows that do not fit are cut equally at the top and the bottom. On a layer, only the part the layer shows is used. It reads with `IMG_NORM_PIXEL_SEAMLESS` | Pixellate by VIDVOX (MIT License) | +| RGB Shift | The red, the green and the blue of the image moved apart, along an angle or away from the centre, each by its own share of the distance, which is a share of the height of the image. Beyond the edges, a colour moved in from outside is missing. On a layer, only the part the layer shows is used | Chromatic Aberration by VIDVOX (MIT License) | +| Edges | Lines where the brightness of the image changes, found with the Sobel filter from eight reads, nine when the line or the background shows the image: a strength, a thickness as a share of the height of the image, a threshold against the grain of a video, lines of a colour or of the colours of the image, on black, on the image or on nothing. The edges of the image draw no line. On a layer, only the part the layer shows is used | Edges by VIDVOX (MIT License) | The files of VIDVOX and of zoidberg come from the collection ISF-Files of VIDVOX (github.com/Vidvox/ISF-Files). `CREDITS.md`, in the folder `shaders`, gives the licences. @@ -93,6 +99,19 @@ A group is shown unfolded. The key `GROUPS` of the header, which MadMapper does "GROUPS": [ { "LABEL": "Levels", "COLLAPSED": true } ] ``` +### Reading without seams, MapMap's own + +The graphics card keeps smaller, smoothed copies of some images, the generators and the images of the sources that have an effect, and reads the one that the size of the pixels drawn calls for, so that a shrunk image does not shimmer. It measures that size by how far the position read moves from one pixel to its neighbour. An effect whose position jumps from one place of the image to another between two neighbouring pixels, as between two tiles, misleads it: at the jump, it reads the smallest copy, and a grey, half transparent seam shows. + +`IMG_NORM_PIXEL_SEAMLESS(image, normCoord, smoothCoord)` reads the image at `normCoord`, as `IMG_NORM_PIXEL`, but chooses the copy as if reading at `smoothCoord`: the same position without its jumps, which moves at the same pace. For tiles, it is the position before each tile is brought back between 0 and 1: + +```glsl +vec2 grid = isf_FragNormCoord * vec2(3.0, 2.0); +gl_FragColor = IMG_NORM_PIXEL_SEAMLESS(inputImage, fract(grid), grid); +``` + +Read close to the edges of the image, where the image is shrunk, a smoothed copy also takes in what is beyond them: keep the position from the edges by as many pixels as the image is shrunk. While an effect reads this way, its image is read without anisotropic filtering, which keeps lines sharp in shapes seen in perspective, but would leave a seam: it reads several points spread along the jump. The images without smaller copies, such as videos, have no seam to fear. Other ISF software does not know this function: an effect that uses it is for MapMap only. + ### How a setting is shown, MapMap's own | Key | Meaning | diff --git a/resources/shaders.qrc b/resources/shaders.qrc index e5d6bb2f..fc705464 100644 --- a/resources/shaders.qrc +++ b/resources/shaders.qrc @@ -15,5 +15,11 @@ shaders/threshold.fs shaders/posterize.fs shaders/luma-key.fs + shaders/kaleidoscope.fs + shaders/mirror.fs + shaders/transform.fs + shaders/pixelate.fs + shaders/rgb-shift.fs + shaders/edges.fs diff --git a/resources/shaders/CREDITS.md b/resources/shaders/CREDITS.md index 5862d6ca..c98f6890 100644 --- a/resources/shaders/CREDITS.md +++ b/resources/shaders/CREDITS.md @@ -8,6 +8,12 @@ The shaders of this folder are part of MapMap, under the GNU General Public Lice | `invert.fs` | Channels: RGB Invert by VIDVOX (ISF-Files, MIT License). | | `colorize.fs` | False Color by zoidberg (ISF-Files, MIT License). | | `posterize.fs` | Posterize by VIDVOX and Luminance Posterize by zoidberg (ISF-Files, MIT License), with the conversions between RGB and HSV of Sam Hocevar that the latter gives. | +| `kaleidoscope.fs` | Kaleidoscope by VIDVOX (ISF-Files, MIT License). | +| `mirror.fs` | Mirror by VIDVOX (ISF-Files, MIT License). | +| `transform.fs` | Zoom, Rotate and Quad Tile by VIDVOX (ISF-Files, MIT License). | +| `pixelate.fs` | Pixellate by VIDVOX (ISF-Files, MIT License). | +| `rgb-shift.fs` | Chromatic Aberration by VIDVOX (ISF-Files, MIT License). | +| `edges.fs` | Edges by VIDVOX (ISF-Files, MIT License). | | `gradient.fs` | Linear Gradient and Radial Gradient by Carter Rosenberg (ISF-Files, MIT License). Blend: the OKLab colour space of Björn Ottosson (MIT License). | | `stripes.fs` | Stripes by VIDVOX (ISF-Files, MIT License). Blend: the OKLab colour space of Björn Ottosson (MIT License). | | `waves.fs` | Blend: the OKLab colour space of Björn Ottosson (MIT License). | diff --git a/resources/shaders/edges.fs b/resources/shaders/edges.fs new file mode 100644 index 00000000..b36b90b1 --- /dev/null +++ b/resources/shaders/edges.fs @@ -0,0 +1,160 @@ +/*{ + "ISFVSN": "2", + "DESCRIPTION": "Draws the outlines of the image: lines where its brightness changes.", + "CREDIT": "Based on Edges by VIDVOX (ISF-Files, MIT License); MapMap contributors, GNU GPL version 3 or later. See CREDITS.md.", + "CATEGORIES": ["Stylize"], + "INPUTS": [ + { + "NAME": "inputImage", + "TYPE": "image" + }, + { + "NAME": "strength", + "TYPE": "float", + "LABEL": "Strength", + "MIN": 0.0, + "MAX": 10.0, + "SLIDER_MAX": 4.0, + "DEFAULT": 1.0, + "UNIT": "percent", + "DESCRIPTION": "How bright the lines are: above 100 %, faint outlines show too." + }, + { + "NAME": "thickness", + "TYPE": "float", + "LABEL": "Options/Thickness", + "MIN": 0.0, + "MAX": 0.05, + "SLIDER_MAX": 0.01, + "DEFAULT": 0.0025, + "UNIT": "percent", + "DESCRIPTION": "The thickness of the lines, as a share of the height of the image; never thinner than a pixel." + }, + { + "NAME": "threshold", + "TYPE": "float", + "LABEL": "Options/Threshold", + "MIN": 0.0, + "MAX": 0.9, + "DEFAULT": 0.05, + "UNIT": "percent", + "DESCRIPTION": "The changes of brightness below this one draw no line, which hides the grain of a video." + }, + { + "NAME": "line_from", + "TYPE": "long", + "LABEL": "Options/Line From", + "VALUES": [0, 1], + "LABELS": ["Color", "Image"], + "DEFAULT": 0, + "DESCRIPTION": "The colour of the lines: the line colour, or the colour of the image under them." + }, + { + "NAME": "line_color", + "TYPE": "color", + "LABEL": "Options/Line Color", + "DEFAULT": [1.0, 1.0, 1.0, 1.0], + "DESCRIPTION": "The colour of the lines, when they take it from the line colour." + }, + { + "NAME": "background", + "TYPE": "long", + "LABEL": "Options/Background", + "VALUES": [0, 1, 2], + "LABELS": ["Black", "Image", "Transparent"], + "DEFAULT": 0, + "DESCRIPTION": "What is seen between the lines: black, the image, or nothing, which lets what is behind show." + } + ], + "GROUPS": [ + { "LABEL": "Options", "COLLAPSED": true } + ] +}*/ + +// An effect: MapMap gives it the image of a source or of a layer (see docs/extensions/isf.md). +// Written in GLSL 1.20, which MapMap compiles on every platform, the Raspberry Pi included. +// +// Based on Edges by VIDVOX (ISF-Files, MIT License, see CREDITS.md), which finds the changes of +// brightness with the Sobel filter, from the eight pixels around each pixel. Reading them at the +// thickness asked, without a vertex shader, the threshold that keeps the full brightness of the +// lines, the colour of the lines and the background are by the MapMap contributors. On a layer, +// everything stays inside the part of the image the layer shows. + +// Values of line_from and of background. +const int LINE_FROM_IMAGE = 1; +const int BACKGROUND_IMAGE = 1; +const int BACKGROUND_TRANSPARENT = 2; + +// The share of red, green and blue in the brightness (Rec. 709). +const vec3 LUMINANCE = vec3(0.2126, 0.7152, 0.0722); + +// The largest result of the Sobel filter along one direction, from black to white: 1 + 2 + 1. +const float SOBEL_SCALE = 4.0; + +// The weight of the neighbour straight across in the Sobel filter; those in a corner weigh 1. +const float SOBEL_SIDE = 2.0; + +// The thinnest line, in pixels of the image. +const float MIN_THICKNESS = 1.0; + +// Below this alpha, a colour mixed from the line and the background is taken as transparent. +const float MIN_ALPHA = 1e-4; + +// The distance from the edge at which the image is read, in pixels of the copy of the image the +// graphics card reads: one pixel, because it mixes that copy with the next smaller one, whose +// pixels are twice as large, and half of one of them must stay inside the edge. +const float EDGE_MARGIN = 1.0; + +// By how many pixels of the image a position moves from one pixel drawn to the next, at least +// one: where the image is shrunk, the graphics card reads a smaller, smoothed copy of it. +float shrinkOf(vec2 position) { + vec2 pixels = position * RENDERSIZE; + return max(1.0, max(length(dFdx(pixels)), length(dFdy(pixels)))); +} + +// The brightness of the image at a position counted from the top left, kept inside the image so +// that its edges draw no line; a transparent part counts as dark. +float brightnessAt(vec2 position, vec2 margin) { + vec2 inside = clamp(position, margin, 1.0 - margin); + vec4 color = IMG_NORM_PIXEL(inputImage, vec2(inside.x, 1.0 - inside.y)); + return dot(color.rgb, LUMINANCE) * color.a; +} + +void main() { + // The position drawn, counted from the top left, and the distance to the neighbours read. + vec2 point = vec2(isf_FragNormCoord.x, 1.0 - isf_FragNormCoord.y); + vec2 reach = vec2(max(MIN_THICKNESS, thickness * RENDERSIZE.y)) / RENDERSIZE; + vec2 margin = EDGE_MARGIN * shrinkOf(point) / RENDERSIZE; + + // The Sobel filter: how the brightness changes across and down. + float topLeft = brightnessAt(point + vec2(-reach.x, -reach.y), margin); + float top = brightnessAt(point + vec2(0.0, -reach.y), margin); + float topRight = brightnessAt(point + vec2(reach.x, -reach.y), margin); + float left = brightnessAt(point + vec2(-reach.x, 0.0), margin); + float right = brightnessAt(point + vec2(reach.x, 0.0), margin); + float bottomLeft = brightnessAt(point + vec2(-reach.x, reach.y), margin); + float bottom = brightnessAt(point + vec2(0.0, reach.y), margin); + float bottomRight = brightnessAt(point + vec2(reach.x, reach.y), margin); + float across = (topRight + SOBEL_SIDE * right + bottomRight) - (topLeft + SOBEL_SIDE * left + bottomLeft); + float down = (bottomLeft + SOBEL_SIDE * bottom + bottomRight) - (topLeft + SOBEL_SIDE * top + topRight); + float change = length(vec2(across, down)) / SOBEL_SCALE; + + // Above the threshold, the line grows to its full brightness at the largest change. + float line = clamp(strength * (change - threshold) / (1.0 - threshold), 0.0, 1.0); + + // The image under the pixel is read only when the line or the background shows it. + vec4 under = vec4(0.0); + if (line_from == LINE_FROM_IMAGE || background == BACKGROUND_IMAGE) + under = IMG_NORM_PIXEL(inputImage, vec2(point.x, 1.0 - point.y)); + vec4 lineColor = line_from == LINE_FROM_IMAGE ? under : line_color; + vec4 behind = vec4(0.0, 0.0, 0.0, 1.0); + if (background == BACKGROUND_IMAGE) + behind = under; + else if (background == BACKGROUND_TRANSPARENT) + behind = vec4(0.0); + + // The line over the background, each colour weighed by its alpha. + float alpha = mix(behind.a, lineColor.a, line); + vec3 weighed = mix(behind.rgb * behind.a, lineColor.rgb * lineColor.a, line); + gl_FragColor = alpha > MIN_ALPHA ? vec4(weighed / alpha, alpha) : vec4(0.0); +} diff --git a/resources/shaders/kaleidoscope.fs b/resources/shaders/kaleidoscope.fs new file mode 100644 index 00000000..81ded2bf --- /dev/null +++ b/resources/shaders/kaleidoscope.fs @@ -0,0 +1,120 @@ +/*{ + "ISFVSN": "2", + "DESCRIPTION": "Repeats a slice of the image around a centre, each copy beside its mirror image, as in a kaleidoscope.", + "CREDIT": "Based on Kaleidoscope by VIDVOX (ISF-Files, MIT License); MapMap contributors, GNU GPL version 3 or later. See CREDITS.md.", + "CATEGORIES": ["Geometry"], + "INPUTS": [ + { + "NAME": "inputImage", + "TYPE": "image" + }, + { + "NAME": "segments", + "TYPE": "long", + "LABEL": "Segments", + "MIN": 2, + "MAX": 32, + "SLIDER_MAX": 16, + "DEFAULT": 6, + "DESCRIPTION": "The number of copies around the centre. Each is a slice of the image beside its mirror image, so that they meet without a seam." + }, + { + "NAME": "center", + "TYPE": "point2D", + "LABEL": "Center", + "MIN": [0.0, 0.0], + "MAX": [1.0, 1.0], + "DEFAULT": [0.5, 0.5], + "DESCRIPTION": "The centre of the kaleidoscope, from the top left corner (0, 0) to the bottom right corner (1, 1)." + }, + { + "NAME": "sample_rotation", + "TYPE": "float", + "LABEL": "Options/Slice Angle", + "MIN": -180.0, + "MAX": 180.0, + "DEFAULT": 0.0, + "STEP": 1.0, + "UNIT": "degrees", + "DESCRIPTION": "Turns the slice taken from the image around the centre, clockwise, to show another part of it." + }, + { + "NAME": "rotation", + "TYPE": "float", + "LABEL": "Options/Rotation", + "MIN": -180.0, + "MAX": 180.0, + "DEFAULT": 0.0, + "STEP": 1.0, + "UNIT": "degrees", + "DESCRIPTION": "Turns the whole kaleidoscope around its centre, clockwise." + }, + { + "NAME": "zoom", + "TYPE": "float", + "LABEL": "Options/Zoom", + "MIN": 0.1, + "MAX": 10.0, + "SLIDER_MAX": 4.0, + "DEFAULT": 1.0, + "UNIT": "percent", + "DESCRIPTION": "Enlarges the slices above 100 %, or makes them smaller below. Beyond the edges, the image is read again in a mirror." + } + ], + "GROUPS": [ + { "LABEL": "Options", "COLLAPSED": true } + ] +}*/ + +// An effect: MapMap gives it the image of a source or of a layer (see docs/extensions/isf.md). +// Written in GLSL 1.20, which MapMap compiles on every platform, the Raspberry Pi included. +// +// Based on Kaleidoscope by VIDVOX (ISF-Files, MIT License, see CREDITS.md): the angle around +// the centre is folded into one segment, then into one half of it by a mirror, so that any +// number of segments meets without a seam. The centre counted from the top left, the turns of +// the result, the zoom and the mirrored reading beyond the edges are by the MapMap contributors. +// On a layer, everything stays inside the part of the image the layer shows. + +const float TWO_PI = 6.28318530718; + +// The distance from the edge at which the image is read, in pixels of the copy of the image the +// graphics card reads: one pixel, because it mixes that copy with the next smaller one, whose +// pixels are twice as large, and half of one of them must stay inside the edge. +const float EDGE_MARGIN = 1.0; + +// A point turned by an angle, clockwise when y goes down. +vec2 turned(vec2 point, float turn) { + float c = cos(turn); + float s = sin(turn); + return vec2(c * point.x - s * point.y, s * point.x + c * point.y); +} + +// A position from (0, 0) to (1, 1), folded back as in a mirror when it goes beyond, and kept +// from the edges by EDGE_MARGIN pixels of the copy of the image the graphics card reads there. Where +// the image is shrunk, it reads a smaller, smoothed copy, whose pixels are larger: by how many +// pixels of the image the position moves from one pixel drawn to the next, measured before the +// fold, which would hide it where two neighbouring pixels fall on either side of the mirror. +vec2 foldedInside(vec2 position) { + vec2 folded = 1.0 - abs(1.0 - mod(position, 2.0)); + vec2 pixels = position * RENDERSIZE; + float shrink = max(1.0, max(length(dFdx(pixels)), length(dFdy(pixels)))); + vec2 margin = EDGE_MARGIN * shrink / RENDERSIZE; + return clamp(folded, margin, 1.0 - margin); +} + +void main() { + // In pixels from the centre, counted from the top left, so that the slices are not + // stretched when the image is not square. + vec2 point = vec2(isf_FragNormCoord.x, 1.0 - isf_FragNormCoord.y); + vec2 offset = turned((point - center) * RENDERSIZE, -radians(rotation)) / zoom; + + // The angle folded into one segment, then into its first half. + float segment = TWO_PI / float(segments); + float angle = atan(offset.y, offset.x); + angle = abs(mod(angle, segment) - 0.5 * segment); + angle += radians(sample_rotation); + + vec2 read = center + length(offset) * vec2(cos(angle), sin(angle)) / RENDERSIZE; + read = foldedInside(read); + gl_FragColor = IMG_NORM_PIXEL(inputImage, vec2(read.x, 1.0 - read.y)); +} diff --git a/resources/shaders/mirror.fs b/resources/shaders/mirror.fs new file mode 100644 index 00000000..d9803db8 --- /dev/null +++ b/resources/shaders/mirror.fs @@ -0,0 +1,113 @@ +/*{ + "ISFVSN": "2", + "DESCRIPTION": "Keeps one half of the image and shows its mirror image in place of the other, left and right, top and bottom, or both.", + "CREDIT": "Based on Mirror by VIDVOX (ISF-Files, MIT License); MapMap contributors, GNU GPL version 3 or later. See CREDITS.md.", + "CATEGORIES": ["Geometry"], + "INPUTS": [ + { + "NAME": "inputImage", + "TYPE": "image" + }, + { + "NAME": "horizontal", + "TYPE": "bool", + "LABEL": "Horizontal", + "DEFAULT": true, + "DESCRIPTION": "Keeps the left half and shows its mirror image on the right, or the other way with From Right." + }, + { + "NAME": "from_right", + "TYPE": "bool", + "LABEL": "From Right", + "DEFAULT": false, + "DESCRIPTION": "Keeps the right half instead, and shows its mirror image on the left." + }, + { + "NAME": "vertical", + "TYPE": "bool", + "LABEL": "Vertical", + "DEFAULT": false, + "DESCRIPTION": "Keeps the top half and shows its mirror image at the bottom, or the other way with From Bottom." + }, + { + "NAME": "from_bottom", + "TYPE": "bool", + "LABEL": "From Bottom", + "DEFAULT": false, + "DESCRIPTION": "Keeps the bottom half instead, and shows its mirror image at the top." + }, + { + "NAME": "center", + "TYPE": "point2D", + "LABEL": "Center", + "MIN": [0.0, 0.0], + "MAX": [1.0, 1.0], + "DEFAULT": [0.5, 0.5], + "DESCRIPTION": "Where the mirrors cross, from the top left corner (0, 0) to the bottom right corner (1, 1)." + }, + { + "NAME": "angle", + "TYPE": "float", + "LABEL": "Options/Angle", + "MIN": -180.0, + "MAX": 180.0, + "DEFAULT": 0.0, + "STEP": 1.0, + "UNIT": "degrees", + "DESCRIPTION": "Turns the mirrors around their centre, clockwise." + } + ], + "GROUPS": [ + { "LABEL": "Options", "COLLAPSED": true } + ] +}*/ + +// An effect: MapMap gives it the image of a source or of a layer (see docs/extensions/isf.md). +// Written in GLSL 1.20, which MapMap compiles on every platform, the Raspberry Pi included. +// +// Based on Mirror by VIDVOX (ISF-Files, MIT License, see CREDITS.md), which folds the right half +// onto the left and the bottom half onto the top. The centre counted from the top left, the +// halves kept, the angle of the mirrors and the mirrored reading beyond the edges are by the +// MapMap contributors. On a layer, everything stays inside the part of the image the layer shows. + +// The distance from the edge at which the image is read, in pixels of the copy of the image the +// graphics card reads: one pixel, because it mixes that copy with the next smaller one, whose +// pixels are twice as large, and half of one of them must stay inside the edge. +const float EDGE_MARGIN = 1.0; + +// A point turned by an angle, clockwise when y goes down. +vec2 turned(vec2 point, float turn) { + float c = cos(turn); + float s = sin(turn); + return vec2(c * point.x - s * point.y, s * point.x + c * point.y); +} + +// A position from (0, 0) to (1, 1), folded back as in a mirror when it goes beyond, and kept +// from the edges by EDGE_MARGIN pixels of the copy of the image the graphics card reads there. +// Where the image is shrunk, it reads a smaller, smoothed copy, whose pixels are larger: by how +// many pixels of the image the position moves from one pixel drawn to the next, measured before +// the fold, which would hide it where two neighbouring pixels fall on either side of the mirror. +vec2 foldedInside(vec2 position) { + vec2 folded = 1.0 - abs(1.0 - mod(position, 2.0)); + vec2 pixels = position * RENDERSIZE; + float shrink = max(1.0, max(length(dFdx(pixels)), length(dFdy(pixels)))); + vec2 margin = EDGE_MARGIN * shrink / RENDERSIZE; + return clamp(folded, margin, 1.0 - margin); +} + +void main() { + // In pixels from the centre, counted from the top left, in the directions of the mirrors, so + // that turned mirrors do not stretch the image when it is not square. + vec2 point = vec2(isf_FragNormCoord.x, 1.0 - isf_FragNormCoord.y); + float turn = radians(angle); + vec2 local = turned((point - center) * RENDERSIZE, -turn); + + // The side kept: left or top, where the coordinates are negative, or the other. + if (horizontal) + local.x = (from_right ? 1.0 : -1.0) * abs(local.x); + if (vertical) + local.y = (from_bottom ? 1.0 : -1.0) * abs(local.y); + + vec2 read = foldedInside(center + turned(local, turn) / RENDERSIZE); + gl_FragColor = IMG_NORM_PIXEL(inputImage, vec2(read.x, 1.0 - read.y)); +} diff --git a/resources/shaders/pixelate.fs b/resources/shaders/pixelate.fs new file mode 100644 index 00000000..f086c8b7 --- /dev/null +++ b/resources/shaders/pixelate.fs @@ -0,0 +1,133 @@ +/*{ + "ISFVSN": "2", + "DESCRIPTION": "Turns the image into large blocks of one colour each.", + "CREDIT": "Based on Pixellate by VIDVOX (ISF-Files, MIT License); MapMap contributors, GNU GPL version 3 or later. See CREDITS.md.", + "CATEGORIES": ["Stylize"], + "INPUTS": [ + { + "NAME": "inputImage", + "TYPE": "image" + }, + { + "NAME": "columns", + "TYPE": "long", + "LABEL": "Columns", + "MIN": 1, + "MAX": 512, + "SLIDER_MAX": 128, + "DEFAULT": 32, + "DESCRIPTION": "The number of blocks across the width of the image: fewer, larger blocks." + }, + { + "NAME": "block_shape", + "TYPE": "long", + "LABEL": "Options/Shape", + "VALUES": [0, 1, 2], + "LABELS": ["Square", "Round", "Diamond"], + "DEFAULT": 0, + "DESCRIPTION": "The shape of each block; around a round block or a diamond, the gap colour shows." + }, + { + "NAME": "block_aspect", + "TYPE": "float", + "LABEL": "Options/Block Width", + "MIN": 0.1, + "MAX": 10.0, + "SLIDER_MIN": 0.25, + "SLIDER_MAX": 4.0, + "DEFAULT": 1.0, + "UNIT": "percent", + "DESCRIPTION": "The width of a block as a share of its height: 100 % for squares, more for wide blocks, less for tall ones." + }, + { + "NAME": "gap", + "TYPE": "float", + "LABEL": "Options/Gap", + "MIN": 0.0, + "MAX": 1.0, + "DEFAULT": 0.0, + "UNIT": "percent", + "DESCRIPTION": "The space between the blocks, as a share of a block." + }, + { + "NAME": "gap_color", + "TYPE": "color", + "LABEL": "Options/Gap Color", + "DEFAULT": [0.0, 0.0, 0.0, 1.0], + "DESCRIPTION": "The colour between the blocks; transparent lets what is behind show." + } + ], + "GROUPS": [ + { "LABEL": "Options", "COLLAPSED": true } + ] +}*/ + +// An effect: MapMap gives it the image of a source or of a layer (see docs/extensions/isf.md). +// Written in GLSL 1.20, which MapMap compiles on every platform, the Raspberry Pi included. +// +// Based on Pixellate by VIDVOX (ISF-Files, MIT License, see CREDITS.md), which gives each block +// one colour of the image. The count of blocks across the width, the single read at the centre +// of the block, the shapes, the gap and the vertical centring of the blocks are by the MapMap +// contributors. On a layer, everything stays inside the part of the image the layer shows. + +// Values of block_shape. +const int SHAPE_ROUND = 1; +const int SHAPE_DIAMOND = 2; + +// How far from a whole number of rows the rows that fit in the height may be, and still count as +// that whole number: the division that gives them is not exact. +const float ROW_TOLERANCE = 0.001; + +// The distance from the edge at which the image is read, in pixels of the copy of the image the +// graphics card reads: one pixel, because it mixes that copy with the next smaller one, whose +// pixels are twice as large, and half of one of them must stay inside the edge. +const float EDGE_MARGIN = 1.0; + +// The smallest change of the distance to the centre from one pixel drawn to the next, so that +// the smoothing of the edge never divides by zero. +const float MIN_CHANGE = 1e-6; + +// Where the edge of a shape crosses a pixel drawn, half of the pixel is covered. +const float HALF_COVERED = 0.5; + +// By how many pixels of the image a position moves from one pixel drawn to the next, at least +// one: where the image is shrunk, the graphics card reads a smaller, smoothed copy of it. +float shrinkOf(vec2 position) { + vec2 pixels = position * RENDERSIZE; + return max(1.0, max(length(dFdx(pixels)), length(dFdy(pixels)))); +} + +void main() { + // The position drawn, counted from the top left. + vec2 point = vec2(isf_FragNormCoord.x, 1.0 - isf_FragNormCoord.y); + + // The blocks, in pixels: as many as asked across the width; the rows that do not fit exactly + // in the height are cut equally at the top and at the bottom. + float blockWidth = RENDERSIZE.x / float(columns); + vec2 block = vec2(blockWidth, blockWidth / block_aspect); + float rows = RENDERSIZE.y / block.y; + vec2 shift = vec2(0.0, 0.5 * (ceil(rows - ROW_TOLERANCE) - rows)); + vec2 grid = point * RENDERSIZE / block + shift; + vec2 cell = floor(grid); + + // The colour of the block, read at its centre, kept inside the image. + vec2 center = (cell + 0.5 - shift) * block / RENDERSIZE; + vec2 margin = EDGE_MARGIN * shrinkOf(point) / RENDERSIZE; + center = clamp(center, margin, 1.0 - margin); + vec4 color = IMG_NORM_PIXEL_SEAMLESS(inputImage, vec2(center.x, 1.0 - center.y), isf_FragNormCoord); + + // How far the pixel is from the centre of its block, 1 on the edge of its shape when there is + // no gap; the edge of the shape is smoothed over one pixel drawn. + vec2 local = 2.0 * (grid - cell) - 1.0; + float distanceToCenter = max(abs(local.x), abs(local.y)); + if (block_shape == SHAPE_ROUND) + distanceToCenter = length(local); + else if (block_shape == SHAPE_DIAMOND) + distanceToCenter = abs(local.x) + abs(local.y); + float edge = 1.0 - gap; + float covered = clamp((edge - distanceToCenter) / max(fwidth(distanceToCenter), MIN_CHANGE) + HALF_COVERED, 0.0, 1.0); + // Square blocks without a gap touch each other: nothing shows between them. + if (block_shape != SHAPE_ROUND && block_shape != SHAPE_DIAMOND && gap <= 0.0) + covered = 1.0; + gl_FragColor = mix(gap_color, color, covered); +} diff --git a/resources/shaders/rgb-shift.fs b/resources/shaders/rgb-shift.fs new file mode 100644 index 00000000..d7e210fc --- /dev/null +++ b/resources/shaders/rgb-shift.fs @@ -0,0 +1,115 @@ +/*{ + "ISFVSN": "2", + "DESCRIPTION": "Moves the red, green and blue of the image apart, leaving coloured fringes along its edges.", + "CREDIT": "Based on Chromatic Aberration by VIDVOX (ISF-Files, MIT License); MapMap contributors, GNU GPL version 3 or later. See CREDITS.md.", + "CATEGORIES": ["Stylize"], + "INPUTS": [ + { + "NAME": "inputImage", + "TYPE": "image" + }, + { + "NAME": "distance", + "TYPE": "float", + "LABEL": "Distance", + "MIN": 0.0, + "MAX": 0.5, + "SLIDER_MAX": 0.05, + "DEFAULT": 0.01, + "UNIT": "percent", + "DESCRIPTION": "How far the colours move apart, as a share of the height of the image." + }, + { + "NAME": "angle", + "TYPE": "float", + "LABEL": "Angle", + "MIN": -180.0, + "MAX": 180.0, + "DEFAULT": 0.0, + "STEP": 1.0, + "UNIT": "degrees", + "DESCRIPTION": "The direction in which the colours move: 0° to the right, turning clockwise." + }, + { + "NAME": "radial", + "TYPE": "bool", + "LABEL": "Radial", + "DEFAULT": false, + "DESCRIPTION": "Moves the colours away from the centre instead, more towards the edges, as through a lens; the angle is then left aside." + }, + { + "NAME": "red", + "TYPE": "float", + "LABEL": "Channels/Red", + "MIN": -1.0, + "MAX": 1.0, + "DEFAULT": 1.0, + "UNIT": "percent", + "DESCRIPTION": "How far the red moves, as a share of the distance; below zero, the other way." + }, + { + "NAME": "green", + "TYPE": "float", + "LABEL": "Channels/Green", + "MIN": -1.0, + "MAX": 1.0, + "DEFAULT": 0.0, + "UNIT": "percent", + "DESCRIPTION": "How far the green moves, as a share of the distance; below zero, the other way." + }, + { + "NAME": "blue", + "TYPE": "float", + "LABEL": "Channels/Blue", + "MIN": -1.0, + "MAX": 1.0, + "DEFAULT": -1.0, + "UNIT": "percent", + "DESCRIPTION": "How far the blue moves, as a share of the distance; below zero, the other way." + } + ], + "GROUPS": [ + { "LABEL": "Channels", "COLLAPSED": true } + ] +}*/ + +// An effect: MapMap gives it the image of a source or of a layer (see docs/extensions/isf.md). +// Written in GLSL 1.20, which MapMap compiles on every platform, the Raspberry Pi included. +// +// Based on Chromatic Aberration by VIDVOX (ISF-Files, MIT License, see CREDITS.md), which reads +// the red, the green and the blue of the image at three places, moved apart from the centre. The +// move along an angle, the share of each channel and the choice of the radial move are by the +// MapMap contributors. On a layer, everything stays inside the part of the image the layer shows. + +// In the radial move, how many times the distance a colour moves at the middle of the top edge, +// half the height of the image away from the centre: once. +const float RADIAL_SCALE = 2.0; + +// The centre of the image, around which the radial move goes. +const vec2 CENTER = vec2(0.5, 0.5); + +// The colour of the image at a position counted from the top left; transparent beyond its +// edges, so that a layer reads nothing outside the part it shows. +vec4 colorAt(vec2 position) { + if (position.x < 0.0 || position.y < 0.0 || position.x > 1.0 || position.y > 1.0) + return vec4(0.0); + return IMG_NORM_PIXEL(inputImage, vec2(position.x, 1.0 - position.y)); +} + +void main() { + // The position drawn, counted from the top left. + vec2 point = vec2(isf_FragNormCoord.x, 1.0 - isf_FragNormCoord.y); + + // The move of a whole channel, as a position: along the angle, in pixels of the height so + // that it keeps its length whatever the proportions of the image, or away from the centre. + vec2 move = vec2(cos(radians(angle)), sin(radians(angle))) * distance * RENDERSIZE.y / RENDERSIZE; + if (radial) + move = (point - CENTER) * RADIAL_SCALE * distance; + + // A channel moved by a share of the move shows what was that far behind it. + vec4 redColor = colorAt(point - red * move); + vec4 greenColor = colorAt(point - green * move); + vec4 blueColor = colorAt(point - blue * move); + float alpha = max(redColor.a, max(greenColor.a, blueColor.a)); + gl_FragColor = vec4(redColor.r, greenColor.g, blueColor.b, alpha); +} diff --git a/resources/shaders/transform.fs b/resources/shaders/transform.fs new file mode 100644 index 00000000..c19c43a1 --- /dev/null +++ b/resources/shaders/transform.fs @@ -0,0 +1,212 @@ +/*{ + "ISFVSN": "2", + "DESCRIPTION": "Moves, scales and turns the image, and repeats it in tiles.", + "CREDIT": "Based on Zoom, Rotate and Quad Tile by VIDVOX (ISF-Files, MIT License); MapMap contributors, GNU GPL version 3 or later. See CREDITS.md.", + "CATEGORIES": ["Geometry"], + "INPUTS": [ + { + "NAME": "inputImage", + "TYPE": "image" + }, + { + "NAME": "position", + "TYPE": "point2D", + "LABEL": "Position", + "MIN": [0.0, 0.0], + "MAX": [1.0, 1.0], + "DEFAULT": [0.5, 0.5], + "DESCRIPTION": "Where the pivot of the image goes, from the top left corner (0, 0) to the bottom right corner (1, 1)." + }, + { + "NAME": "scale", + "TYPE": "float", + "LABEL": "Scale", + "MIN": 0.01, + "MAX": 10.0, + "SLIDER_MAX": 4.0, + "DEFAULT": 1.0, + "UNIT": "percent", + "DESCRIPTION": "The size of the image: 100 % as it is, smaller below, larger above." + }, + { + "NAME": "rotation", + "TYPE": "float", + "LABEL": "Rotation", + "MIN": -180.0, + "MAX": 180.0, + "DEFAULT": 0.0, + "STEP": 1.0, + "UNIT": "degrees", + "DESCRIPTION": "Turns the image around its pivot, clockwise." + }, + { + "NAME": "columns", + "TYPE": "long", + "LABEL": "Tiling/Columns", + "MIN": 1, + "MAX": 64, + "SLIDER_MAX": 16, + "DEFAULT": 1, + "DESCRIPTION": "The number of copies of the image side by side, each narrower." + }, + { + "NAME": "rows", + "TYPE": "long", + "LABEL": "Tiling/Rows", + "MIN": 1, + "MAX": 64, + "SLIDER_MAX": 16, + "DEFAULT": 1, + "DESCRIPTION": "The number of copies of the image one above the other, each lower." + }, + { + "NAME": "mirror_tiles", + "TYPE": "bool", + "LABEL": "Tiling/Mirror Tiles", + "DEFAULT": false, + "DESCRIPTION": "Shows every other tile in a mirror, so that the tiles meet without a seam." + }, + { + "NAME": "row_offset", + "TYPE": "float", + "LABEL": "Tiling/Row Offset", + "MIN": 0.0, + "MAX": 1.0, + "DEFAULT": 0.0, + "UNIT": "percent", + "DESCRIPTION": "Slides every other row sideways by this share of a tile, as bricks in a wall." + }, + { + "NAME": "skew", + "TYPE": "float", + "LABEL": "Tiling/Skew", + "MIN": -1.0, + "MAX": 1.0, + "DEFAULT": 0.0, + "UNIT": "percent", + "DESCRIPTION": "Slants the tiles: one tile lower, they are slid sideways by this share of a tile." + }, + { + "NAME": "pivot", + "TYPE": "point2D", + "LABEL": "Options/Pivot", + "MIN": [0.0, 0.0], + "MAX": [1.0, 1.0], + "DEFAULT": [0.5, 0.5], + "DESCRIPTION": "The point of the image that stays at its position and around which it turns and scales, from its top left corner (0, 0) to its bottom right corner (1, 1)." + }, + { + "NAME": "stretch_x", + "TYPE": "float", + "LABEL": "Options/Width", + "MIN": 0.01, + "MAX": 10.0, + "SLIDER_MAX": 4.0, + "DEFAULT": 1.0, + "UNIT": "percent", + "DESCRIPTION": "Stretches the image across, after the scale: 100 % keeps its proportions." + }, + { + "NAME": "stretch_y", + "TYPE": "float", + "LABEL": "Options/Height", + "MIN": 0.01, + "MAX": 10.0, + "SLIDER_MAX": 4.0, + "DEFAULT": 1.0, + "UNIT": "percent", + "DESCRIPTION": "Stretches the image up and down, after the scale: 100 % keeps its proportions." + }, + { + "NAME": "outside", + "TYPE": "long", + "LABEL": "Options/Outside", + "VALUES": [0, 1, 2, 3], + "LABELS": ["Nothing", "Edge", "Repeat", "Mirror"], + "DEFAULT": 0, + "DESCRIPTION": "What is seen beyond the edges of the image: nothing, which lets what is behind show, its edge drawn out, the image repeated, or repeated in a mirror." + } + ], + "GROUPS": [ + { "LABEL": "Tiling", "COLLAPSED": true }, + { "LABEL": "Options", "COLLAPSED": true } + ] +}*/ + +// An effect: MapMap gives it the image of a source or of a layer (see docs/extensions/isf.md). +// Written in GLSL 1.20, which MapMap compiles on every platform, the Raspberry Pi included. +// +// Based on Zoom and Rotate by VIDVOX, which scale and turn the image around a point, and on +// Quad Tile by VIDVOX, which repeats it in tiles, every other one in a mirror (ISF-Files, MIT +// License, see CREDITS.md). The order of the steps, the pivot, the stretch, the row offset, the +// skew and the choice of what is seen beyond the edges are by the MapMap contributors. On a +// layer, everything stays inside the part of the image the layer shows. + +// Values of outside. +const int OUTSIDE_EDGE = 1; +const int OUTSIDE_REPEAT = 2; +const int OUTSIDE_MIRROR = 3; + +// The distance from the edge at which the image is read, in pixels of the copy of the image the +// graphics card reads: one pixel, because it mixes that copy with the next smaller one, whose +// pixels are twice as large, and half of one of them must stay inside the edge. +const float EDGE_MARGIN = 1.0; + +// A point turned by an angle, clockwise when y goes down. +vec2 turned(vec2 point, float turn) { + float c = cos(turn); + float s = sin(turn); + return vec2(c * point.x - s * point.y, s * point.x + c * point.y); +} + +// By how many pixels of the image a position moves from one pixel drawn to the next, at least +// one: where the image is shrunk, the graphics card reads a smaller, smoothed copy of it. +float shrinkOf(vec2 position) { + vec2 pixels = position * RENDERSIZE; + return max(1.0, max(length(dFdx(pixels)), length(dFdy(pixels)))); +} + +void main() { + // From the position drawn, counted from the top left, back to the position in the tiled + // image: the steps undone in the reverse order, in pixels so that a turn does not stretch an + // image that is not square. + vec2 point = vec2(isf_FragNormCoord.x, 1.0 - isf_FragNormCoord.y); + vec2 offset = turned((point - position) * RENDERSIZE, -radians(rotation)); + vec2 unfolded = pivot + offset / (scale * vec2(stretch_x, stretch_y)) / RENDERSIZE; + + // Beyond the edges of the tiled image. + vec2 tiled = unfolded; + bool inside = true; + if (outside == OUTSIDE_EDGE) + tiled = clamp(tiled, 0.0, 1.0); + else if (outside == OUTSIDE_REPEAT) + tiled = fract(tiled); + else if (outside == OUTSIDE_MIRROR) + tiled = 1.0 - abs(1.0 - mod(tiled, 2.0)); + else + inside = tiled.x >= 0.0 && tiled.y >= 0.0 && tiled.x <= 1.0 && tiled.y <= 1.0; + + // The tiles, counted from the top left: slanted by the skew, every other row slid, every + // other tile in a mirror. + vec2 tileCount = vec2(float(columns), float(rows)); + vec2 grid = tiled * tileCount; + float row = floor(grid.y); + grid.x += skew * grid.y + row_offset * mod(row, 2.0); + vec2 cell = floor(grid); + vec2 inTile = grid - cell; + if (mirror_tiles) + inTile = mix(inTile, 1.0 - inTile, mod(cell, 2.0)); + + // The same position in the tiles, without the jumps from one tile to the next or beyond the + // edges: it says how large the pixels drawn are on the image, so that the graphics card reads + // the copy of the image they call for even where the position jumps, without a seam. + vec2 continuous = unfolded * tileCount; + continuous.x += skew * continuous.y; + + // Kept from the edges of the image by the copy the graphics card reads there. Read on every + // pixel, outside included, so that each pixel can compare its position with its neighbours'. + vec2 margin = EDGE_MARGIN * shrinkOf(continuous) / RENDERSIZE; + vec2 read = clamp(inTile, margin, 1.0 - margin); + vec4 color = IMG_NORM_PIXEL_SEAMLESS(inputImage, vec2(read.x, 1.0 - read.y), vec2(continuous.x, -continuous.y)); + gl_FragColor = inside ? color : vec4(0.0); +} diff --git a/scripts/extract-shader-strings.py b/scripts/extract-shader-strings.py index f7e90a88..30911834 100755 --- a/scripts/extract-shader-strings.py +++ b/scripts/extract-shader-strings.py @@ -19,13 +19,16 @@ ROOT = Path(__file__).resolve().parent.parent CONTEXT = "Shaders" +# A copyright line of the header, as scripts/update-copyright.py writes it. +COPYRIGHT_LINE = re.compile(r"^ \* \(c\) .*$", re.MULTILINE) + HEADER = """\ /* * ShaderStrings.cpp * * Written by scripts/extract-shader-strings.py from resources/shaders: do not edit. * - * This program is free software: you can redistribute it and/or modify +{copyright} * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. @@ -49,10 +52,21 @@ """ +# Words written in capitals in the name of a shader, as ACRONYMS in IsfShader.cpp. +ACRONYMS = {"cmyk", "hsl", "hsv", "rgb", "rgba", "yuv"} + + +def _word_of_name(word): + """Return a word of a file name as it is written in the name of its shader.""" + if word in ACRONYMS: + return word.upper() + return word[0].upper() + word[1:] if word == word.lower() else word + + def name_from_file_name(file_name): """Return the name of a generator from its file name, as IsfShader::nameFromFileName().""" words = [word for word in re.split(r"[-_ ]+", Path(file_name).stem) if word] - return " ".join(word[0].upper() + word[1:] if word == word.lower() else word for word in words) + return " ".join(_word_of_name(word) for word in words) def header_of(path): @@ -87,6 +101,15 @@ def texts_of(path): return texts +def copyright_of(path): + """Return the copyright lines of the header of a file written before, each followed by a + newline, then an empty comment line; nothing when it has none.""" + if not path.exists(): + return "" + lines = COPYRIGHT_LINE.findall(path.read_text(encoding="utf-8")) + return "".join(line + "\n" for line in lines) + " *\n" if lines else "" + + def c_string(text): """Return a text as a C++ string literal.""" return json.dumps(text, ensure_ascii=False) @@ -107,7 +130,9 @@ def main(): if text not in texts: texts.append(text) entries = ",\n".join(f' QT_TRANSLATE_NOOP("{CONTEXT}", {c_string(text)})' for text in texts) - args.output.write_text(HEADER.format(entries=entries), encoding="utf-8") + # The copyright lines belong to scripts/update-copyright.py: they are kept as they are. + copyright_lines = copyright_of(args.output) + args.output.write_text(HEADER.format(copyright=copyright_lines, entries=entries), encoding="utf-8") print(f"{len(texts)} texts written to {args.output}", file=sys.stderr) diff --git a/src/core/EffectRenderer.cpp b/src/core/EffectRenderer.cpp index 5c4a295f..18d46239 100644 --- a/src/core/EffectRenderer.cpp +++ b/src/core/EffectRenderer.cpp @@ -33,10 +33,21 @@ #include #include +// From GL_EXT_texture_filter_anisotropic, which the headers of some systems lack. +#ifndef GL_TEXTURE_MAX_ANISOTROPY_EXT +#define GL_TEXTURE_MAX_ANISOTROPY_EXT 0x84FE +#endif + namespace mmp { namespace { +/// The anisotropy of an image read without anisotropic filtering. +const GLfloat NO_ANISOTROPY = 1.0f; + +/// The name of the seamless read in the code of an effect. +const char* const SEAMLESS_READ = "IMG_NORM_PIXEL_SEAMLESS"; + /// Smallest side of the part of a layer, in texture coordinates: an input shape whose /// vertices are on a line still gives finite positions. const qreal MIN_PART_SIDE = 1e-6; @@ -76,9 +87,12 @@ const char* const COMMON_HEADER = "uniform float mm_mix;\n" "vec2 isf_FragNormCoord;\n"; +// mm_sample(tc, bias) reads a smaller, smoother copy of the image (a level of its mipmap) when +// bias is above 0, a larger, sharper one below, than the graphics card would choose. const char* const RGBA_SAMPLER = "uniform sampler2D mm_tex;\n" - "vec4 mm_sample(vec2 tc) { return texture2D(mm_tex, tc); }\n"; + "vec4 mm_sample(vec2 tc) { return texture2D(mm_tex, tc); }\n" + "vec4 mm_sample(vec2 tc, float bias) { return texture2D(mm_tex, tc, bias); }\n"; // As the YUV shader of ShapeRenderer: one plane per unit, or interleaved chroma. const char* const YUV_SAMPLER = @@ -87,27 +101,53 @@ const char* const YUV_SAMPLER = "uniform sampler2D mm_texV;\n" "uniform int mm_chromaLayout;\n" "uniform mat4 mm_colorMatrix;\n" - "vec4 mm_sample(vec2 tc) {\n" - " float y = texture2D(mm_texY, tc).r;\n" + "vec4 mm_sample(vec2 tc, float bias) {\n" + " float y = texture2D(mm_texY, tc, bias).r;\n" " vec2 uv;\n" " if (mm_chromaLayout == 0) {\n" - " uv = vec2(texture2D(mm_texU, tc).r, texture2D(mm_texV, tc).r);\n" + " uv = vec2(texture2D(mm_texU, tc, bias).r, texture2D(mm_texV, tc, bias).r);\n" " } else {\n" - " vec4 c = texture2D(mm_texU, tc);\n" + " vec4 c = texture2D(mm_texU, tc, bias);\n" " uv = (mm_chromaLayout == 1) ? c.ra : c.ar;\n" " }\n" " return vec4(clamp((mm_colorMatrix * vec4(y, uv, 1.0)).rgb, 0.0, 1.0), 1.0);\n" - "}\n"; + "}\n" + "vec4 mm_sample(vec2 tc) { return mm_sample(tc, 0.0); }\n"; // Transparent outside the source, whatever it is: the planes of a video repeat their border. +// +// mm_readSeamless() reads at normCoord, but chooses the copy of the image as if reading at +// smoothCoord, the same position without its jumps. Where an effect jumps from one place of +// the image to another between two neighbouring pixels, as between two tiles, the graphics card +// takes the jump for a very shrunk image and reads its smallest copy: a grey seam. The bias +// gives back the copy that the size of the pixels on smoothCoord calls for. It only ever makes +// the copy sharper. const char* const READ_FUNCTIONS = + "vec2 mm_texCoord(vec2 normCoord) {\n" + " return mm_rect.xy + vec2(normCoord.x, 1.0 - normCoord.y) * mm_rect.zw;\n" + "}\n" + "bool mm_outside(vec2 tc) {\n" + " return tc.x < 0.0 || tc.y < 0.0 || tc.x > 1.0 || tc.y > 1.0;\n" + "}\n" "vec4 mm_read(vec2 normCoord) {\n" - " vec2 tc = mm_rect.xy + vec2(normCoord.x, 1.0 - normCoord.y) * mm_rect.zw;\n" - " if (tc.x < 0.0 || tc.y < 0.0 || tc.x > 1.0 || tc.y > 1.0) return vec4(0.0);\n" + " vec2 tc = mm_texCoord(normCoord);\n" + " if (mm_outside(tc)) return vec4(0.0);\n" " if (mm_bottomUp) tc.y = 1.0 - tc.y;\n" " return mm_sample(tc);\n" "}\n" + "float mm_pixelSize(vec2 normCoord) {\n" + " vec2 pixels = normCoord * RENDERSIZE;\n" + " return max(max(length(dFdx(pixels)), length(dFdy(pixels))), 1e-6);\n" + "}\n" + "vec4 mm_readSeamless(vec2 normCoord, vec2 smoothCoord) {\n" + " float bias = min(0.0, log2(mm_pixelSize(smoothCoord) / mm_pixelSize(normCoord)));\n" + " vec2 tc = mm_texCoord(normCoord);\n" + " if (mm_outside(tc)) return vec4(0.0);\n" + " if (mm_bottomUp) tc.y = 1.0 - tc.y;\n" + " return mm_sample(tc, bias);\n" + "}\n" "#define IMG_NORM_PIXEL(image, normCoord) mm_read(normCoord)\n" + "#define IMG_NORM_PIXEL_SEAMLESS(image, normCoord, smoothCoord) mm_readSeamless(normCoord, smoothCoord)\n" "#define IMG_PIXEL(image, pixelCoord) mm_read((pixelCoord) / RENDERSIZE)\n" "#define IMG_THIS_NORM_PIXEL(image) mm_read(isf_FragNormCoord)\n" "#define IMG_THIS_PIXEL(image) mm_read(isf_FragNormCoord)\n" @@ -269,6 +309,40 @@ QRectF EffectRenderer::layerPart(const QRectF& inputBounds, const QRectF& textur qMax(MIN_PART_SIDE, inputBounds.height() / textureRect.height())); } +bool EffectRenderer::readsSeamless(const IsfShader& shader) +{ + return shader.code().contains(QLatin1String(SEAMLESS_READ)); +} + +EffectRenderer::SeamlessReading::SeamlessReading(const Effect* effect, const YuvTexture* yuv) + : _anisotropy(0) +{ + QOpenGLContext* context = QOpenGLContext::currentContext(); + if (yuv || !effect || !effect->shader() || !context || !readsSeamless(*effect->shader())) + return; + if (!context->hasExtension(QByteArrayLiteral("GL_EXT_texture_filter_anisotropic")) && + !context->hasExtension(QByteArrayLiteral("GL_ARB_texture_filter_anisotropic"))) + return; + QOpenGLFunctions* gl = context->functions(); + gl->glActiveTexture(GL_TEXTURE0 + RGBA_UNIT); + GLfloat anisotropy = NO_ANISOTROPY; + gl->glGetTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, &anisotropy); + if (anisotropy <= NO_ANISOTROPY) + return; + _anisotropy = anisotropy; + gl->glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, NO_ANISOTROPY); +} + +EffectRenderer::SeamlessReading::~SeamlessReading() +{ + QOpenGLContext* context = QOpenGLContext::currentContext(); + if (_anisotropy <= 0 || !context) + return; + QOpenGLFunctions* gl = context->functions(); + gl->glActiveTexture(GL_TEXTURE0 + RGBA_UNIT); + gl->glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, _anisotropy); +} + SourceEffectImage::SourceEffectImage() : _renderCount(0) { @@ -301,6 +375,7 @@ bool SourceEffectImage::render(Texture& source, const Effect& effect) } if (program) { + const EffectRenderer::SeamlessReading seamless(&effect, yuv); program->bind(); EffectRenderer::setUniforms(program, effect, yuv, source.isBottomUp(), QRectF(0, 0, 1, 1), QSizeF(size), 1.0f); resources->fullTarget->bind(); diff --git a/src/core/EffectRenderer.h b/src/core/EffectRenderer.h index e5edc0a8..495f6327 100644 --- a/src/core/EffectRenderer.h +++ b/src/core/EffectRenderer.h @@ -43,7 +43,9 @@ class YuvTexture; * The text of an effect is assembled with a preamble that gives it what ISF promises: the * functions that read the image (IMG_NORM_PIXEL, IMG_PIXEL, IMG_THIS_PIXEL, * IMG_THIS_NORM_PIXEL, IMG_SIZE), its position isf_FragNormCoord, from (0, 0) at the bottom - * left to (1, 1) at the top right, and RENDERSIZE. Two places: + * left to (1, 1) at the top right, and RENDERSIZE. It adds IMG_NORM_PIXEL_SEAMLESS, for the + * effects whose position jumps from one place of the image to another between two + * neighbouring pixels (see docs/extensions/isf.md). Two places: * * - on a source, the effect is drawn over the whole image of the source, in an image of the * same size (see SourceEffectImage); @@ -116,6 +118,32 @@ void setUniforms(QOpenGLShaderProgram* program, const Effect& effect, const YuvT */ QRectF layerPart(const QRectF& inputBounds, const QRectF& textureRect); +/// Returns true when the code of a shader reads with IMG_NORM_PIXEL_SEAMLESS. +bool readsSeamless(const IsfShader& shader); + +/** + * Turns off, for its life, the anisotropic filtering of the image bound to RGBA_UNIT, when an + * effect reads it with IMG_NORM_PIXEL_SEAMLESS, then gives it back. That filtering reads + * several points spread along the jump of the position between two neighbouring pixels, which + * the bias of the seamless read cannot shorten: a seam would remain. The images that have it + * are those with smaller copies (see IntermediateImage::bind()); the planes of a video have + * none. + */ +class SeamlessReading +{ +public: + /// In the current context, with the image bound; effect may be null. + SeamlessReading(const Effect* effect, const YuvTexture* yuv); + ~SeamlessReading(); + + SeamlessReading(const SeamlessReading&) = delete; + SeamlessReading& operator=(const SeamlessReading&) = delete; + +private: + /// The anisotropy given back; 0 when nothing was changed. + GLfloat _anisotropy; +}; + } // namespace EffectRenderer /** diff --git a/src/core/IsfShader.cpp b/src/core/IsfShader.cpp index 058497c2..6a7a74b2 100644 --- a/src/core/IsfShader.cpp +++ b/src/core/IsfShader.cpp @@ -39,6 +39,10 @@ const char* const IsfShader::RESERVED_PREFIX = "mm_"; namespace { +/// Words of the name of a file written in capitals in the name of its shader, in lowercase: +/// "rgb-shift.fs" gives "RGB Shift". +const QStringList ACRONYMS = { "cmyk", "hsl", "hsv", "rgb", "rgba", "yuv" }; + /// Factor between a value from 0 to 1 and the percentage shown. const double PERCENT_SCALE = 100.0; @@ -181,7 +185,9 @@ QString IsfShader::nameFromFileName(const QString& fileName) static const QRegularExpression separators(QStringLiteral("[-_ ]+")); QStringList words = QFileInfo(fileName).completeBaseName().split(separators, Qt::SkipEmptyParts); for (QString& word : words) - if (word == word.toLower()) + if (ACRONYMS.contains(word)) + word = word.toUpper(); + else if (word == word.toLower()) word[0] = word.at(0).toUpper(); return words.join(QLatin1Char(' ')); } diff --git a/src/core/IsfShader.h b/src/core/IsfShader.h index 1451783b..f99d4a3c 100644 --- a/src/core/IsfShader.h +++ b/src/core/IsfShader.h @@ -151,7 +151,8 @@ class IsfShader static IsfShader fromFile(const QString& path, const QString& id); /// Returns the name of a kind of generator from the name of its file: "color-wheel.fs" - /// gives "Color Wheel". + /// gives "Color Wheel", and a few names of colour models are written in capitals: + /// "rgb-shift.fs" gives "RGB Shift". static QString nameFromFileName(const QString& fileName); /// Returns the text translated in the context of the shipped shaders. diff --git a/src/core/ShaderStrings.cpp b/src/core/ShaderStrings.cpp index 1c48faec..8369382b 100644 --- a/src/core/ShaderStrings.cpp +++ b/src/core/ShaderStrings.cpp @@ -110,6 +110,24 @@ QStringList ShaderLibrary::shippedTexts() QT_TRANSLATE_NOOP("Shaders", "Luminance"), QT_TRANSLATE_NOOP("Shaders", "Maximum"), QT_TRANSLATE_NOOP("Shaders", "Average"), + QT_TRANSLATE_NOOP("Shaders", "Edges"), + QT_TRANSLATE_NOOP("Shaders", "Draws the outlines of the image: lines where its brightness changes."), + QT_TRANSLATE_NOOP("Shaders", "Stylize"), + QT_TRANSLATE_NOOP("Shaders", "Strength"), + QT_TRANSLATE_NOOP("Shaders", "How bright the lines are: above 100 %, faint outlines show too."), + QT_TRANSLATE_NOOP("Shaders", "Thickness"), + QT_TRANSLATE_NOOP("Shaders", "The thickness of the lines, as a share of the height of the image; never thinner than a pixel."), + QT_TRANSLATE_NOOP("Shaders", "Threshold"), + QT_TRANSLATE_NOOP("Shaders", "The changes of brightness below this one draw no line, which hides the grain of a video."), + QT_TRANSLATE_NOOP("Shaders", "Line From"), + QT_TRANSLATE_NOOP("Shaders", "The colour of the lines: the line colour, or the colour of the image under them."), + QT_TRANSLATE_NOOP("Shaders", "Image"), + QT_TRANSLATE_NOOP("Shaders", "Line Color"), + QT_TRANSLATE_NOOP("Shaders", "The colour of the lines, when they take it from the line colour."), + QT_TRANSLATE_NOOP("Shaders", "Background"), + QT_TRANSLATE_NOOP("Shaders", "What is seen between the lines: black, the image, or nothing, which lets what is behind show."), + QT_TRANSLATE_NOOP("Shaders", "Black"), + QT_TRANSLATE_NOOP("Shaders", "Transparent"), QT_TRANSLATE_NOOP("Shaders", "Gradient"), QT_TRANSLATE_NOOP("Shaders", "A gradient of two or three colours, linear, radial, conic or in a diamond, repeated, mirrored and moving along its shape."), QT_TRANSLATE_NOOP("Shaders", "Shape"), @@ -150,20 +168,27 @@ QStringList ShaderLibrary::shippedTexts() QT_TRANSLATE_NOOP("Shaders", "The number of columns between the lines."), QT_TRANSLATE_NOOP("Shaders", "Rows"), QT_TRANSLATE_NOOP("Shaders", "The number of rows between the lines."), - QT_TRANSLATE_NOOP("Shaders", "Thickness"), QT_TRANSLATE_NOOP("Shaders", "The thickness of the lines."), QT_TRANSLATE_NOOP("Shaders", "The colour between the lines."), - QT_TRANSLATE_NOOP("Shaders", "Line Color"), QT_TRANSLATE_NOOP("Shaders", "The colour of the lines."), QT_TRANSLATE_NOOP("Shaders", "Invert"), QT_TRANSLATE_NOOP("Shaders", "Inverts the colours of the image, as a photographic negative."), QT_TRANSLATE_NOOP("Shaders", "Keep Colors"), QT_TRANSLATE_NOOP("Shaders", "Inverts the light and the dark but keeps the colours: red stays red, lighter or darker."), - QT_TRANSLATE_NOOP("Shaders", "Threshold"), QT_TRANSLATE_NOOP("Shaders", "Only what is at least this light is inverted, as a solarized photograph."), QT_TRANSLATE_NOOP("Shaders", "Inverts the red, unless the colours are kept."), QT_TRANSLATE_NOOP("Shaders", "Inverts the green, unless the colours are kept."), QT_TRANSLATE_NOOP("Shaders", "Inverts the blue, unless the colours are kept."), + QT_TRANSLATE_NOOP("Shaders", "Kaleidoscope"), + QT_TRANSLATE_NOOP("Shaders", "Repeats a slice of the image around a centre, each copy beside its mirror image, as in a kaleidoscope."), + QT_TRANSLATE_NOOP("Shaders", "The number of copies around the centre. Each is a slice of the image beside its mirror image, so that they meet without a seam."), + QT_TRANSLATE_NOOP("Shaders", "The centre of the kaleidoscope, from the top left corner (0, 0) to the bottom right corner (1, 1)."), + QT_TRANSLATE_NOOP("Shaders", "Slice Angle"), + QT_TRANSLATE_NOOP("Shaders", "Turns the slice taken from the image around the centre, clockwise, to show another part of it."), + QT_TRANSLATE_NOOP("Shaders", "Rotation"), + QT_TRANSLATE_NOOP("Shaders", "Turns the whole kaleidoscope around its centre, clockwise."), + QT_TRANSLATE_NOOP("Shaders", "Zoom"), + QT_TRANSLATE_NOOP("Shaders", "Enlarges the slices above 100 %, or makes them smaller below. Beyond the edges, the image is read again in a mirror."), QT_TRANSLATE_NOOP("Shaders", "Luma Key"), QT_TRANSLATE_NOOP("Shaders", "Makes the dark parts of the image transparent, or the light ones, to show what is behind."), QT_TRANSLATE_NOOP("Shaders", "Keying"), @@ -174,6 +199,17 @@ QStringList ShaderLibrary::shippedTexts() QT_TRANSLATE_NOOP("Shaders", "Removes the light parts instead of the dark ones."), QT_TRANSLATE_NOOP("Shaders", "Channel"), QT_TRANSLATE_NOOP("Shaders", "What is compared with the thresholds: the brightness seen by the eye, one channel, or the strongest of the three."), + QT_TRANSLATE_NOOP("Shaders", "Keeps one half of the image and shows its mirror image in place of the other, left and right, top and bottom, or both."), + QT_TRANSLATE_NOOP("Shaders", "Horizontal"), + QT_TRANSLATE_NOOP("Shaders", "Keeps the left half and shows its mirror image on the right, or the other way with From Right."), + QT_TRANSLATE_NOOP("Shaders", "From Right"), + QT_TRANSLATE_NOOP("Shaders", "Keeps the right half instead, and shows its mirror image on the left."), + QT_TRANSLATE_NOOP("Shaders", "Vertical"), + QT_TRANSLATE_NOOP("Shaders", "Keeps the top half and shows its mirror image at the bottom, or the other way with From Bottom."), + QT_TRANSLATE_NOOP("Shaders", "From Bottom"), + QT_TRANSLATE_NOOP("Shaders", "Keeps the bottom half instead, and shows its mirror image at the top."), + QT_TRANSLATE_NOOP("Shaders", "Where the mirrors cross, from the top left corner (0, 0) to the bottom right corner (1, 1)."), + QT_TRANSLATE_NOOP("Shaders", "Turns the mirrors around their centre, clockwise."), QT_TRANSLATE_NOOP("Shaders", "Noise"), QT_TRANSLATE_NOOP("Shaders", "Noise that moves slowly: smooth clouds, cells or a plasma, between two colours."), QT_TRANSLATE_NOOP("Shaders", "Kind"), @@ -191,6 +227,18 @@ QStringList ShaderLibrary::shippedTexts() QT_TRANSLATE_NOOP("Shaders", "The colour of the high values."), QT_TRANSLATE_NOOP("Shaders", "How many times a minute the noise goes through its motion and comes back to where it started: at 6, every 10 seconds; 0 keeps it still."), QT_TRANSLATE_NOOP("Shaders", "The noise goes through its motion the other way."), + QT_TRANSLATE_NOOP("Shaders", "Pixelate"), + QT_TRANSLATE_NOOP("Shaders", "Turns the image into large blocks of one colour each."), + QT_TRANSLATE_NOOP("Shaders", "The number of blocks across the width of the image: fewer, larger blocks."), + QT_TRANSLATE_NOOP("Shaders", "The shape of each block; around a round block or a diamond, the gap colour shows."), + QT_TRANSLATE_NOOP("Shaders", "Square"), + QT_TRANSLATE_NOOP("Shaders", "Round"), + QT_TRANSLATE_NOOP("Shaders", "Block Width"), + QT_TRANSLATE_NOOP("Shaders", "The width of a block as a share of its height: 100 % for squares, more for wide blocks, less for tall ones."), + QT_TRANSLATE_NOOP("Shaders", "Gap"), + QT_TRANSLATE_NOOP("Shaders", "The space between the blocks, as a share of a block."), + QT_TRANSLATE_NOOP("Shaders", "Gap Color"), + QT_TRANSLATE_NOOP("Shaders", "The colour between the blocks; transparent lets what is behind show."), QT_TRANSLATE_NOOP("Shaders", "Posterize"), QT_TRANSLATE_NOOP("Shaders", "Reduces the image to a few levels of each colour, in flat bands, as a poster."), QT_TRANSLATE_NOOP("Shaders", "The number of levels of each colour, black and full included."), @@ -200,6 +248,15 @@ QStringList ShaderLibrary::shippedTexts() QT_TRANSLATE_NOOP("Shaders", "Mixes the pixels of two neighbouring levels in a fine pattern, to suggest the shades between."), QT_TRANSLATE_NOOP("Shaders", "Brightness Only"), QT_TRANSLATE_NOOP("Shaders", "Keeps the colours and reduces only their brightness to levels."), + QT_TRANSLATE_NOOP("Shaders", "RGB Shift"), + QT_TRANSLATE_NOOP("Shaders", "Moves the red, green and blue of the image apart, leaving coloured fringes along its edges."), + QT_TRANSLATE_NOOP("Shaders", "Distance"), + QT_TRANSLATE_NOOP("Shaders", "How far the colours move apart, as a share of the height of the image."), + QT_TRANSLATE_NOOP("Shaders", "The direction in which the colours move: 0° to the right, turning clockwise."), + QT_TRANSLATE_NOOP("Shaders", "Moves the colours away from the centre instead, more towards the edges, as through a lens; the angle is then left aside."), + QT_TRANSLATE_NOOP("Shaders", "How far the red moves, as a share of the distance; below zero, the other way."), + QT_TRANSLATE_NOOP("Shaders", "How far the green moves, as a share of the distance; below zero, the other way."), + QT_TRANSLATE_NOOP("Shaders", "How far the blue moves, as a share of the distance; below zero, the other way."), QT_TRANSLATE_NOOP("Shaders", "Stripes"), QT_TRANSLATE_NOOP("Shaders", "Stripes of two colours, at any angle and width, going by."), QT_TRANSLATE_NOOP("Shaders", "How many pairs of stripes, one of each colour, cross the image."), @@ -218,11 +275,35 @@ QStringList ShaderLibrary::shippedTexts() QT_TRANSLATE_NOOP("Shaders", "Colors"), QT_TRANSLATE_NOOP("Shaders", "The colour of what is below the threshold."), QT_TRANSLATE_NOOP("Shaders", "The colour of what is above the threshold."), - QT_TRANSLATE_NOOP("Shaders", "Transparent"), QT_TRANSLATE_NOOP("Shaders", "Which side lets what is behind show through instead of its colour."), QT_TRANSLATE_NOOP("Shaders", "None"), QT_TRANSLATE_NOOP("Shaders", "Dark"), QT_TRANSLATE_NOOP("Shaders", "Light"), + QT_TRANSLATE_NOOP("Shaders", "Transform"), + QT_TRANSLATE_NOOP("Shaders", "Moves, scales and turns the image, and repeats it in tiles."), + QT_TRANSLATE_NOOP("Shaders", "Position"), + QT_TRANSLATE_NOOP("Shaders", "Where the pivot of the image goes, from the top left corner (0, 0) to the bottom right corner (1, 1)."), + QT_TRANSLATE_NOOP("Shaders", "The size of the image: 100 % as it is, smaller below, larger above."), + QT_TRANSLATE_NOOP("Shaders", "Turns the image around its pivot, clockwise."), + QT_TRANSLATE_NOOP("Shaders", "Tiling"), + QT_TRANSLATE_NOOP("Shaders", "The number of copies of the image side by side, each narrower."), + QT_TRANSLATE_NOOP("Shaders", "The number of copies of the image one above the other, each lower."), + QT_TRANSLATE_NOOP("Shaders", "Mirror Tiles"), + QT_TRANSLATE_NOOP("Shaders", "Shows every other tile in a mirror, so that the tiles meet without a seam."), + QT_TRANSLATE_NOOP("Shaders", "Row Offset"), + QT_TRANSLATE_NOOP("Shaders", "Slides every other row sideways by this share of a tile, as bricks in a wall."), + QT_TRANSLATE_NOOP("Shaders", "Skew"), + QT_TRANSLATE_NOOP("Shaders", "Slants the tiles: one tile lower, they are slid sideways by this share of a tile."), + QT_TRANSLATE_NOOP("Shaders", "Pivot"), + QT_TRANSLATE_NOOP("Shaders", "The point of the image that stays at its position and around which it turns and scales, from its top left corner (0, 0) to its bottom right corner (1, 1)."), + QT_TRANSLATE_NOOP("Shaders", "Stretches the image across, after the scale: 100 % keeps its proportions."), + QT_TRANSLATE_NOOP("Shaders", "Height"), + QT_TRANSLATE_NOOP("Shaders", "Stretches the image up and down, after the scale: 100 % keeps its proportions."), + QT_TRANSLATE_NOOP("Shaders", "Outside"), + QT_TRANSLATE_NOOP("Shaders", "What is seen beyond the edges of the image: nothing, which lets what is behind show, its edge drawn out, the image repeated, or repeated in a mirror."), + QT_TRANSLATE_NOOP("Shaders", "Nothing"), + QT_TRANSLATE_NOOP("Shaders", "Edge"), + QT_TRANSLATE_NOOP("Shaders", "Repeat"), QT_TRANSLATE_NOOP("Shaders", "Waves"), QT_TRANSLATE_NOOP("Shaders", "Waves of two colours, in straight lines or in rings around a centre, from soft to sharp, going forward."), QT_TRANSLATE_NOOP("Shaders", "Lines are straight waves across the image; rings go out from the centre."), diff --git a/src/core/ShapeRenderer.cpp b/src/core/ShapeRenderer.cpp index d72e3a45..eda54e73 100644 --- a/src/core/ShapeRenderer.cpp +++ b/src/core/ShapeRenderer.cpp @@ -255,7 +255,10 @@ bool ShapeRenderer::drawTriangles(const QVector& vertices, const program->setAttributeBuffer(TEX_COORD_LOCATION, GL_FLOAT, offsetof(TexturedVertex, s), TEX_COORD_SIZE, sizeof(TexturedVertex)); - gl->glDrawArrays(GL_TRIANGLES, 0, GLsizei(vertices.size())); + { + const EffectRenderer::SeamlessReading seamless(withEffect ? effect->effect : nullptr, yuv); + gl->glDrawArrays(GL_TRIANGLES, 0, GLsizei(vertices.size())); + } program->disableAttributeArray(TEX_COORD_LOCATION); program->disableAttributeArray(POSITION_LOCATION); diff --git a/tests/TestEffects.cpp b/tests/TestEffects.cpp index d68faef6..f55bc7cb 100644 --- a/tests/TestEffects.cpp +++ b/tests/TestEffects.cpp @@ -19,6 +19,7 @@ #include "EffectTypes.h" #include "FrameCounter.h" #include "Generators.h" +#include "IsfGenerator.h" #include "Layer.h" #include "Link.h" #include "MappingManager.h" @@ -58,6 +59,12 @@ const char* const COLORIZE = "mapmap.shaders:colorize.fs"; const char* const THRESHOLD = "mapmap.shaders:threshold.fs"; const char* const POSTERIZE = "mapmap.shaders:posterize.fs"; const char* const LUMA_KEY = "mapmap.shaders:luma-key.fs"; +const char* const KALEIDOSCOPE = "mapmap.shaders:kaleidoscope.fs"; +const char* const MIRROR = "mapmap.shaders:mirror.fs"; +const char* const TRANSFORM = "mapmap.shaders:transform.fs"; +const char* const PIXELATE = "mapmap.shaders:pixelate.fs"; +const char* const RGB_SHIFT = "mapmap.shaders:rgb-shift.fs"; +const char* const EDGES = "mapmap.shaders:edges.fs"; /// Grey of pure red with no saturation: its weight in the brightness seen by the eye (BT.709). const int RED_LUMA = 54; @@ -238,18 +245,15 @@ struct EffectColors QColor onLayer; }; -/// Draws the shipped effect of that type, with those settings and that mix, on a source of that -/// colour, then on a layer of that source, and returns the colours at the centre. An unknown -/// type or setting gives invalid colours, with a warning. -EffectColors shippedEffectColors(const char* type, const QColor& color, const QVariantMap& settings, - double mix = 1.0) +/// Returns the shipped effect of that type with those settings; null, with a warning, for an +/// unknown type or setting. +Effect::ptr shippedEffect(const char* type, const QVariantMap& settings) { - EffectColors colors; Effect::ptr effect(EffectTypeRegistry::instance().create(type, NULL_UID)); if (!effect) { qWarning("No effect of type %s", type); - return colors; + return Effect::ptr(); } for (auto it = settings.constBegin(); it != settings.constEnd(); ++it) { @@ -257,9 +261,22 @@ EffectColors shippedEffectColors(const char* type, const QColor& color, const QV if (!setting || !setting->setValue(it.value())) { qWarning("Cannot set %s of %s", qPrintable(it.key()), type); - return colors; + return Effect::ptr(); } } + return effect; +} + +/// Draws the shipped effect of that type, with those settings and that mix, on a source of that +/// colour, then on a layer of that source, and returns the colours at the centre. An unknown +/// type or setting gives invalid colours, with a warning. +EffectColors shippedEffectColors(const char* type, const QColor& color, const QVariantMap& settings, + double mix = 1.0) +{ + EffectColors colors; + Effect::ptr effect = shippedEffect(type, settings); + if (!effect) + return colors; effect->setMix(mix); ColorGenerator source{color}; @@ -312,6 +329,21 @@ void addShippedEffectRows() << QStringList({ "levels", "softness", "dithering", "brightness_only" }); QTest::newRow("luma key") << QString(LUMA_KEY) << QString("Luma Key") << QStringList({ "low_threshold", "high_threshold", "invert", "channel" }); + QTest::newRow("kaleidoscope") + << QString(KALEIDOSCOPE) << QString("Kaleidoscope") + << QStringList({ "segments", "center", "sample_rotation", "rotation", "zoom" }); + QTest::newRow("mirror") << QString(MIRROR) << QString("Mirror") + << QStringList({ "horizontal", "from_right", "vertical", "from_bottom", "center", "angle" }); + QTest::newRow("transform") + << QString(TRANSFORM) << QString("Transform") + << QStringList({ "position", "scale", "rotation", "columns", "rows", "mirror_tiles", "row_offset", "skew", + "pivot", "stretch_x", "stretch_y", "outside" }); + QTest::newRow("pixelate") << QString(PIXELATE) << QString("Pixelate") + << QStringList({ "columns", "block_shape", "block_aspect", "gap", "gap_color" }); + QTest::newRow("rgb shift") << QString(RGB_SHIFT) << QString("RGB Shift") + << QStringList({ "distance", "angle", "radial", "red", "green", "blue" }); + QTest::newRow("edges") << QString(EDGES) << QString("Edges") + << QStringList({ "strength", "thickness", "threshold", "line_from", "line_color", "background" }); } /// The effect of a layer showing the bottom right quarter of the 2 × 2 texture. @@ -324,6 +356,118 @@ LayerEffectDrawing quarterEffect(const Effect* effect) return drawing; } +/// A generator whose colour gives its position: red from its left edge (0) to its right edge +/// (1), green from its top edge (0) to its bottom edge (1). Under a geometry effect, the colour +/// of a pixel says where the effect read the image. +const char* const POSITION_GENERATOR = + "/*{ \"INPUTS\": [] }*/\n" + "void main() { gl_FragColor = vec4(isf_FragNormCoord.x, 1.0 - isf_FragNormCoord.y, 0.0, 1.0); }\n"; + +/// POSITION_GENERATOR with its blue from its right edge (0) to its left edge (1), so that each +/// of the three channels says where it was read. +const char* const POSITION_IN_EACH_CHANNEL_GENERATOR = + "/*{ \"INPUTS\": [] }*/\n" + "void main() { gl_FragColor = vec4(isf_FragNormCoord.x, 1.0 - isf_FragNormCoord.y, 1.0 - isf_FragNormCoord.x, 1.0); }\n"; + +/// Largest difference allowed on a position read from the colours of POSITION_GENERATOR: a few +/// steps of a colour of eight bits. +const double POSITION_TOLERANCE = 0.03; + +/// The images of an effect over POSITION_GENERATOR, top row first: drawn on the source, then +/// on a layer of the source. A null image when the effect cannot be made or drawn. +struct EffectImages +{ + QImage onSource; + QImage onLayer; +}; + +/// Returns the two triangles over the whole image drawn into that read a part of the texture, +/// given from (0, 0) at its top left to (1, 1) at its bottom right. +QVector partVertices(const QRectF& part) +{ + QVector vertices = ShapeGeometry::rectangle(QRectF(0, 0, SIZE, SIZE)); + for (TexturedVertex& v : vertices) + { + v.s = float(part.x() + part.width() * v.s); + v.t = float(part.y() + part.height() * v.t); + } + return vertices; +} + +/// Draws an effect on POSITION_GENERATOR, then on a layer of it that shows that part, +/// stretched over the whole image drawn into. The generator, whose image has smaller copies +/// (see IntermediateImage), is read as the graphics card reads a shrunk image. Another generator +/// can be given. +EffectImages positionImages(const Effect::ptr& effect, const QRectF& part = QRectF(0, 0, 1, 1), + const char* generator = POSITION_GENERATOR) +{ + EffectImages images; + if (!effect) + return images; + + IsfGenerator source(IsfGenerator::ShaderPtr( + new IsfShader(IsfShader::fromText(generator, "test.effects:position.fs", "position.fs")))); + source.setResolution(SIZE); + source.insertEffect(0, effect); + FrameCounter::advance(); + source.updateShown(); + images.onSource = shownImage(source).convertToFormat(QImage::Format_ARGB32); + + // The same effect on a layer of the source, which then has none. + source.takeEffect(effect->getId()); + FrameCounter::advance(); + source.updateShown(); + LayerEffectDrawing drawing; + drawing.effect = effect.data(); + drawing.part = part; + drawing.size = QSizeF(part.width() * SIZE, part.height() * SIZE); + images.onLayer = draw(partVertices(part), &drawing, 1.0f, &source); + return images; +} + +/// Draws the shipped effect of that type, with those settings, as positionImages() above. +EffectImages positionImages(const char* type, const QVariantMap& settings, + const QRectF& part = QRectF(0, 0, 1, 1)) +{ + return positionImages(shippedEffect(type, settings), part); +} + +/// Checks that no pixel of an image is transparent, as where the smallest copy of an image +/// is read at a seam. +#define CHECK_OPAQUE(image) \ + do { \ + for (int row = 0; row < (image).height(); row++) \ + for (int column = 0; column < (image).width(); column++) \ + QVERIFY2((image).pixelColor(column, row).alpha() >= FULL - TOLERANCE, \ + qPrintable(QString("pixel (%1, %2) is %3").arg(column).arg(row) \ + .arg(colorText((image).pixelColor(column, row))))); \ + } while (false) + +/// Returns the position of POSITION_GENERATOR that a pixel shows, from its colour. +QPointF readPoint(const QImage& image, int x, int y) +{ + const QColor color = image.pixelColor(x, y); + return QPointF(color.redF(), color.greenF()); +} + +/// Checks the position of POSITION_GENERATOR that a pixel of an image shows. +#define CHECK_POINT(image, column, row, expected) \ + do { \ + const QPointF read = readPoint((image), (column), (row)); \ + const QPointF wanted = (expected); \ + QVERIFY2(qAbs(read.x() - wanted.x()) <= POSITION_TOLERANCE && \ + qAbs(read.y() - wanted.y()) <= POSITION_TOLERANCE, \ + qPrintable(QString("pixel (%1, %2) shows (%3, %4), expected (%5, %6)") \ + .arg(column).arg(row).arg(read.x()).arg(read.y()).arg(wanted.x()).arg(wanted.y()))); \ + } while (false) + +/// Checks that two pixels of an image have the same colour, alpha left aside. +#define CHECK_SAME_PIXELS(image, x1, y1, x2, y2) \ + QVERIFY2(closeColors((image).pixelColor((x1), (y1)), (image).pixelColor((x2), (y2))), \ + qPrintable(QString("pixel (%1, %2) is %3, pixel (%4, %5) is %6").arg(x1).arg(y1) \ + .arg(colorText((image).pixelColor((x1), (y1)))).arg(x2).arg(y2) \ + .arg(colorText((image).pixelColor((x2), (y2)))))) + } // namespace void TestEffects::initTestCase() @@ -1061,7 +1205,7 @@ void TestEffects::effectsAreSortedByFamily() EffectTypeRegistry::instance().remove("test.family:loose.fs"); // The shipped families in their order, then the others, then the effects without family. - QCOMPARE(families, QStringList({ "Color", "Keying", "Geometry", "Animals", QString() })); + QCOMPARE(families, QStringList({ "Color", "Keying", "Geometry", "Stylize", "Animals", QString() })); QCOMPARE(color, QStringList({ "Color Adjustment", "Colorize", "Invert", "Posterize", "Threshold" })); QCOMPARE(animals, QStringList({ "Aardvark", "Zebra" })); } @@ -1255,6 +1399,459 @@ void TestEffects::lumaKeyMakesTheDarkTransparent() QCOMPARE(shippedEffectColors(LUMA_KEY, QColor(140, 140, 140), crossed).onSource.alpha(), FULL); } +void TestEffects::kaleidoscopeRepeatsASlice() +{ + OffscreenContext gl; + if (!gl.ok) + QSKIP("No desktop OpenGL context available"); + // The centre of the image falls between the pixels 31 and 32. The pixel (44, 39) is 12.5 + // pixels right of it and 7.5 down, about 30° clockwise from the right; (50, 20) is above it. + const int x = 44; + const int y = 39; + const int highX = 50; + const int highY = 20; + const double distance = qSqrt(12.5 * 12.5 + 7.5 * 7.5) / SIZE; + + const EffectImages six = positionImages(KALEIDOSCOPE, QVariantMap()); + QVERIFY2(!six.onSource.isNull() && !six.onLayer.isNull(), "The effect is not drawn"); + for (const QImage& image : { six.onSource, six.onLayer }) + { + // In the middle of a segment of six, the image is read straight right of the centre. + CHECK_POINT(image, x, y, QPointF(0.5 + distance, 0.5)); + // Each slice beside its mirror image: the same above and below the line through the centre. + CHECK_SAME_PIXELS(image, x, y, x, SIZE - 1 - y); + CHECK_SAME_PIXELS(image, highX, highY, highX, SIZE - 1 - highY); + } + + // Four segments: the same a quarter turn away. + const EffectImages four = positionImages(KALEIDOSCOPE, QVariantMap({ { "segments", 4 } })); + for (const QImage& image : { four.onSource, four.onLayer }) + { + CHECK_SAME_PIXELS(image, x, y, SIZE - 1 - y, x); + CHECK_SAME_PIXELS(image, highX, highY, SIZE - 1 - highY, highX); + } + + // Turned by a twelfth of a turn, the pixel that was at the edge of a segment is now in its + // middle: 30° clockwise of the right is moved to 60°. + const EffectImages turned = positionImages(KALEIDOSCOPE, QVariantMap({ { "rotation", 30.0 } })); + const int turnedX = 38; + const int turnedY = 43; + const double turnedDistance = qSqrt(6.5 * 6.5 + 11.5 * 11.5) / SIZE; + CHECK_POINT(turned.onSource, turnedX, turnedY, QPointF(0.5 + turnedDistance, 0.5)); + + // On a layer that shows the right half of the source, made small enough to read beyond its + // edges: only that half is read, in a mirror beyond its edges, even where the image is shrunk + // and the graphics card reads a smoothed copy of it. + const QImage half = positionImages(KALEIDOSCOPE, QVariantMap({ { "zoom", 0.25 } }), QRectF(0.5, 0, 0.5, 1)).onLayer; + QVERIFY(!half.isNull()); + const int step = 4; + for (int row = 0; row < SIZE; row += step) + for (int column = 0; column < SIZE; column += step) + QVERIFY2(readPoint(half, column, row).x() >= 0.5 - POSITION_TOLERANCE, + qPrintable(QString("pixel (%1, %2) shows the left half: %3").arg(column).arg(row) + .arg(colorText(half.pixelColor(column, row))))); + + // Read beyond its edges, the image stays opaque: nothing transparent from outside it is + // smoothed into the pixels along the folds. + const QImage small = positionImages(KALEIDOSCOPE, QVariantMap({ { "zoom", 0.5 } })).onSource; + CHECK_OPAQUE(small); +} + +void TestEffects::mirrorKeepsOneHalf() +{ + OffscreenContext gl; + if (!gl.ok) + QSKIP("No desktop OpenGL context available"); + // The position of the centre of a pixel, from 0 to 1. + auto at = [](double pixel) { return (pixel + 0.5) / SIZE; }; + const int left = 10; + const int right = SIZE - 1 - left; + const int top = 20; + const int bottom = SIZE - 1 - top; + + // Left and right: the left half stays, the right half shows it in a mirror. + const EffectImages horizontal = positionImages(MIRROR, QVariantMap()); + QVERIFY2(!horizontal.onSource.isNull() && !horizontal.onLayer.isNull(), "The effect is not drawn"); + for (const QImage& image : { horizontal.onSource, horizontal.onLayer }) + { + CHECK_POINT(image, left, top, QPointF(at(left), at(top))); + CHECK_POINT(image, right, top, QPointF(at(left), at(top))); + CHECK_POINT(image, right, bottom, QPointF(at(left), at(bottom))); + } + + // Top and bottom only: the top half stays. + const EffectImages vertical = positionImages(MIRROR, QVariantMap({ { "horizontal", false }, { "vertical", true } })); + for (const QImage& image : { vertical.onSource, vertical.onLayer }) + { + CHECK_POINT(image, right, top, QPointF(at(right), at(top))); + CHECK_POINT(image, right, bottom, QPointF(at(right), at(top))); + } + + // From the right: the right half stays, the left half shows it in a mirror. + const EffectImages fromRight = positionImages(MIRROR, QVariantMap({ { "from_right", true } })); + for (const QImage& image : { fromRight.onSource, fromRight.onLayer }) + { + CHECK_POINT(image, left, top, QPointF(at(right), at(top))); + CHECK_POINT(image, right, top, QPointF(at(right), at(top))); + } + + // Both, each from its own side: from the right and from the top, the top right quarter is + // seen four times; from the left and from the bottom, the bottom left one. + const EffectImages topRight = positionImages(MIRROR, QVariantMap({ { "vertical", true }, { "from_right", true } })); + const EffectImages bottomLeft = positionImages(MIRROR, QVariantMap({ { "vertical", true }, { "from_bottom", true } })); + for (const QImage& image : { topRight.onSource, topRight.onLayer }) + { + CHECK_POINT(image, left, bottom, QPointF(at(right), at(top))); + CHECK_POINT(image, right, top, QPointF(at(right), at(top))); + } + for (const QImage& image : { bottomLeft.onSource, bottomLeft.onLayer }) + { + CHECK_POINT(image, right, top, QPointF(at(left), at(bottom))); + CHECK_POINT(image, left, bottom, QPointF(at(left), at(bottom))); + } + + // From the bottom alone does nothing while Vertical is off. + const EffectImages bottomOnly = positionImages(MIRROR, QVariantMap({ { "horizontal", false }, { "from_bottom", true } })); + CHECK_POINT(bottomOnly.onSource, right, top, QPointF(at(right), at(top))); + + // The mirror moved to a quarter of the width, at 16 pixels: what is right of it shows what is + // left of it, and what falls beyond the left edge is read again in a mirror. + const EffectImages moved = positionImages(MIRROR, QVariantMap({ { "center", QPointF(0.25, 0.5) } })); + const double axis = 0.25 * SIZE; + CHECK_POINT(moved.onSource, 20, top, QPointF((2.0 * axis - 20.5) / SIZE, at(top))); + CHECK_POINT(moved.onSource, 60, top, QPointF(-(2.0 * axis - 60.5) / SIZE, at(top))); + + // Turned a quarter turn, the mirror between left and right lies between top and bottom. + const EffectImages turned = positionImages(MIRROR, QVariantMap({ { "angle", 90.0 } })); + CHECK_POINT(turned.onSource, right, bottom, QPointF(at(right), at(top))); + CHECK_POINT(turned.onSource, right, top, QPointF(at(right), at(top))); + + // On a layer that shows the right half of the source, the mirror is in the middle of that + // half: the right of the drawing shows the left of the part, never the left of the source. + const QImage half = positionImages(MIRROR, QVariantMap(), QRectF(0.5, 0, 0.5, 1)).onLayer; + CHECK_POINT(half, right, top, QPointF(0.5 + 0.5 * at(left), at(top))); + CHECK_POINT(half, left, top, QPointF(0.5 + 0.5 * at(left), at(top))); +} + +void TestEffects::seamlessReadLeavesNoSeam() +{ + OffscreenContext gl; + if (!gl.ok) + QSKIP("No desktop OpenGL context available"); + // Three tiles across: the first border falls at 21.3 pixels, between the pixels 20 and 21, + // which the graphics card draws together. Read plainly, the jump from the right edge of the + // image to its left edge would read its smallest copy there, a grey, half transparent seam. + // Each tile is read a few pixels from the edges of the image, which the copy read where the + // image is shrunk three times calls for. + const Effect::ptr tiles(new IsfEffect(effectShader( + "void main() {\n" + " vec2 grid = isf_FragNormCoord * vec2(3.0, 1.0);\n" + " gl_FragColor = IMG_NORM_PIXEL_SEAMLESS(inputImage, clamp(fract(grid), 0.05, 0.95), grid);\n" + "}\n"))); + const EffectImages images = positionImages(tiles); + QVERIFY2(!images.onSource.isNull() && !images.onLayer.isNull(), "The effect is not drawn"); + CHECK_OPAQUE(images.onSource); + // On each side of the seam, a tile ends with the right of the image, the next one begins + // with its left. + const int lastOfTile = 20; + const double side = 0.1; + for (const QImage& image : { images.onSource, images.onLayer }) + { + QVERIFY2(readPoint(image, lastOfTile, SIZE / 2).x() > 1.0 - side, + qPrintable(colorText(image.pixelColor(lastOfTile, SIZE / 2)))); + QVERIFY2(readPoint(image, lastOfTile + 1, SIZE / 2).x() < side, + qPrintable(colorText(image.pixelColor(lastOfTile + 1, SIZE / 2)))); + } +} + +void TestEffects::transformMovesScalesAndTiles() +{ + OffscreenContext gl; + if (!gl.ok) + QSKIP("No desktop OpenGL context available"); + // The position of the centre of a pixel, from 0 to 1. + auto at = [](double pixel) { return (pixel + 0.5) / SIZE; }; + // Pixels, from the top left. + const int near = 10; + const int far = 40; + const int high = 20; + const int corner = 2; + + // As it is, the image is not changed. + const EffectImages same = positionImages(TRANSFORM, QVariantMap()); + QVERIFY2(!same.onSource.isNull() && !same.onLayer.isNull(), "The effect is not drawn"); + for (const QImage& image : { same.onSource, same.onLayer }) + CHECK_POINT(image, far, high, QPointF(at(far), at(high))); + + // Moved a quarter of the width to the right. + const EffectImages moved = positionImages(TRANSFORM, QVariantMap({ { "position", QPointF(0.75, 0.5) } })); + for (const QImage& image : { moved.onSource, moved.onLayer }) + CHECK_POINT(image, far, high, QPointF(at(far) - 0.25, at(high))); + + // Half as large around its centre: twice as far from the centre is read; nothing is seen + // beyond its edges, until the image is repeated there. + const QVariantMap half { { "scale", 0.5 } }; + const EffectImages smaller = positionImages(TRANSFORM, half); + auto halved = [&](double pixel) { return 0.5 + 2.0 * (at(pixel) - 0.5); }; + for (const QImage& image : { smaller.onSource, smaller.onLayer }) + CHECK_POINT(image, far, high, QPointF(halved(far), halved(high))); + QCOMPARE(smaller.onSource.pixelColor(corner, corner).alpha(), 0); + CHECK_PIXEL(smaller.onLayer, corner, corner, QColor(0, 0, 0)); + QVariantMap repeated = half; + repeated["outside"] = "repeat"; + const double wrapped = halved(corner) + 1.0; + CHECK_POINT(positionImages(TRANSFORM, repeated).onSource, corner, corner, QPointF(wrapped, wrapped)); + + // Half as wide only. + const EffectImages narrower = positionImages(TRANSFORM, QVariantMap({ { "stretch_x", 0.5 } })); + CHECK_POINT(narrower.onSource, far, high, QPointF(halved(far), at(high))); + + // Half as large around its top left corner, put at the top left: the top left quarter shows + // the whole image. + const EffectImages pivoted = positionImages(TRANSFORM, QVariantMap({ { "scale", 0.5 }, + { "pivot", QPointF(0, 0) }, { "position", QPointF(0, 0) } })); + CHECK_POINT(pivoted.onSource, near, near, QPointF(2.0 * at(near), 2.0 * at(near))); + + // A quarter turn clockwise: up right of the centre shows what was up left of it. + const EffectImages turned = positionImages(TRANSFORM, QVariantMap({ { "rotation", 90.0 } })); + CHECK_POINT(turned.onSource, 50, high, QPointF((SIZE / 2 - 11.5) / SIZE, (SIZE / 2 - 18.5) / SIZE)); + + // Three tiles across and two down, opaque along their seams. + const QVariantMap grid { { "columns", 3 }, { "rows", 2 } }; + const EffectImages tiles = positionImages(TRANSFORM, grid); + for (const QImage& image : { tiles.onSource, tiles.onLayer }) + { + CHECK_POINT(image, near, near, QPointF(3.0 * at(near), 2.0 * at(near))); + CHECK_POINT(image, 50, far, QPointF(3.0 * at(50) - 2.0, 2.0 * at(far) - 1.0)); + } + CHECK_OPAQUE(tiles.onSource); + + // Every other tile in a mirror: the second column is read from right to left. + QVariantMap mirrored = grid; + mirrored["mirror_tiles"] = true; + CHECK_POINT(positionImages(TRANSFORM, mirrored).onSource, 30, near, + QPointF(2.0 - 3.0 * at(30), 2.0 * at(near))); + + // The second row slid by half a tile, as bricks. + const EffectImages bricks = positionImages(TRANSFORM, QVariantMap({ { "columns", 2 }, { "rows", 2 }, + { "row_offset", 0.5 } })); + CHECK_POINT(bricks.onSource, near, far, QPointF(2.0 * at(near) + 0.5, 2.0 * at(far) - 1.0)); + CHECK_OPAQUE(bricks.onSource); + + // Slanted by half a tile: half way down, the image is read a quarter further right. + const EffectImages slanted = positionImages(TRANSFORM, QVariantMap({ { "skew", 0.5 } })); + CHECK_POINT(slanted.onSource, near, far, QPointF(at(near) + 0.5 * at(far), at(far))); + + // On a layer that shows the right half of the source, made half as large: only that half + // is shown, in the middle, and nothing around it. + const QImage part = positionImages(TRANSFORM, half, QRectF(0.5, 0, 0.5, 1)).onLayer; + CHECK_POINT(part, SIZE / 2, SIZE / 2, QPointF(0.75, at(SIZE / 2))); + CHECK_PIXEL(part, corner, corner, QColor(0, 0, 0)); +} + +void TestEffects::pixelateMakesBlocks() +{ + OffscreenContext gl; + if (!gl.ok) + QSKIP("No desktop OpenGL context available"); + // Eight blocks of 8 pixels across, eight down: every pixel of a block shows its centre. + const QVariantMap eight { { "columns", 8 } }; + const EffectImages blocks = positionImages(PIXELATE, eight); + QVERIFY2(!blocks.onSource.isNull() && !blocks.onLayer.isNull(), "The effect is not drawn"); + const QPointF blockCenter(12.0 / SIZE, 20.0 / SIZE); + for (const QImage& image : { blocks.onSource, blocks.onLayer }) + { + CHECK_POINT(image, 9, 17, blockCenter); + CHECK_POINT(image, 14, 22, blockCenter); + CHECK_POINT(image, 30, 50, QPointF(28.0 / SIZE, 52.0 / SIZE)); + } + + // Blocks twice as wide as tall: sixteen rows of 4 pixels. + QVariantMap wide = eight; + wide["block_aspect"] = 2.0; + CHECK_POINT(positionImages(PIXELATE, wide).onSource, 10, 21, QPointF(12.0 / SIZE, 22.0 / SIZE)); + + // Blocks taller than the rows that fit: 64 / 24 is 2.67 rows, cut equally at the top and the + // bottom, so that a block is centred on the middle of the height. + QVariantMap tall { { "columns", 2 } }; + tall["block_aspect"] = 32.0 / 24.0; + CHECK_POINT(positionImages(PIXELATE, tall).onSource, 10, 30, QPointF(16.0 / SIZE, 32.0 / SIZE)); + + // A gap of half a block, in red: the corners of a block are red, its centre shows the image. + const QColor red(Qt::red); + QVariantMap gapped = eight; + gapped["gap"] = 0.5; + gapped["gap_color"] = red; + const QImage spaced = positionImages(PIXELATE, gapped).onSource; + CHECK_PIXEL(spaced, 8, 16, red); + CHECK_POINT(spaced, 12, 20, blockCenter); + + // Round blocks and diamonds touch the middle of the sides of their block: its corners are in + // the gap colour, even without a gap; its centre shows the image. + for (int shape : { 1, 2 }) + { + QVariantMap shaped = eight; + shaped["block_shape"] = shape == 1 ? "round" : "diamond"; + shaped["gap_color"] = red; + const QImage image = positionImages(PIXELATE, shaped).onSource; + CHECK_PIXEL(image, 8, 16, red); + CHECK_POINT(image, 12, 20, blockCenter); + } + + // A transparent gap lets what is behind show. + QVariantMap clear = gapped; + clear["gap_color"] = QColor(0, 0, 0, 0); + QCOMPARE(positionImages(PIXELATE, clear).onSource.pixelColor(8, 16).alpha(), 0); + + // Three blocks across, whose borders fall between two pixels drawn together: no seam. + const QImage thirds = positionImages(PIXELATE, QVariantMap({ { "columns", 3 } })).onSource; + CHECK_OPAQUE(thirds); + const double firstCenter = 1.0 / 6.0; + CHECK_POINT(thirds, 20, 10, QPointF(firstCenter, firstCenter)); + CHECK_POINT(thirds, 21, 10, QPointF(0.5, firstCenter)); + + // On a layer that shows the right half of the source, in four blocks across: each shows a + // point of that half. + const QImage part = positionImages(PIXELATE, QVariantMap({ { "columns", 4 } }), QRectF(0.5, 0, 0.5, 1)).onLayer; + CHECK_POINT(part, 2, 2, QPointF(0.5 + 4.0 / SIZE, 4.0 / SIZE)); + CHECK_POINT(part, SIZE - 2, SIZE - 2, QPointF(1.0 - 4.0 / SIZE, 1.0 - 4.0 / SIZE)); +} + +void TestEffects::rgbShiftMovesTheChannels() +{ + OffscreenContext gl; + if (!gl.ok) + QSKIP("No desktop OpenGL context available"); + // The position each channel was read at, from the colour of a pixel: red and blue the + // horizontal one, green the vertical one. + auto redX = [](const QImage& image, int x, int y) { return image.pixelColor(x, y).redF(); }; + auto greenY = [](const QImage& image, int x, int y) { return image.pixelColor(x, y).greenF(); }; + auto blueX = [](const QImage& image, int x, int y) { return 1.0 - image.pixelColor(x, y).blueF(); }; + auto at = [](double pixel) { return (pixel + 0.5) / SIZE; }; + const int middle = SIZE / 2; + const double distance = 0.125; + auto shifted = [&](const QVariantMap& settings, const QRectF& part = QRectF(0, 0, 1, 1)) { + QVariantMap all = settings; + all["distance"] = distance; + Effect::ptr effect = shippedEffect(RGB_SHIFT, all); + return positionImages(effect, part, POSITION_IN_EACH_CHANNEL_GENERATOR); + }; +#define CHECK_READ(actual, expected) \ + QVERIFY2(qAbs((actual) - (expected)) <= POSITION_TOLERANCE, \ + qPrintable(QString("read at %1, expected %2").arg(actual).arg(expected))) + + // As it is: the red moved to the right, the blue to the left, the green where it was. + const EffectImages apart = shifted(QVariantMap()); + QVERIFY2(!apart.onSource.isNull() && !apart.onLayer.isNull(), "The effect is not drawn"); + for (const QImage& image : { apart.onSource, apart.onLayer }) + { + CHECK_READ(redX(image, middle, middle), at(middle) - distance); + CHECK_READ(blueX(image, middle, middle), at(middle) + distance); + CHECK_READ(greenY(image, middle, middle), at(middle)); + QCOMPARE(image.pixelColor(middle, middle).alpha(), FULL); + } + + // A quarter turn clockwise: the green moved down by half the distance, the others stay. + const QImage down = shifted(QVariantMap({ { "angle", 90.0 }, { "green", 0.5 } })).onSource; + CHECK_READ(greenY(down, middle, middle), at(middle) - 0.5 * distance); + CHECK_READ(redX(down, middle, middle), at(middle)); + + // Away from the centre: a quarter of the height to the right of it, the red moves by half + // the distance. + const int right = middle + SIZE / 4; + const QImage lens = shifted(QVariantMap({ { "radial", true } })).onSource; + CHECK_READ(redX(lens, right, middle), at(right) - 2.0 * distance * (at(right) - 0.5)); + CHECK_READ(redX(lens, middle, middle), at(middle)); + + // Along the left edge, the red comes from beyond the image: nothing; the blue is still there. + const QColor edge = apart.onSource.pixelColor(2, middle); + QCOMPARE(edge.red(), 0); + QVERIFY2(edge.blue() > 0 && edge.alpha() == FULL, qPrintable(colorText(edge))); + + // On a layer that shows the right half of the source: along its left edge too, the red comes + // from beyond the part, which it does not read. + const QImage part = shifted(QVariantMap(), QRectF(0.5, 0, 0.5, 1)).onLayer; + QCOMPARE(part.pixelColor(2, middle).red(), 0); + CHECK_READ(redX(part, middle, middle), 0.75 - distance); +#undef CHECK_READ +} + +void TestEffects::edgesDrawTheOutlines() +{ + OffscreenContext gl; + if (!gl.ok) + QSKIP("No desktop OpenGL context available"); + // A generator black on its left half, of a brightness on its right half: one outline, down + // its middle, between the pixels 31 and 32. + auto halves = [](double brightness) { + return QString("/*{ \"INPUTS\": [] }*/\n" + "void main() { gl_FragColor = vec4(vec3(%1 * step(0.5, isf_FragNormCoord.x)), 1.0); }\n") + .arg(brightness, 0, 'f', 2).toLatin1(); + }; + const QByteArray blackAndWhite = halves(1.0); + auto edges = [&](const QVariantMap& settings, const QByteArray& generator, const QRectF& part = QRectF(0, 0, 1, 1)) { + return positionImages(shippedEffect(EDGES, settings), part, generator.constData()); + }; + const int middle = SIZE / 2; + const int dark = 10; + const int light = 50; + const QColor black(Qt::black); + const QColor white(Qt::white); + + // As it is: a white line on black, on both sides of the outline; nothing along the edges of + // the image. + const EffectImages lines = edges(QVariantMap(), blackAndWhite); + QVERIFY2(!lines.onSource.isNull() && !lines.onLayer.isNull(), "The effect is not drawn"); + for (const QImage& image : { lines.onSource, lines.onLayer }) + { + CHECK_PIXEL(image, middle - 1, middle, white); + CHECK_PIXEL(image, middle, middle, white); + CHECK_PIXEL(image, dark, middle, black); + CHECK_PIXEL(image, light, middle, black); + CHECK_PIXEL(image, SIZE - 1, middle, black); + CHECK_PIXEL(image, light, 0, black); + QCOMPARE(image.pixelColor(dark, middle).alpha(), FULL); + } + + // Half as strong: a grey line. + CHECK_PIXEL(edges(QVariantMap({ { "strength", 0.5 } }), blackAndWhite).onSource, middle, middle, + QColor(128, 128, 128)); + + // Thicker: the line covers the pixels a few pixels away from the outline. + const QImage thick = edges(QVariantMap({ { "thickness", 4.0 / SIZE } }), blackAndWhite).onSource; + CHECK_PIXEL(thick, middle + 2, middle, white); + CHECK_PIXEL(thick, middle + 5, middle, black); + + // A faint outline, from black to 20 % grey: drawn with no threshold, hidden by a threshold + // above it. + const QByteArray faint = halves(0.2); + QVERIFY(edges(QVariantMap({ { "threshold", 0.0 } }), faint).onSource.pixelColor(middle, middle).red() > 0); + CHECK_PIXEL(edges(QVariantMap({ { "threshold", 0.3 } }), faint).onSource, middle, middle, black); + + // A red line on the image. + const QColor red(Qt::red); + const QImage onImage = edges(QVariantMap({ { "line_color", red }, { "background", "image" } }), blackAndWhite).onSource; + CHECK_PIXEL(onImage, middle, middle, red); + CHECK_PIXEL(onImage, dark, middle, black); + CHECK_PIXEL(onImage, light, middle, white); + + // Lines of the colours of the image, with nothing around them: white on the light side, black + // on the dark side, transparent elsewhere. + const QImage alone = edges(QVariantMap({ { "line_from", "image" }, { "background", "transparent" } }), + blackAndWhite).onSource; + CHECK_PIXEL(alone, middle, middle, white); + CHECK_PIXEL(alone, middle - 1, middle, black); + QCOMPARE(alone.pixelColor(middle - 1, middle).alpha(), FULL); + QCOMPARE(alone.pixelColor(dark, middle).alpha(), 0); + QCOMPARE(alone.pixelColor(light, middle).alpha(), 0); + + // On a layer that shows only the white half: no line along the edge of the part, where the + // black half of the source is not read. + const QImage part = edges(QVariantMap(), blackAndWhite, QRectF(0.5, 0, 0.5, 1)).onLayer; + CHECK_PIXEL(part, 0, middle, black); + CHECK_PIXEL(part, 1, middle, black); +} + void TestEffects::sourceAndLayerEffectsCompose() { OffscreenContext gl; diff --git a/tests/TestEffects.h b/tests/TestEffects.h index 7c4d4838..376ee513 100644 --- a/tests/TestEffects.h +++ b/tests/TestEffects.h @@ -70,6 +70,13 @@ private slots: void thresholdSplitsTheImage(); void posterizeMakesBands(); void lumaKeyMakesTheDarkTransparent(); + void kaleidoscopeRepeatsASlice(); + void mirrorKeepsOneHalf(); + void seamlessReadLeavesNoSeam(); + void transformMovesScalesAndTiles(); + void pixelateMakesBlocks(); + void rgbShiftMovesTheChannels(); + void edgesDrawTheOutlines(); void sourceAndLayerEffectsCompose(); }; diff --git a/tests/TestIsfShader.cpp b/tests/TestIsfShader.cpp index f0bc976d..b72f5185 100644 --- a/tests/TestIsfShader.cpp +++ b/tests/TestIsfShader.cpp @@ -337,6 +337,8 @@ void TestIsfShader::namesGeneratorsAfterTheirFiles() QCOMPARE(IsfShader::nameFromFileName("color-wheel.fs"), QString("Color Wheel")); QCOMPARE(IsfShader::nameFromFileName("Noise_Field.fs"), QString("Noise Field")); QCOMPARE(IsfShader::nameFromFileName("CMYK halftone.fs"), QString("CMYK Halftone")); + QCOMPARE(IsfShader::nameFromFileName("rgb-shift.fs"), QString("RGB Shift")); + QCOMPARE(IsfShader::nameFromFileName("hsv-to-rgb.fs"), QString("HSV To RGB")); } void TestIsfShader::knowsWhenTheImageChangesWithTime()