Sync
This commit is contained in:
parent
6f050a029e
commit
776bba907c
3887 changed files with 59894 additions and 7280 deletions
48
Task/Bitmap/ALGOL-68/bitmap-1.alg
Normal file
48
Task/Bitmap/ALGOL-68/bitmap-1.alg
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
# -*- coding: utf-8 -*- #
|
||||
|
||||
MODE PIXEL = STRUCT(#SHORT# BITS red,green,blue);
|
||||
MODE POINT = STRUCT(INT x,y);
|
||||
|
||||
MODE IMAGE = [0,0]PIXEL; # instance attributes #
|
||||
|
||||
MODE CLASSIMAGE = STRUCT ( # class attributes #
|
||||
PIXEL black, red, green, blue, white,
|
||||
PROC (REF IMAGE)REF IMAGE init,
|
||||
PROC (REF IMAGE, PIXEL)VOID fill,
|
||||
PROC (REF IMAGE)VOID print,
|
||||
# virtual: #
|
||||
REF PROC (REF IMAGE, POINT, POINT, PIXEL)VOID line,
|
||||
REF PROC (REF IMAGE, POINT, INT, PIXEL)VOID circle,
|
||||
REF PROC (REF IMAGE, POINT, POINT, POINT, POINT, PIXEL, UNION(INT, VOID))VOID cubic bezier
|
||||
);
|
||||
|
||||
CLASSIMAGE class image = (
|
||||
# black = # (#SHORTEN# 16r00, #SHORTEN# 16r00, #SHORTEN# 16r00),
|
||||
# red = # (#SHORTEN# 16rff, #SHORTEN# 16r00, #SHORTEN# 16r00),
|
||||
# green = # (#SHORTEN# 16r00, #SHORTEN# 16rff, #SHORTEN# 16r00),
|
||||
# blue = # (#SHORTEN# 16r00, #SHORTEN# 16r00, #SHORTEN# 16rff),
|
||||
# white = # (#SHORTEN# 16rff, #SHORTEN# 16rff, #SHORTEN# 16rff),
|
||||
# PROC init = # (REF IMAGE self)REF IMAGE:
|
||||
BEGIN
|
||||
(fill OF class image)(self, black OF class image);
|
||||
self
|
||||
END,
|
||||
|
||||
# PROC fill = # (REF IMAGE self, PIXEL color)VOID:
|
||||
FOR x FROM 1 LWB self TO 1 UPB self DO
|
||||
FOR y FROM 2 LWB self TO 2 UPB self DO
|
||||
self[x,y] := color
|
||||
OD
|
||||
OD,
|
||||
# PROC print = # (REF IMAGE self)VOID:
|
||||
printf(($n(UPB self)(3(16r2d))l$, self)),
|
||||
# virtual: #
|
||||
# REF PROC line = # LOC PROC (REF IMAGE, POINT, POINT, PIXEL)VOID,
|
||||
# REF PROC circle = # LOC PROC (REF IMAGE, POINT, INT, PIXEL)VOID,
|
||||
# REF PROC cubic bezier = # LOC PROC (REF IMAGE, POINT, POINT, POINT, POINT, PIXEL, UNION(INT, VOID))VOID
|
||||
);
|
||||
|
||||
OP CLASSOF = (IMAGE image)CLASSIMAGE: class image;
|
||||
OP INIT = (REF IMAGE image)REF IMAGE: (init OF (CLASSOF image))(image);
|
||||
|
||||
SKIP
|
||||
11
Task/Bitmap/ALGOL-68/bitmap-2.alg
Normal file
11
Task/Bitmap/ALGOL-68/bitmap-2.alg
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
#!/usr/bin/a68g --script #
|
||||
# -*- coding: utf-8 -*- #
|
||||
|
||||
### The test program ###
|
||||
PR READ "prelude/Bitmap.a68" PR;
|
||||
|
||||
test:(
|
||||
REF IMAGE x := INIT LOC[1:16, 1:16]PIXEL;
|
||||
(fill OF class image) (x, white OF class image);
|
||||
(print OF class image) (x)
|
||||
)
|
||||
|
|
@ -1,12 +1,15 @@
|
|||
module bitmap;
|
||||
|
||||
import std.stdio, std.array, std.exception, std.string, std.conv;
|
||||
import std.stdio, std.array, std.exception, std.string, std.conv,
|
||||
std.algorithm, std.ascii;
|
||||
|
||||
final class Image(T) {
|
||||
static if (__traits(compiles, { auto x = T.black; }))
|
||||
T blackColor = T.black;
|
||||
static if (is(typeof({ auto x = T.black; })))
|
||||
const static T black = T.black;
|
||||
else
|
||||
T blackColor = T.init;
|
||||
const static T black = T.init;
|
||||
static if (is(typeof({ auto x = T.white; })))
|
||||
const static T white = T.white;
|
||||
|
||||
T[] image;
|
||||
private size_t nx_, ny_;
|
||||
|
|
@ -73,16 +76,37 @@ final class Image(T) {
|
|||
mixin("image[x + y * nx_] " ~ op ~ ";");
|
||||
}
|
||||
|
||||
void clear(in T color=blackColor) pure nothrow {
|
||||
void clear(in T color=this.black) pure nothrow {
|
||||
image[] = color;
|
||||
}
|
||||
|
||||
/// Convert a 2D array of chars to a binary Image.
|
||||
static Image fromText(in string txt,
|
||||
in char one='#', in char zero='.')
|
||||
pure
|
||||
out(result) {
|
||||
assert(result.image.all!(x => x == black || x == white));
|
||||
} body {
|
||||
auto M = txt
|
||||
.strip
|
||||
.split
|
||||
.map!(row => row
|
||||
.filter!(c => c == one || c == zero)
|
||||
.map!(c => cast(T)(c == one))
|
||||
.array)
|
||||
.array;
|
||||
assert(M.join.length > 0); // Not empty.
|
||||
foreach (row; M)
|
||||
assert(row.length == M[0].length); // Rectangular
|
||||
return Image.fromData(M.join, M[0].length, M.length);
|
||||
}
|
||||
|
||||
/// The axis origin is at the top left.
|
||||
void textualShow() const {
|
||||
void textualShow(in char bl='#', in char wh='.') const {
|
||||
size_t i = 0;
|
||||
foreach (immutable y; 0 .. ny_) {
|
||||
foreach (immutable x; 0 .. nx_)
|
||||
putchar(image[i++] == blackColor ? '#' : '.');
|
||||
putchar(image[i++] == black ? bl : wh);
|
||||
putchar('\n');
|
||||
}
|
||||
}
|
||||
|
|
@ -91,8 +115,8 @@ final class Image(T) {
|
|||
|
||||
struct RGB {
|
||||
ubyte r, g, b;
|
||||
enum black = RGB();
|
||||
enum white = RGB(255, 255, 255);
|
||||
static immutable black = typeof(this)();
|
||||
static immutable white = typeof(this)(255, 255, 255);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
28
Task/Bitmap/Elixir/bitmap.elixir
Normal file
28
Task/Bitmap/Elixir/bitmap.elixir
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
defmodule RosBitmap do
|
||||
defrecord Bitmap, pixels: nil, shape: {0, 0}
|
||||
|
||||
defp new(width, height, {:rgb, r, g, b}) do
|
||||
Bitmap[
|
||||
pixels: :array.new(width * height,
|
||||
{:default, <<r::size(8), g::size(8), b::size(8)>>}),
|
||||
shape: {width, height}]
|
||||
end
|
||||
|
||||
def new(width, height), do: new(width, height, {:rgb, 0, 0, 0})
|
||||
|
||||
def fill(Bitmap[shape: {width, height}], {:rgb, _r, _g, _b}=color) do
|
||||
new(width, height, color)
|
||||
end
|
||||
|
||||
def set_pixel(Bitmap[pixels: pixels, shape: {width, _height}]=bitmap,
|
||||
{:at, x, y}, {:rgb, r, g, b}) do
|
||||
index = x + y * width
|
||||
bitmap.pixels(:array.set(index, <<r::size(8), g::size(8), b::size(8)>>, pixels))
|
||||
end
|
||||
|
||||
def get_pixel(Bitmap[pixels: pixels, shape: {width, _height}], {:at, x, y}) do
|
||||
index = x + y * width
|
||||
<<r::size(8), g::size(8), b::size(8)>> = :array.get(index, pixels)
|
||||
{:rgb, r, g, b}
|
||||
end
|
||||
end
|
||||
25
Task/Bitmap/Erlang/bitmap.erl
Normal file
25
Task/Bitmap/Erlang/bitmap.erl
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
-module(ros_bitmap).
|
||||
|
||||
-export([new/2, fill/2, set_pixel/3, get_pixel/2]).
|
||||
|
||||
-record(bitmap, {
|
||||
pixels = nil,
|
||||
shape = {0, 0}
|
||||
}).
|
||||
|
||||
new(Width, Height) ->
|
||||
#bitmap{pixels=array:new(Width * Height, {default, <<0:8, 0:8, 0:8>>}), shape={Width, Height}}.
|
||||
|
||||
fill(#bitmap{shape={Width, Height}}, {rgb, R, G, B}) ->
|
||||
#bitmap{
|
||||
pixels=array:new(Width * Height, {default, <<R:8, G:8, B:8>>}),
|
||||
shape={Width, Height}}.
|
||||
|
||||
set_pixel(#bitmap{pixels=Pixels, shape={Width, _Height}}=Bitmap, {at, X, Y}, {rgb, R, G, B}) ->
|
||||
Index = X + Y * Width,
|
||||
Bitmap#bitmap{pixels=array:set(Index, <<R:8, G:8, B:8>>, Pixels)}.
|
||||
|
||||
get_pixel(#bitmap{pixels=Pixels, shape={Width, _Height}}, {at, X, Y}) ->
|
||||
Index = X + Y * Width,
|
||||
<<R:8, G:8, B:8>> = array:get(Index, Pixels),
|
||||
{rgb, R, G, B}.
|
||||
28
Task/Bitmap/JavaScript/bitmap.js
Normal file
28
Task/Bitmap/JavaScript/bitmap.js
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
// Set up the canvas
|
||||
var canvas = document.createElement("canvas"),
|
||||
ctx = canvas.getContext("2d"),
|
||||
width = 400, height = 400;
|
||||
|
||||
ctx.canvas.width = width;
|
||||
ctx.canvas.height = height;
|
||||
|
||||
// Optionaly add it to the current page
|
||||
document.body.appendChild(canvas);
|
||||
|
||||
// Draw an image
|
||||
var img = document.createElement("img");
|
||||
img.onload = function(){
|
||||
// Draw the element into the top-left of the canvas
|
||||
ctx.drawImage(img, 0, 0);
|
||||
};
|
||||
img.src = "//placehold.it/400x400";
|
||||
|
||||
// Fill the canvas with a solid blue color
|
||||
ctx.fillStyle = "blue";
|
||||
ctx.fillRect(0, 0, width, height);
|
||||
|
||||
// Place a black pixel in the middle
|
||||
// Note that a pixel is a 1 by 1 rectangle
|
||||
// This is the fastest method as of 2012 benchmarks
|
||||
ctx.fillStyle = "black";
|
||||
ctx.fillRect(width / 2, height / 2, 1, 1);
|
||||
57
Task/Bitmap/Scala/bitmap-3.scala
Normal file
57
Task/Bitmap/Scala/bitmap-3.scala
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
package org.rosettacode
|
||||
package bitmapstore
|
||||
|
||||
object RgbBitmap {
|
||||
|
||||
import java.awt.image.BufferedImage
|
||||
import java.awt.Color
|
||||
|
||||
class RgbBitmap(val dim: (Int, Int)) {
|
||||
def width = dim._1
|
||||
def height = dim._2
|
||||
|
||||
private val image = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR)
|
||||
|
||||
def apply(x: Int, y: Int) = new Color(image.getRGB(x, y))
|
||||
|
||||
def update(x: Int, y: Int, c: Color) = image.setRGB(x, y, c.getRGB())
|
||||
|
||||
def fill(c: Color) =
|
||||
{
|
||||
val g = image.getGraphics()
|
||||
g.setColor(c)
|
||||
g.fillRect(0, 0, width, height)
|
||||
}
|
||||
}
|
||||
|
||||
object RgbBitmap {
|
||||
def apply(width: Int, height: Int) = new RgbBitmap(width, height)
|
||||
}
|
||||
|
||||
def main(args: Array[String]): Unit = { // Test Scala style
|
||||
val img = RgbBitmap(50, 60) // Calls the apply function in companion object
|
||||
img.fill(Color.CYAN)
|
||||
img(5, 6) = Color.BLUE
|
||||
|
||||
assert(img(0, 1) == Color.CYAN) // Calls automagically the apply method in the class
|
||||
assert(img(5, 6) == Color.BLUE)
|
||||
assert(img.dim == (50, 60))
|
||||
|
||||
/** Even Javanese style testing is still possible.
|
||||
*/
|
||||
import language.reflectiveCalls // Just to satisfy a polite warning
|
||||
val img0 = new RgbBitmap(50, 60) { // Wrappers to enable adhoc Javanese style
|
||||
def getPixel(x: Int, y: Int) = this(x, y)
|
||||
def setPixel(x: Int, y: Int, c: Color) = this(x, y) = c
|
||||
}
|
||||
|
||||
img0.fill(Color.CYAN)
|
||||
img0.setPixel(5, 6, Color.BLUE)
|
||||
// Testing java style
|
||||
assert(img0.getPixel(0, 1) == Color.CYAN)
|
||||
assert(img0.getPixel(5, 6) == Color.BLUE)
|
||||
assert(img0.width == 50)
|
||||
assert(img0.height == 60)
|
||||
println("No errors found.")
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue