Advertisement
Guest User

Processing - Pixel Stretching (RGB)

a guest
Jul 3rd, 2015
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. // pixel stretching
  2.  
  3. PImage img;
  4. PImage out;
  5. int[] offsetsR;
  6. int[] offsetsG;
  7. int[] offsetsB;
  8.  
  9. float AMOUNT = 12.0;
  10. String INPUT_FILE = "C:\\path\\to\\input.jpg";
  11. String OUTPUT_FILE = "C:\\path\\to\\output.jpg";
  12.  
  13. void setup()
  14. {
  15. img = loadImage(INPUT_FILE);
  16. out = createImage(img.width, img.height, RGB);
  17. for (int i = 0; i < img.width * img.height; i++)
  18. {
  19. out.pixels[i] = 0;
  20. }
  21. offsetsR = new int[img.width];
  22. offsetsG = new int[img.width];
  23. offsetsB = new int[img.width];
  24. size(img.width, img.height);
  25. }
  26.  
  27. void draw()
  28. {
  29. background(0);
  30. for (int x = 0; x < img.width; x++)
  31. {
  32. for (int y = 0; y < img.height; y++)
  33. {
  34. // read source pixel intensity
  35. color src = img.get(x, y);
  36. float intensityR = red(src) / 255.0;
  37. float intensityG = green(src) / 255.0;
  38. float intensityB = blue(src) / 255.0;
  39. // compute an integer pixel length based on intensity, and ensure it's at least 1
  40. int pixelLengthR = int(log(intensityR * AMOUNT));
  41. int pixelLengthG = int(log(intensityG * AMOUNT));
  42. int pixelLengthB = int(log(intensityB * AMOUNT));
  43. pixelLengthR = (pixelLengthR < 1) ? 1 : pixelLengthR;
  44. pixelLengthG = (pixelLengthG < 1) ? 1 : pixelLengthG;
  45. pixelLengthB = (pixelLengthB < 1) ? 1 : pixelLengthB;
  46. // capture the existing offset for this column (starts at 0)
  47. int posR = offsetsR[x];
  48. int posG = offsetsG[x];
  49. int posB = offsetsB[x];
  50. // draw colour for the computed pixel length...
  51. // red
  52. for (int n = 0; n < pixelLengthR; n++)
  53. {
  54. color c = out.get(x, posR + n);
  55. c &= 0x00FFFF;
  56. c |= int(red(src)) << 16;
  57. out.set(x, posR + n, c);
  58. }
  59. // green
  60. for (int n = 0; n < pixelLengthG; n++)
  61. {
  62. color c = out.get(x, posG + n);
  63. c &= 0xFF00FF;
  64. c |= int(green(src)) << 8;
  65. out.set(x, posG + n, c);
  66. }
  67. // blue
  68. for (int n = 0; n < pixelLengthB; n++)
  69. {
  70. color c = out.get(x, posB + n);
  71. c &= 0xFFFF00;
  72. c |= int(blue(src));
  73. out.set(x, posB + n, c);
  74. }
  75.  
  76. // update the offset for this line
  77. offsetsR[x] += pixelLengthR;
  78. offsetsG[x] += pixelLengthG;
  79. offsetsB[x] += pixelLengthB;
  80. }
  81. }
  82. out.updatePixels();
  83. out.save(OUTPUT_FILE);
  84. image(out, 0, 0);
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement