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)

1  
static RGB probeRandomPixel(RGBImage image) {
2  
  int x = (int) (random()*(image.getWidth()-1));
3  
  int y = (int) (random()*(image.getHeight()-1));
4  
  return image.getPixel(x, y);
5  
}
6  
7  
static float probeRandomPixel(BWImage image) {
8  
  int x = (int) (random()*(image.getWidth()-1));
9  
  int y = (int) (random()*(image.getHeight()-1));
10  
  return image.getPixel(x, y);
11  
}
12  
13  
static RGB randomColor() {
14  
  return new RGB(random(), random(), random());
15  
}
16  
17  
static float randomBrightness() {
18  
  ret (float) random();
19  
}
20  
21  
static RGBImage resizeToWidth(RGBImage image, int w) {
22  
  return resize(image, w, (int) ((image.getHeight()*(double) w)/image.getWidth()));
23  
}
24  
25  
public static RGBImage resize(RGBImage image, int w, int h) {
26  
  if (w == image.getWidth() && h == image.getHeight()) return image;
27  
28  
  int[] pixels = new int[w*h];
29  
  for (int y = 0; y < h; y++)
30  
    for (int x = 0; x < w; x++)
31  
      pixels[y*w+x] = image.getInt(x*image.getWidth()/w, y*image.getHeight()/h);
32  
  return new RGBImage(w, h, pixels);
33  
}
34  
35  
static Random _random = new Random();
36  
static double random() {
37  
  return _random.nextInt(100001)/100000.0;
38  
}
39  
40  
static int random(int max) {
41  
  return _random.nextInt(max);
42  
}
43  
44  
static double random(double min, double max) {
45  
  return min+random()*(max-min);
46  
}
47  
48  
static double normalize(double d) {
49  
  return Math.max(0, Math.min(1, d));
50  
}
51  
52  
static int round(double d) {
53  
  return (int) Math.round(d);
54  
}
55  
56  
static double mix(double a, double b, double bishness) {
57  
  return a+(b-a)*bishness;
58  
}
59  
60  
public static double pixelDiff(RGB a, RGB b) {
61  
  return (Math.abs(a.r-b.r) + Math.abs(a.g-b.g) + Math.abs(a.b-b.b))/3;
62  
}
63  
64  
public static double diff(RGBImage image, RGBImage image2) {
65  
  int w = image.getWidth(), h = image.getHeight();
66  
  double sum = 0;
67  
  for (int y = 0; y < h; y++)
68  
    for (int x = 0; x < w; x++)
69  
      sum += pixelDiff(image.getRGB(x, y), image2.getRGB(x, y));
70  
  return sum/(w*h);
71  
}
72  
73  
static double diff(BWImage image, BWImage image2) {
74  
  int w = image.getWidth(), h = image.getHeight();
75  
  double sum = 0;
76  
  for (int y = 0; y < h; y++)
77  
    for (int x = 0; x < w; x++)
78  
      sum += Math.abs(image.getPixel(x, y) - image2.getPixel(x, y));
79  
  return sum/(w*h);
80  
}
81  
82  
public static void copy(RGBImage src, int srcX, int srcY, RGBImage dst, int dstX, int dstY, int w, int h) {
83  
  for (int y = 0; y < h; y++)
84  
    for (int x = 0; x < w; x++)
85  
      dst.setPixel(dstX+x, dstY+y, src.getPixel(srcX+x, srcY+y));
86  
}
87  
88  
public static void copy(BWImage src, int srcX, int srcY, BWImage dst, int dstX, int dstY, int w, int h) {
89  
  w = min(w, dst.getWidth()-dstX);
90  
  for (int y = 0; y < h; y++)
91  
    for (int x = 0; x < w; x++)
92  
      dst.setByte(dstX+x, dstY+y, src.getByte(srcX+x, srcY+y));
93  
}
94  
95  
static float varyChannel(float x) {
96  
  return Math.max(0f, Math.min(1f, (float) (x+random(-0.1, 0.1))));
97  
}
98  
99  
static double vary01(double x) {
100  
  return Math.max(0, Math.min(1, x+random(-0.1, 0.1)));
101  
}
102  
103  
static RGB varyColor(RGB rgb) {
104  
  int s = random(3);
105  
  if (s == 0)
106  
    return new RGB(varyChannel(rgb.r), rgb.g, rgb.b);
107  
  else if (s == 1)
108  
    return new RGB(rgb.r, varyChannel(rgb.g), rgb.b);
109  
  else
110  
    return new RGB(rgb.r, rgb.g, varyChannel(rgb.b));
111  
}

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: 709 / 4211
Referenced in: [show references]