Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

111
LINES

< > BotCompany Repo | #1000522 // Helper functions for reproducing images

JavaX fragment (include)

static RGB probeRandomPixel(RGBImage image) {
  int x = (int) (random()*(image.getWidth()-1));
  int y = (int) (random()*(image.getHeight()-1));
  return image.getPixel(x, y);
}

static float probeRandomPixel(BWImage image) {
  int x = (int) (random()*(image.getWidth()-1));
  int y = (int) (random()*(image.getHeight()-1));
  return image.getPixel(x, y);
}

static RGB randomColor() {
  return new RGB(random(), random(), random());
}

static float randomBrightness() {
  ret (float) random();
}

static RGBImage resizeToWidth(RGBImage image, int w) {
  return resize(image, w, (int) ((image.getHeight()*(double) w)/image.getWidth()));
}

public static RGBImage resize(RGBImage image, int w, int h) {
  if (w == image.getWidth() && h == image.getHeight()) return image;

  int[] pixels = new int[w*h];
  for (int y = 0; y < h; y++)
    for (int x = 0; x < w; x++)
      pixels[y*w+x] = image.getInt(x*image.getWidth()/w, y*image.getHeight()/h);
  return new RGBImage(w, h, pixels);
}

static Random _random = new Random();
static double random() {
  return _random.nextInt(100001)/100000.0;
}

static int random(int max) {
  return _random.nextInt(max);
}

static double random(double min, double max) {
  return min+random()*(max-min);
}

static double normalize(double d) {
  return Math.max(0, Math.min(1, d));
}

static int round(double d) {
  return (int) Math.round(d);
}

static double mix(double a, double b, double bishness) {
  return a+(b-a)*bishness;
}

public static double pixelDiff(RGB a, RGB b) {
  return (Math.abs(a.r-b.r) + Math.abs(a.g-b.g) + Math.abs(a.b-b.b))/3;
}

public static double diff(RGBImage image, RGBImage image2) {
  int w = image.getWidth(), h = image.getHeight();
  double sum = 0;
  for (int y = 0; y < h; y++)
    for (int x = 0; x < w; x++)
      sum += pixelDiff(image.getRGB(x, y), image2.getRGB(x, y));
  return sum/(w*h);
}

static double diff(BWImage image, BWImage image2) {
  int w = image.getWidth(), h = image.getHeight();
  double sum = 0;
  for (int y = 0; y < h; y++)
    for (int x = 0; x < w; x++)
      sum += Math.abs(image.getPixel(x, y) - image2.getPixel(x, y));
  return sum/(w*h);
}

public static void copy(RGBImage src, int srcX, int srcY, RGBImage dst, int dstX, int dstY, int w, int h) {
  for (int y = 0; y < h; y++)
    for (int x = 0; x < w; x++)
      dst.setPixel(dstX+x, dstY+y, src.getPixel(srcX+x, srcY+y));
}

public static void copy(BWImage src, int srcX, int srcY, BWImage dst, int dstX, int dstY, int w, int h) {
  w = min(w, dst.getWidth()-dstX);
  for (int y = 0; y < h; y++)
    for (int x = 0; x < w; x++)
      dst.setByte(dstX+x, dstY+y, src.getByte(srcX+x, srcY+y));
}

static float varyChannel(float x) {
  return Math.max(0f, Math.min(1f, (float) (x+random(-0.1, 0.1))));
}

static double vary01(double x) {
  return Math.max(0, Math.min(1, x+random(-0.1, 0.1)));
}

static RGB varyColor(RGB rgb) {
  int s = random(3);
  if (s == 0)
    return new RGB(varyChannel(rgb.r), rgb.g, rgb.b);
  else if (s == 1)
    return new RGB(rgb.r, varyChannel(rgb.g), rgb.b);
  else
    return new RGB(rgb.r, rgb.g, varyChannel(rgb.b));
}

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1000522
Snippet name: Helper functions for reproducing images
Eternal ID of this version: #1000522/1
Text MD5: 0cfa8e6eb8ec0e9c8cc3bbcca9df6b95
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-08-20 21:45:09
Source code size: 3220 bytes / 111 lines
Pitched / IR pitched: No / No
Views / Downloads: 707 / 4208
Referenced in: [show references]