How do I artificially introduce aliasing?

Hello
I have always been fascinated with aliasing, and I am sitting with a project that implements aliasing as a deliberate aesthetical choice. I guess that it’s all about duplicating values, so I wrote this code that takes a list of values, and outputs a modified list with duplicates. This would have to be done for both x-axis and y-axis.
So for instance: A list like
[[1,2,3,4], [5,6,7,8], [9,10,11,12], [13,14,15,15]]
when processed with a factor of 2 for both x-axis and y-axis, would produce
[[1, 1, 3, 3], [1, 1, 3, 3], [9, 9, 11, 11], [9, 9, 11, 11]]

I have come to the following solution:

l = [
     [1,2,3,4],
     [5,6,7,8],
     [9,10,11,12],
     [13,14,15,16]
     ]

valx,valy = 2,2 # repetition factors, can be set individually for each axis
halpx,halpy = 0,0 # helper variables for counting
out = [] # the final list

# duplicating the x-axis: repeat values for the x-axis (factor set by valx)
print("x-axis:")
for sublist in l:
    for i in range(len(sublist)):
        if halpx % valx != 0:
            sublist[i] = sublist[i-1]
        halpx += 1

print(l)

print("y-axis:")
# duplicating the y-axis: repeat values for the y-axis (factor set by valy)
for sublist in l:
    if halpy % valy == 0:
        for i in range(valy):
            out.append(sublist)
    halpy += 1
out = out[:len(l)]

print(out)

This works just fine. But I was wondering if there were better and faster ways of doing this. Thanks for taking a look!

One way is to render your image to a lower resolution PGraphics and then display it at a higher resolution. The problem you’ll hit, though, is that Processing insists on smoothing out the pixels when blowing up an image (since that’s nearly always what you want) even if you tell it noSmooth(). My solution is to use my own shader to display the image doing my own point sampling.

PGraphics lowRes;
PShader shdr;

void setup() {
  size( 600, 600, P2D );
  //noSmooth();
  
  lowRes = createGraphics( 100, 100, P2D );
  lowRes.noSmooth();
 
  shdr = new PShader( this, vertSrc, fragSrc );
}

void draw() {
  lowRes.beginDraw();
  lowRes.background( 128 );
  lowRes.stroke( 0 );
  lowRes.circle( 40, 40, 60 );
  lowRes.stroke( 255, 192, 0 );
  lowRes.translate( 50, 50 );
  lowRes.rotate( frameCount*0.002 );
  lowRes.line( -45, 0, 45, 0 );
  lowRes.endDraw();

  shdr.set( "img", lowRes );
  filter( shdr );
//  image( lowRes, 0, 0, width, height );
}


String[] vertSrc = {"""
#version 330 core
uniform mat4 modelview;
uniform mat4 projection;
in vec4 position;
in vec4 texCoord;
out vec2 uv;
void main() {
  uv = texCoord.xy;
  uv.y = 1.0 - uv.y;
  gl_Position = projection * modelview * position;
}
"""};


String[] fragSrc = {"""
#version 330 core
uniform sampler2D img;
in vec2 uv;
out vec4 fragColor;
void main() {
  ivec2 ij = ivec2( uv*textureSize( img, 0 ).xy );
  vec3 col = texelFetch( img, ij, 0 ).rgb;
  fragColor = vec4( col, 1.0 );
}
"""};
1 Like