CDE
This commit is contained in:
parent
518da4a923
commit
764da6cbbb
6144 changed files with 83610 additions and 11 deletions
20
Task/Bitmap/C++/bitmap.cpp
Normal file
20
Task/Bitmap/C++/bitmap.cpp
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
#include <iostream>
|
||||
#include <boost/gil/gil_all.hpp>
|
||||
int main()
|
||||
{
|
||||
using namespace boost::gil;
|
||||
// create 30x40 image
|
||||
rgb8_image_t img(30, 40);
|
||||
|
||||
// fill with red
|
||||
rgb8_pixel_t red(255, 0, 0);
|
||||
fill_pixels(view(img), red);
|
||||
|
||||
// set pixel at 10x20 to blue
|
||||
rgb8_pixel_t blue(0, 0, 255);
|
||||
view(img)(10, 20) = blue;
|
||||
|
||||
// read the value of pixel at 11x20
|
||||
rgb8_pixel_t px = const_view(img)(11, 20);
|
||||
std::cout << "the pixel at 11, 20 is " << (unsigned)px[0] << ':' << (unsigned)px[1] << ':' << (unsigned)px[2] << '\n';
|
||||
}
|
||||
7
Task/Bitmap/Common-Lisp/bitmap-1.lisp
Normal file
7
Task/Bitmap/Common-Lisp/bitmap-1.lisp
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
(defpackage #:rgb-pixel-buffer
|
||||
(:use #:common-lisp)
|
||||
(:export #:rgb-pixel-component #:rgb-pixel #:rgb-pixel-buffer
|
||||
#:+red+ #:+green+ #:+blue+ #:+black+ #:+white+
|
||||
#:make-rgb-pixel #:make-rgb-pixel-buffer #:rgb-pixel-buffer-width
|
||||
#:rgb-pixel-buffer-height #:rgb-pixel-red #:rgb-pixel-green
|
||||
#:rgb-pixel-blue #:fill-rgb-pixel-buffer))
|
||||
69
Task/Bitmap/Common-Lisp/bitmap-2.lisp
Normal file
69
Task/Bitmap/Common-Lisp/bitmap-2.lisp
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
(in-package #:rgb-pixel-buffer)
|
||||
|
||||
(deftype rgb-pixel-component ()
|
||||
'(unsigned-byte 8))
|
||||
|
||||
(deftype rgb-pixel ()
|
||||
'(unsigned-byte 24))
|
||||
|
||||
(deftype rgb-pixel-buffer (&optional (width '*) (height '*))
|
||||
`(array rgb-pixel (,width ,height)))
|
||||
|
||||
(defconstant +black+ 0)
|
||||
(defconstant +white+ #xFFFFFF)
|
||||
(defconstant +red+ #xFF0000)
|
||||
(defconstant +green+ #x00FF00)
|
||||
(defconstant +blue+ #x0000FF)
|
||||
|
||||
(defun make-rgb-pixel (r g b)
|
||||
(declare (type rgb-pixel-component r g b))
|
||||
(logior (ash r 16) (ash g 8) b))
|
||||
|
||||
(defun rgb-pixel-red (rgb)
|
||||
(declare (type rgb-pixel rgb))
|
||||
(logand (ash rgb -16) #xFF))
|
||||
|
||||
(defun rgb-pixel-green (rgb)
|
||||
(declare (type rgb-pixel rgb))
|
||||
(logand (ash rgb -8) #xFF))
|
||||
|
||||
(defun rgb-pixel-blue (rgb)
|
||||
(declare (type rgb-pixel rgb))
|
||||
(logand rgb #xFF))
|
||||
|
||||
(defun make-rgb-pixel-buffer (width height &optional (initial-element +black+))
|
||||
(declare (type (integer 1) width height))
|
||||
(declare (type rgb-pixel initial-element))
|
||||
(make-array (list width height)
|
||||
:element-type 'rgb-pixel
|
||||
:initial-element initial-element))
|
||||
|
||||
(defun rgb-pixel-buffer-width (buffer)
|
||||
(first (array-dimensions buffer)))
|
||||
|
||||
(defun rgb-pixel-buffer-height (buffer)
|
||||
(second (array-dimensions buffer)))
|
||||
|
||||
(defun rgb-pixel (buffer x y)
|
||||
(declare (type rgb-pixel-buffer buffer))
|
||||
(declare (type (integer 0) x y))
|
||||
(aref buffer x y))
|
||||
|
||||
(defun (setf rgb-pixel) (value buffer x y)
|
||||
(declare (type rgb-pixel-buffer buffer))
|
||||
(declare (type rgb-pixel value))
|
||||
(declare (type (integer 0) x y))
|
||||
(setf (aref buffer x y) value))
|
||||
|
||||
(defun fill-rgb-pixel-buffer (buffer pixel)
|
||||
(declare (type rgb-pixel-buffer buffer))
|
||||
(declare (type rgb-pixel pixel))
|
||||
(let* ((dimensions (array-dimensions buffer))
|
||||
(width (first dimensions))
|
||||
(height (second dimensions)))
|
||||
(loop
|
||||
:for y :of-type fixnum :upfrom 0 :below height
|
||||
:do (loop
|
||||
:for x :of-type fixnum :upfrom 0 :below width
|
||||
:do (setf (rgb-pixel buffer x y) pixel)))
|
||||
buffer))
|
||||
6
Task/Bitmap/Common-Lisp/bitmap-3.lisp
Normal file
6
Task/Bitmap/Common-Lisp/bitmap-3.lisp
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
(defvar *buffer* (make-rgb-pixel-buffer 10 10))
|
||||
(fill-rgb-pixel-buffer *buffer* +white+)
|
||||
(setf (rgb-pixel *buffer* 0 0) +red+)
|
||||
(setf (rgb-pixel *buffer* 0 9) +red+)
|
||||
(setf (rgb-pixel *buffer* 9 0) +red+)
|
||||
(setf (rgb-pixel *buffer* 9 9) +red+)
|
||||
144
Task/Bitmap/D/bitmap.d
Normal file
144
Task/Bitmap/D/bitmap.d
Normal file
|
|
@ -0,0 +1,144 @@
|
|||
module bitmap;
|
||||
|
||||
import std.stdio, std.array, std.exception, std.string, std.conv;
|
||||
|
||||
final class Image(T) {
|
||||
static if (__traits(compiles, { auto x = T.black; }))
|
||||
T blackColor = T.black;
|
||||
else
|
||||
T blackColor = T.init;
|
||||
|
||||
T[] image;
|
||||
private size_t nx_, ny_;
|
||||
|
||||
this(in int nxx=0, in int nyy=0, in bool inizialize=true)
|
||||
pure nothrow {
|
||||
allocate(nxx, nyy, inizialize);
|
||||
}
|
||||
|
||||
void allocate(in int nxx=0, in int nyy=0, in bool inizialize=true)
|
||||
pure nothrow in {
|
||||
assert(nxx >= 0 && nyy >= 0);
|
||||
} body {
|
||||
this.nx_ = nxx;
|
||||
this.ny_ = nyy;
|
||||
if (nxx * nyy > 0) {
|
||||
if (inizialize)
|
||||
image.length = nxx * nyy;
|
||||
else // Optimization.
|
||||
image = minimallyInitializedArray!(typeof(image))
|
||||
(nxx * nyy);
|
||||
}
|
||||
}
|
||||
|
||||
static Image fromData(T[] data, in size_t nxx=0, in size_t nyy=0)
|
||||
pure nothrow in {
|
||||
assert(nxx >= 0 && nyy >= 0 && data.length == nxx * nyy);
|
||||
} body {
|
||||
auto result = new Image();
|
||||
result.image = data;
|
||||
result.nx_ = nxx;
|
||||
result.ny_ = nyy;
|
||||
return result;
|
||||
}
|
||||
|
||||
@property size_t nx() const pure nothrow { return nx_; }
|
||||
@property size_t ny() const pure nothrow { return ny_; }
|
||||
|
||||
ref T opIndex(in size_t x, in size_t y) pure nothrow
|
||||
in {
|
||||
assert(x < nx_ && y < ny_);
|
||||
} body {
|
||||
return image[x + y * nx_];
|
||||
}
|
||||
|
||||
T opIndex(in size_t x, in size_t y) const pure nothrow
|
||||
in {
|
||||
assert(x < nx_ && y < ny_);
|
||||
} body {
|
||||
return image[x + y * nx_];
|
||||
}
|
||||
|
||||
T opIndexAssign(in T color, in size_t x, in size_t y) pure nothrow
|
||||
in {
|
||||
assert(x < nx_ && y < ny_);
|
||||
} body {
|
||||
return image[x + y * nx_] = color;
|
||||
}
|
||||
|
||||
void opIndexUnary(string op)(in size_t x, in size_t y) pure nothrow
|
||||
if (op == "++" || op == "--") in {
|
||||
assert(x < nx_ && y < ny_);
|
||||
} body {
|
||||
mixin("image[x + y * nx_] " ~ op ~ ";");
|
||||
}
|
||||
|
||||
void clear(in T color=blackColor) pure nothrow {
|
||||
image[] = color;
|
||||
}
|
||||
|
||||
/// The axis origin is at the top left.
|
||||
void textualShow() const {
|
||||
size_t i = 0;
|
||||
foreach (immutable y; 0 .. ny_) {
|
||||
foreach (immutable x; 0 .. nx_)
|
||||
putchar(image[i++] == blackColor ? '#' : '.');
|
||||
putchar('\n');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
struct RGB {
|
||||
ubyte r, g, b;
|
||||
enum black = RGB();
|
||||
enum white = RGB(255, 255, 255);
|
||||
}
|
||||
|
||||
|
||||
Image!RGB loadPPM6(Image!RGB img, in string fileName) {
|
||||
if (img is null)
|
||||
img = new Image!RGB();
|
||||
auto f = File(fileName, "rb");
|
||||
enforce(f.readln().strip() == "P6");
|
||||
string line;
|
||||
do {
|
||||
line = f.readln();
|
||||
} while (line.length && line[0] == '#'); // Skip comments.
|
||||
const size = line.split();
|
||||
enforce(size.length == 2);
|
||||
img.allocate(size[0].to!uint(), size[1].to!uint());
|
||||
enforce(f.readln().strip() == "255");
|
||||
auto l = new ubyte[img.nx * 3];
|
||||
size_t i = 0;
|
||||
foreach (immutable y; 0 .. img.ny) {
|
||||
f.rawRead!ubyte(l);
|
||||
foreach (immutable x; 0 .. img.nx)
|
||||
img.image[i++] = RGB(l[x*3], l[x*3 + 1], l[x*3 + 2]);
|
||||
}
|
||||
return img;
|
||||
}
|
||||
|
||||
|
||||
void savePPM6(in Image!RGB img, in string fileName)
|
||||
in {
|
||||
assert(img !is null);
|
||||
assert(img.nx > 0 && img.nx > 0);
|
||||
} body {
|
||||
auto f = File(fileName, "wb");
|
||||
f.writefln("P6\n%d %d\n255", img.nx, img.ny);
|
||||
size_t i = 0;
|
||||
foreach (immutable y; 0 .. img.ny)
|
||||
foreach (immutable x; 0 .. img.nx) {
|
||||
immutable p = img.image[i++];
|
||||
f.write(cast(char)p.r, cast(char)p.g, cast(char)p.b);
|
||||
}
|
||||
}
|
||||
|
||||
version (bitmap_main) {
|
||||
void main() {
|
||||
auto img = new Image!RGB(30, 10);
|
||||
img[4, 5] = RGB.white;
|
||||
img.textualShow();
|
||||
}
|
||||
}
|
||||
97
Task/Bitmap/E/bitmap-1.e
Normal file
97
Task/Bitmap/E/bitmap-1.e
Normal file
|
|
@ -0,0 +1,97 @@
|
|||
def makeFlexList := <elib:tables.makeFlexList>
|
||||
def format := <import:java.lang.makeString>.format
|
||||
|
||||
def CHANNELS := 3
|
||||
def UByte := 0..255
|
||||
|
||||
def makeColor {
|
||||
to fromFloat(r, g, b) {
|
||||
return makeColor.fromByte((r * 255).round(),
|
||||
(g * 255).round(),
|
||||
(b * 255).round())
|
||||
}
|
||||
to fromByte(r :UByte, g :UByte, b :UByte) {
|
||||
def color {
|
||||
to __printOn(out) {
|
||||
out.print(format("%02x%02x%02x", [color.rb(), color.gb(), color.bb()]))
|
||||
}
|
||||
to rf() { return r / 255 }
|
||||
to gf() { return g / 255 }
|
||||
to bf() { return b / 255 }
|
||||
to rb() { return r }
|
||||
to gb() { return g }
|
||||
to bb() { return b }
|
||||
}
|
||||
return color
|
||||
}
|
||||
}
|
||||
|
||||
/** Convert 0..255 into 0..127 -128..-1 */
|
||||
def sign(v) {
|
||||
return v %% 256 - 2*(v & 128)
|
||||
}
|
||||
|
||||
def makeImage(width, height) {
|
||||
# NOTE: The primary E implementation is in Java and Java's fixed-size integers only
|
||||
# come in signed varieties. Therefore, there is a little bit of extra arithmetic.
|
||||
#
|
||||
# In an ideal E implementation we would specify the type 0..255, but this is not
|
||||
# currently possible everywhere, or efficient.
|
||||
|
||||
def storage := makeFlexList.fromType(<type:java.lang.Byte>, width * height * CHANNELS)
|
||||
storage.setSize(width * height * CHANNELS)
|
||||
|
||||
def X := 0..!width
|
||||
def Y := 0..!height
|
||||
|
||||
def flexImage {
|
||||
to __printOn(out) {
|
||||
for y in Y {
|
||||
out.print("[")
|
||||
for x in X {
|
||||
out.print(flexImage[x, y], " ")
|
||||
}
|
||||
out.println("]")
|
||||
}
|
||||
}
|
||||
to width() { return width }
|
||||
to height() { return height }
|
||||
to fill(color) {
|
||||
for x in X {
|
||||
for y in Y {
|
||||
flexImage[x, y] := color
|
||||
}
|
||||
}
|
||||
}
|
||||
to get(x :X, y :Y) {
|
||||
def base := (y * width + x) * CHANNELS
|
||||
return makeColor.fromByte(storage[base + 0] %% 256,
|
||||
storage[base + 1] %% 256,
|
||||
storage[base + 2] %% 256)
|
||||
}
|
||||
/** Provided to make [[Flood fill]] slightly less insanely slow. */
|
||||
to test(x :X, y :Y, c) {
|
||||
def base := (y * width + x) * CHANNELS
|
||||
return storage[base + 0] <=> sign(c.rb()) &&
|
||||
storage[base + 1] <=> sign(c.gb()) &&
|
||||
storage[base + 2] <=> sign(c.bb())
|
||||
}
|
||||
to put(x :X, y :Y, c) {
|
||||
def base := (y * width + x) * CHANNELS
|
||||
storage[base + 0] := sign(c.rb())
|
||||
storage[base + 1] := sign(c.gb())
|
||||
storage[base + 2] := sign(c.bb())
|
||||
}
|
||||
to writePPM(outputStream) {
|
||||
outputStream.write(`P6$\n$width $height$\n255$\n`.getBytes("US-ASCII"))
|
||||
outputStream.write(storage.getArray())
|
||||
}
|
||||
/** Used for [[Read ppm file]] */
|
||||
to replace(list :List) {
|
||||
require(list.size() == width * height * CHANNELS)
|
||||
storage(0) := list
|
||||
}
|
||||
}
|
||||
|
||||
return flexImage
|
||||
}
|
||||
29
Task/Bitmap/E/bitmap-2.e
Normal file
29
Task/Bitmap/E/bitmap-2.e
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
? def i := makeImage(3, 3)
|
||||
# value: [000000 000000 000000 ]
|
||||
# [000000 000000 000000 ]
|
||||
# [000000 000000 000000 ]
|
||||
#
|
||||
|
||||
? i.fill(makeColor.fromFloat(1, 0, 0))
|
||||
? i
|
||||
# value: [ff0000 ff0000 ff0000 ]
|
||||
# [ff0000 ff0000 ff0000 ]
|
||||
# [ff0000 ff0000 ff0000 ]
|
||||
#
|
||||
|
||||
? i[1, 1] := makeColor.fromFloat(0.5, 0.5, 0.5)
|
||||
# value: 808080
|
||||
|
||||
? i
|
||||
# value: [ff0000 ff0000 ff0000 ]
|
||||
# [ff0000 808080 ff0000 ]
|
||||
# [ff0000 ff0000 ff0000 ]
|
||||
#
|
||||
|
||||
? i[0, 1]
|
||||
# value: ff0000
|
||||
|
||||
? i[1, 1]
|
||||
# value: 808080
|
||||
|
||||
? i.writePPM(<import:java.io.makeFileOutputStream>(<file:~/Desktop/Rosetta.ppm>))
|
||||
23
Task/Bitmap/Euphoria/bitmap.euphoria
Normal file
23
Task/Bitmap/Euphoria/bitmap.euphoria
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
-- Some color constants:
|
||||
constant
|
||||
black = #000000,
|
||||
white = #FFFFFF,
|
||||
red = #FF0000,
|
||||
green = #00FF00,
|
||||
blue = #0000FF
|
||||
|
||||
-- Create new image filled with some color
|
||||
function new_image(integer width, integer height, atom fill_color)
|
||||
return repeat(repeat(fill_color,height),width)
|
||||
end function
|
||||
|
||||
-- Usage example:
|
||||
sequence image
|
||||
image = new_image(800,600,black)
|
||||
|
||||
-- Set pixel color:
|
||||
image[400][300] = red
|
||||
|
||||
-- Get pixel color
|
||||
sequence color
|
||||
color = image[400][300] -- Now color is #FF0000
|
||||
Loading…
Add table
Add a link
Reference in a new issue