Another update from ingydotnet^djgoku

This commit is contained in:
Ingy döt Net 2015-11-18 06:14:39 +00:00
parent 91df62d461
commit 948b86eafa
7604 changed files with 108452 additions and 22726 deletions

View file

@ -1,4 +1,5 @@
NB. create 10 by 10 block of magenta pixels in top right quadrant of a 300 wide by 600 high green image
myimg=: ((145 + pixellist) ; 255 0 255) setPixels 0 255 0 makeRGB 600 200
pixellist=: >,{;~i.10
myimg=: ((150 + pixellist) ; 255 0 255) setPixels 0 255 0 makeRGB 600 300
myimg writeppm jpath '~temp/myimg.ppm'
360015
540015

View file

@ -0,0 +1,15 @@
using Color, Images, FixedPointNumbers
w = 70
h = 50
a = zeros(RGB{Ufixed8}, h, w)
img = Image(a)
img["x", 10:40, "y", 5:35] = color("skyblue")
for i in 45:65, j in (i-25):40
img["x", i, "y", j] = color("sienna1")
end
imwrite(img, "bitmap_write.ppm")
imwrite(img, "bitmap_write.png")

View file

@ -0,0 +1,16 @@
function writeppm(fn::String, a::Image)
outf = open(fn, "w")
(w, h) = size(a.pic)
write(outf, "P6\n")
write(outf, @sprintf "%d %d\n" w h)
write(outf, @sprintf "%d\n" 255)
for i in 1:h
for j in 1:w
c = color(a, j, i)
write(outf, c.r)
write(outf, c.g)
write(outf, c.b)
end
end
close(outf)
end

View file

@ -0,0 +1,18 @@
w = 500
h = 300
a = Image(w, h)
purple = Color(0xff, 0, 0xff)
green = Color(0, 0xff, 0)
white = Color(0xff, 0xff, 0xff)
fill!(a, green)
for i in 20:220, j in 10:100
splat!(a, i, j, purple)
end
for i in 180:400, j in 80:200
splat!(a, i, j, white)
end
fn = "bitmap_write.ppm"
writeppm(fn, a)

View file

@ -1,70 +1,69 @@
use std::vec::from_elem;
use std::path::posix::{Path};
use std::io::File;
use std::path::Path;
use std::io::Write;
use std::fs::File;
pub struct RGB {
r: u8
,g: u8
,b: u8
r: u8,
g: u8,
b: u8,
}
pub struct PPM {
height: uint
,width: uint
,data: ~[u8]
height: u32,
width: u32,
data: Vec<u8>,
}
impl PPM {
pub fn new(height: uint, width: uint) -> PPM {
let size = 3 * height * width;
let buffer = from_elem(size, 0u8);
PPM{height: height, width: width, data: buffer}
}
fn buffer_size(&self) -> uint {
3 * self.height * self.width
}
fn get_offset(&self, x: uint, y: uint) -> Option<uint> {
let offset = (y * self.width * 3) + (x * 3);
if(offset < self.buffer_size()){
Some(offset)
}else{
None
pub fn new(height: u32, width: u32) -> PPM {
let size = 3 * height * width;
let buffer = vec![0; size as usize];
PPM { height: height, width: width, data: buffer }
}
}
pub fn get_pixel(&self, x: uint, y: uint) -> Option<RGB> {
match self.get_offset(x, y) {
Some(offset) => {
let r = self.data[offset];
let g = self.data[offset + 1];
let b = self.data[offset + 2];
Some(RGB{r, g, b})
},
None => None
fn buffer_size(&self) -> u32 {
3 * self.height * self.width
}
}
pub fn set_pixel(&mut self, x: uint, y: uint, color: RGB) -> bool {
match self.get_offset(x, y) {
Some(offset) => {
self.data[offset] = color.r;
self.data[offset + 1] = color.g;
self.data[offset + 2] = color.b;
true
},
None => false
fn get_offset(&self, x: u32, y: u32) -> Option<usize> {
let offset = (y * self.width * 3) + (x * 3);
if offset < self.buffer_size() {
Some(offset as usize)
} else {
None
}
}
}
pub fn write_file(&self, filename: &str) -> bool {
let path = Path::new(filename);
let mut file = File::create(&path);
let header = format!("P6 {} {} 255\n", self.width, self.height);
file.write(header.as_bytes());
file.write(self.data);
true
}
pub fn get_pixel(&self, x: u32, y: u32) -> Option<RGB> {
match self.get_offset(x, y) {
Some(offset) => {
let r = self.data[offset];
let g = self.data[offset + 1];
let b = self.data[offset + 2];
Some(RGB {r: r, g: g, b: b})
},
None => None
}
}
pub fn set_pixel(&mut self, x: u32, y: u32, color: RGB) -> bool {
match self.get_offset(x, y) {
Some(offset) => {
self.data[offset] = color.r;
self.data[offset + 1] = color.g;
self.data[offset + 2] = color.b;
true
},
None => false
}
}
pub fn write_file(&self, filename: &str) -> std::io::Result<()> {
let path = Path::new(filename);
let mut file = try!(File::create(&path));
let header = format!("P6 {} {} 255\n", self.width, self.height);
try!(file.write(header.as_bytes()));
try!(file.write(&self.data));
Ok(())
}
}