-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0_3_draw_array_to_png.dart
56 lines (44 loc) · 1.37 KB
/
0_3_draw_array_to_png.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import 'dart:io';
import 'dart:math';
import 'package:image/image.dart';
import 'openfile.dart';
import 'pixel.dart';
/**
* Create and populate an array with random pixels.
* Save it as a PNG.
* Open it via the default Mac app.
*/
void main() {
const width = 640;
const height = 480;
const maxColourValue = 256;
List<Pixel> pixels = List<Pixel>(width * height);
Image im = Image(width, height);
List imagePixels = im.getBytes();
var rnd = Random();
// Image is prepped - draw!
for (var x = 0; x < width; x++) {
for (var y = 0; y < height; y++) {
var r = rnd.nextInt(maxColourValue).toDouble();
var g = rnd.nextInt(maxColourValue).toDouble();
var b = rnd.nextInt(maxColourValue).toDouble();
pixels[x + (y * width)] = Pixel(r, g, b);
}
}
var pixelpos = 0;
var maxsize = width * height * 4;
for (var pos = 0; pos < maxsize; pos += 4) {
var pix = pixels[pixelpos];
imagePixels[pos] = pix.r.toInt();
imagePixels[pos + 1] = pix.g.toInt();
imagePixels[pos + 2] = pix.b.toInt();
imagePixels[pos + 3] = 255;
pixelpos += 1;
}
Image image = Image.fromBytes(width, height, imagePixels);
// Done Drawing, now save it.
List<int> png = encodePng(image);
new File('output_arraypixels.png').writeAsBytesSync(png);
// Open the file in the Mac previewer
OpenFile().openFileInPreview('output_arraypixels.png');
}