Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // pixel stretching
- PImage img;
- PImage out;
- int[] offsetsR;
- int[] offsetsG;
- int[] offsetsB;
- float AMOUNT = 12.0;
- String INPUT_FILE = "C:\\path\\to\\input.jpg";
- String OUTPUT_FILE = "C:\\path\\to\\output.jpg";
- void setup()
- {
- img = loadImage(INPUT_FILE);
- out = createImage(img.width, img.height, RGB);
- for (int i = 0; i < img.width * img.height; i++)
- {
- out.pixels[i] = 0;
- }
- offsetsR = new int[img.width];
- offsetsG = new int[img.width];
- offsetsB = new int[img.width];
- size(img.width, img.height);
- }
- void draw()
- {
- background(0);
- for (int x = 0; x < img.width; x++)
- {
- for (int y = 0; y < img.height; y++)
- {
- // read source pixel intensity
- color src = img.get(x, y);
- float intensityR = red(src) / 255.0;
- float intensityG = green(src) / 255.0;
- float intensityB = blue(src) / 255.0;
- // compute an integer pixel length based on intensity, and ensure it's at least 1
- int pixelLengthR = int(log(intensityR * AMOUNT));
- int pixelLengthG = int(log(intensityG * AMOUNT));
- int pixelLengthB = int(log(intensityB * AMOUNT));
- pixelLengthR = (pixelLengthR < 1) ? 1 : pixelLengthR;
- pixelLengthG = (pixelLengthG < 1) ? 1 : pixelLengthG;
- pixelLengthB = (pixelLengthB < 1) ? 1 : pixelLengthB;
- // capture the existing offset for this column (starts at 0)
- int posR = offsetsR[x];
- int posG = offsetsG[x];
- int posB = offsetsB[x];
- // draw colour for the computed pixel length...
- // red
- for (int n = 0; n < pixelLengthR; n++)
- {
- color c = out.get(x, posR + n);
- c &= 0x00FFFF;
- c |= int(red(src)) << 16;
- out.set(x, posR + n, c);
- }
- // green
- for (int n = 0; n < pixelLengthG; n++)
- {
- color c = out.get(x, posG + n);
- c &= 0xFF00FF;
- c |= int(green(src)) << 8;
- out.set(x, posG + n, c);
- }
- // blue
- for (int n = 0; n < pixelLengthB; n++)
- {
- color c = out.get(x, posB + n);
- c &= 0xFFFF00;
- c |= int(blue(src));
- out.set(x, posB + n, c);
- }
- // update the offset for this line
- offsetsR[x] += pixelLengthR;
- offsetsG[x] += pixelLengthG;
- offsetsB[x] += pixelLengthB;
- }
- }
- out.updatePixels();
- out.save(OUTPUT_FILE);
- image(out, 0, 0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement