June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -3,34 +3,29 @@ import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
public class BasicBitmapStorage
{
private BufferedImage image;
public class BasicBitmapStorage {
public BasicBitmapStorage(final int width, final int height)
{
image = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
}
private final BufferedImage image;
public void fill(final Color c)
{
Graphics g = image.getGraphics();
g.setColor(c);
g.fillRect(0, 0, image.getWidth(), image.getHeight());
}
public BasicBitmapStorage(int width, int height) {
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
}
public void setPixel(final int x, final int y, final Color c)
{
image.setRGB(x, y, c.getRGB());
}
public void fill(Color c) {
Graphics g = image.getGraphics();
g.setColor(c);
g.fillRect(0, 0, image.getWidth(), image.getHeight());
}
public Color getPixel(final int x, final int y)
{
return new Color(image.getRGB(x, y));
}
public void setPixel(int x, int y, Color c) {
image.setRGB(x, y, c.getRGB());
}
public Image getImage()
{
return image;
}
public Color getPixel(int x, int y) {
return new Color(image.getRGB(x, y));
}
public Image getImage() {
return image;
}
}

View file

@ -1,19 +1,22 @@
import static org.junit.Assert.assertEquals;
import java.awt.Color;
import junit.framework.TestCase;
import org.junit.Test;
public class BasicBitmapStorageTest extends TestCase
{
public static final int WIDTH = 640, HEIGHT = 480;
public class BasicBitmapStorageTest {
BasicBitmapStorage bbs = new BasicBitmapStorage(WIDTH, HEIGHT);
@Test
public void testHappy() {
int width = 640;
int height = 480;
public void testHappy()
{
bbs.fill(Color.cyan);
bbs.setPixel(WIDTH / 2, HEIGHT / 2, Color.BLACK);
Color c1 = bbs.getPixel(WIDTH / 2, HEIGHT / 2);
Color c2 = bbs.getPixel(20, 20);
assertEquals(Color.BLACK, c1);
assertEquals(Color.CYAN, c2);
}
BasicBitmapStorage bbs = new BasicBitmapStorage(width, height);
bbs.fill(Color.CYAN);
bbs.setPixel(width / 2, height / 2, Color.BLACK);
Color c1 = bbs.getPixel(width / 2, height / 2);
Color c2 = bbs.getPixel(20, 20);
assertEquals(Color.BLACK, c1);
assertEquals(Color.CYAN, c2);
}
}

View file

@ -0,0 +1,24 @@
using Images, Colors
Base.hex(p::RGB{T}) where T = join(hex(c(p), 2) for c in (red, green, blue))
function showhex(m::Matrix{RGB{T}}, pad::Integer=4) where T
for r in 1:size(m, 1)
println(" " ^ pad, join(hex.(m[r, :]), " "))
end
end
w, h = 5, 7
cback = RGB(1, 0, 1)
cfore = RGB(0, 1, 0)
img = Array{RGB{N0f8}}(h, w);
println("Uninitialized image:")
showhex(img)
fill!(img, cback)
println("\nImage filled with background color:")
showhex(img)
img[2, 3] = cfore
println("\nImage with a pixel set for foreground color:")
showhex(img)

View file

@ -0,0 +1,23 @@
allocateImg := proc(width, height)
return Array(1..width, 1..height, 1..3);
end proc:
fillColor := proc(img, rgb::list)
local i;
for i from 1 to 3 do
img[..,..,i] := map(x->rgb[i], img[..,..,i]):
end do:
end proc:
setColor := proc(x, y, img, rgb::list)
local i:
for i from 1 to 3 do
img[x,y,i] := rgb[i]:
end do:
end proc:
getColor := proc(x,y,img)
local rgb,i:
rgb := Array(1..3):
for i from 1 to 3 do
rgb(i) := img[x,y,i]:
end do:
return rgb:
end proc:

View file

@ -10,7 +10,7 @@ class Bitmap {
$i where ^$!width,
$j where ^$!height
--> Pixel
) is rw { @!data[$i*$!height + $j] }
) is rw { @!data[$i + $j * $!width] }
method set-pixel ($i, $j, Pixel $p) {
self.pixel($i, $j) = $p.clone;

View file

@ -2,36 +2,36 @@
red = '00000000 00000000 11111111'b /*define a red value. */
blue = '11111111 00000000 00000000'b /* " " blue " */
blue = 'ff 00 00'x /*another way to define a blue value.*/
image.= /*"allocate" an "array" to nulls. */
call RGBfill red /*set the entire image to red. */
call RGBset 10, 40, blue /*set a pixel to blue. */
x=10; y=40; pix=RGBget(x, y) /*get the color of a pixel. */
hexv = c2x(pix) /*get hex value of pix's color. */
binv = x2b(hexv) /* " binary " " " " */
say 'pixel' x","y '=' binv /*show the binary value of 20,50 */
bin3v = left(binv, 8) substr(binv, 9, 8) right(binv, 8)
say 'pixel' x","y '=' bin3v /*show again, but with spaces. */
@. = /*define entire @. array to nulls. */
x=10; y=40 /*set pixel's coördinates. */
call RGBfill red /*set the entire image to red. */
call RGBset x, y, blue /*set a pixel (at 10,40) to blue. */
color = RGBget(x, y) /*get the color of a pixel. */
hexV = c2x(color) /*get hex value of pixel's color. */
binV = x2b(hexV) /* " binary " " " " */
bin3V = left(binV, 8) substr(binV, 9, 8) right(binV, 8)
hex3V = left(hexV, 2) substr(hexV, 3, 2) right(hexV, 2)
xy= '(' || x","y')' /*create a handy─dandy literal for SAY.*/
say xy ' pixel in binary: ' binV /*show the binary value of 20,50 */
say xy ' pixel in binary: ' bin3V /*show again, but with spaces. */
say /*show a blank between binary and hex. */
say 'pixel' x","y '=' hexv /*show again, but in hexadecimal. */
hex3v = left(hexv, 2) substr(hexv, 3, 2) right(hexv, 2)
say 'pixel' x","y '=' hex3v /*show again, but with spaces. */
say xy ' pixel in hex: ' hexV /*show again, but in hexadecimal. */
say xy ' pixel in hex: ' hex3V /*show again, but with spaces. */
call PPMwrite 'image', 500, 500 /*create a PPM (output) file of image. */ /* ◄■■■■■■■■ not part of this task.*/
say /*show a blank. */
say 'The file image.PPM was created.' /*inform user that a file was created. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
RGBfill: procedure expose image.; image.=arg(1); return
RGBfill: @.=arg(1); return /*fill image with a color.*/
RGBget: parse arg px,py; return @.px.py /*get a pixel's color. */
RGBset: parse arg px,py,p$; @.px.py=p$; return /*set " " " */
/*──────────────────────────────────────────────────────────────────────────────────────*/
RGBget: procedure expose image.; parse arg Xpixel,Ypixel
return image.Xpixel.Ypixel /*obtain and return the pixel's color. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
RGBraster: procedure expose image.; parse arg Xsize,Ysize,color; RGB=
do x=1 to Xsize; _= /*build raster one line at a time. */
do y=1 to Ysize /* " a line " pixel " " " */
_=_ || image.x.y /*append single pixel to the line. */
end /*y*/ /* [↑] all done building a line. */
RGB=RGB || _ /*append a single line to raster. */
end /*x*/ /* [↑] all done building raster. */
r return RGB /*return RGB raster to the invoker. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
RGBset: procedure expose image.; parse arg Xpixel,Ypixel,color
image.Xpixel.Ypixel=color; return /*define pixel, return to the invoker. */
PPMwrite: parse arg oFN, width, height /*obtain output filename, width, height*/
oFID= oFN'.PPM'; $='9'x; #=255 /*fileID; separator; max color value.*/
call charout oFID, , 1 /*set the position of the file's output*/
call charout oFID,'P6'width || $ || height || $ || # || $ /*write hdr info.*/
do j=1 for width
do k=1 for height; call charout oFID, @.j.k
end /*k*/ /* ↑ write the PPM file, ··· */
end /*j*/ /* └───────── ··· one pixel at a time.*/
call charout oFID; return /*close the output file just to be safe*/