Bitmap: Difference between revisions

22,639 bytes added ,  6 months ago
m
→‎{{header|Wren}}: Changed to Wren S/H
m (Automated syntax highlighting fixup (second round - minor fixes))
m (→‎{{header|Wren}}: Changed to Wren S/H)
(17 intermediate revisions by 6 users not shown)
Line 421:
ldrH r3,[r2]
bx lr</syntaxhighlight>
=={{header|ATS}}==
 
Because this code will be used in other tasks, I have separated it into "static" and "dynamic" source files. The former is the equivalent of an "interface" file in some other languages, and the latter is equivalent of an "implementation" file. I included some test code that gets compiled if you put the correct option on the compiler command line.
 
===The ATS static file===
This file should be called <code>bitmap_task.sats</code>.
<syntaxhighlight lang="ats">
#define ATS_PACKNAME "Rosetta_Code.bitmap_task"
 
(*------------------------------------------------------------------*)
 
(* I am going to do this at the most primitive level. So here is the
"abstractified" type, or really a whole set of different types:
w-by-h pixmap of values of type a, with pixel storage at address
p. The type is linear (‘use it once and only once’). We will make
pixmap a boxed type, so its size will be equal to that of a
pointer. (This is actually a general 2-dimensional array type!
But let us ignore that.) *)
absvtype pixmap (a : t@ype, w : int, h : int, p : addr) = ptr
 
(* A shorthand for a pixmap with its pixel storage at "some"
address. *)
vtypedef pixmap (a : t@ype, w : int, h : int) =
[p : addr] pixmap (a, w, h, p)
 
(* A shorthand for a pixmap with "some" width and height, and with its
pixel storage at "some" address. *)
vtypedef pixmap (a : t@ype) = [w, h : int] pixmap (a, w, h)
 
(* A shorthand for a pixmap with "some" POSITIVE width and POSITIVE
height, and with its pixel storage at "some" address. *)
vtypedef pixmap1 (a : t@ype) = [w, h : pos] pixmap (a, w, h)
 
(*------------------------------------------------------------------*)
(* Here are definitions for a small set of operations, including the
ones requested in the task document.
 
But note that, in ATS, we are careful about uninitialized data. It
is POSSIBLE to create an uninitialized pixmap, but NOT possible to
set or get individual pixels, if the pixmap is not already fully
initialized by some other means (such as "fill" or "load"). *)
 
fn {}
pixmap_width :
{a : t@ype}
{w, h : int}
(!pixmap (a, w, h)) -<> size_t w
 
fn {}
pixmap_height :
{a : t@ype}
{w, h : int}
(!pixmap (a, w, h)) -<> size_t h
 
fn {a : t@ype}
pixmap_make_array :
(* Make a new pixmap from an existing array. The array may be
anywhere (for instance, a stack frame or the heap), and need not
be initialized. *)
{w, h : int} {p : addr}
(array_v (a, p, w * h) | size_t w, size_t h, ptr p) ->
pixmap (a, w, h, p)
 
fn {a : t@ype}
pixmap_unmake :
(* Essentially the reverse of pixmap_make_array. Temporarily treat a
pixmap as an array. The array will be organized as rows from left
to right, with the rows themselves going from top to bottom. Thus
an index would be i = x + (y * w). *)
{w, h : int} {p : addr}
pixmap (a, w, h, p) ->
@(array_v (a, p, w * h) | size_t w, size_t h, ptr p)
 
prfn
pixmap_prove_index_bounds :
(* A proof that i = x + (y * w) is within bounds of the array
returned by pixmap_unmake. *)
{w, h : int}
{x, y : nat | x < w; y < h}
() -<prf>
[0 <= x + (y * w);
x + (y * w) < w * h]
void
 
fn {a : t@ype}
pixmap_make_uninitized :
(* Make a new uninitialized pixmap, with the pixels stored in the
heap. *)
{w, h : int}
(size_t w, size_t h) ->
[p : addr | null < p] @(mfree_gc_v p | pixmap (a?, w, h, p))
 
fn {a : t@ype}
pixmap_make_elt :
(* Make a new pixmap, initialized with a given element, with the
pixels stored in the heap. *)
{w, h : int}
(size_t w, size_t h, a) ->
[p : addr | null < p] @(mfree_gc_v p | pixmap (a, w, h, p))
 
fn {}
pixmap_free_storage_return :
(* Free a pixmap, returning the storage array to the user. *)
{a : t@ype}
{w, h : int} {p : addr}
pixmap (a, w, h, p) -> @(array_v (a, p, w * h) | ptr p)
 
fn {}
pixmap_free_storage_free :
(* If a pixmap's pixels were allocated in the heap, then free its
storage. *)
{a : t@ype}
{w, h : int} {p : addr}
(mfree_gc_v p | pixmap (a, w, h, p)) -> void
 
fn {a : t@ype}
pixmap_fill_elt :
(* Fill a pixmap with the given element. (Technically speaking, the
value of the first argument is consumed, and replaced by a new
value. Its type before and after is linear.) *)
{w, h : int} {p : addr}
(* The question mark means that the pixmap elements can start out
uninitialized. *)
(!pixmap (a?, w, h, p) >> pixmap (a, w, h, p), a) -> void
 
fn {a : t@ype}
{tk : tkind}
pixmap_set_at_guint :
(* Set a pixel at unsigned integer coordinates. You can do this only
on a pixmap that has been initialized. (It would be prohibitively
tedious to safely work with randomly located pixels, if the array
were not already fully initialized.) *)
{w, h : int}
{x, y : int | x < w; y < h}
(!pixmap (a, w, h), g1uint (tk, x), g1uint (tk, y), a) -> void
 
fn {a : t@ype}
{tk : tkind}
pixmap_set_at_gint :
(* Set a pixel, but with signed integer coordinates. *)
{w, h : int}
{x, y : nat | x < w; y < h}
(!pixmap (a, w, h), g1int (tk, x), g1int (tk, y), a) -> void
 
fn {a : t@ype} {tk : tkind}
pixmap_get_at_guint :
(* Get a pixel at unsigned integer coordinates. You can do this only
on a pixmap that has been initialized. *)
{w, h : int}
{x, y : int | x < w; y < h}
(!pixmap (a, w, h), g1uint (tk, x), g1uint (tk, y)) -> a
 
fn {a : t@ype} {tk : tkind}
pixmap_get_at_gint :
(* Get a pixel, but with signed integer coordinates. *)
{w, h : int}
{x, y : nat | x < w; y < h}
(!pixmap (a, w, h), g1int (tk, x), g1int (tk, y)) -> a
 
fn {a : t@ype}
pixmap_dump :
(* Dump the contents of a pixmap to an output stream, row by row as
in a PPM. You must implement the pixmap$pixels_dump template
function. (We are anticipating the task to write a PPM file, and
wish to do it in a nice way. I am likely to end up actually using
this code, after all.) *)
{w, h : int}
(* I return a success-or-failure value, to avoid committing to using
an exception here. There are circumstances in which exceptions are
not the best approach. *)
(FILEref, !pixmap (a, w, h)) -> bool (* success *)
 
fn {a : t@ype}
pixmap$pixels_dump :
(* A function that the writes n pixels to an output stream. (It
could be one pixel, it could be the entire image. From the user's
standpoint, it makes no difference. It is an implementation
detail HOW the function is called by pixmap_dump.) *)
{n : int}
(FILEref, &array (a, n), size_t n) -> bool (* success *)
 
fn {a : t@ype}
pixmap_load :
(* Load the contents of a pixmap from an input stream, row by row as
in a PPM. You must implement the pixmap$pixels_load template
function. A value of type a has to be given, to initialize the
array with if the loading fails. *)
{w, h : int} {p : addr}
(FILEref, !pixmap (a?, w, h, p) >> pixmap (a, w, h, p), a) ->
bool (* success *)
 
fn {a : t@ype}
pixmap$pixels_load :
(* A function that the reads n pixels from an input stream. (It
could be one pixel, it could be the entire image. From the user's
standpoint, it makes no difference. It is an implementation
detail HOW the function is called by pixmap_load.) *)
{n : int}
(FILEref, &array (a?, n) >> array (a, n), size_t n, a) ->
bool (* success *)
 
overload pixmap_make with pixmap_make_array
overload pixmap_make with pixmap_make_uninitized
overload pixmap_make with pixmap_make_elt
 
overload pixmap_free with pixmap_free_storage_return
overload pixmap_free with pixmap_free_storage_free
overload free with pixmap_free_storage_free
 
overload fill with pixmap_fill_elt
 
overload pixmap_set_at with pixmap_set_at_guint
overload pixmap_set_at with pixmap_set_at_gint
overload [] with pixmap_set_at
 
overload pixmap_get_at with pixmap_get_at_guint
overload pixmap_get_at with pixmap_get_at_gint
overload [] with pixmap_get_at
 
overload dump with pixmap_dump
overload load with pixmap_load
 
overload width with pixmap_width
overload height with pixmap_height
 
(*------------------------------------------------------------------*)
(* Here is a type for 24-bit RGB data. An RGB pixmap type thus can be
written as "pixmap (rgb24, w, h, p)".
There are, though you cannot see it here (they are in the dynamic
file), default implementations of pixmap$pixels_dump<rgb24> and
pixmap$pixels_load<rgb24>. These implementations are for dumping
raw data in PPM format. *)
 
(* It is an abstract type, the size of a triple of uint8. (It is, in
fact, a triple of uint8, but we hide this fact, so the template
system will not confuse the type with other triples of uint8. It is
a subtle matter. *)
abst@ype rgb24 = @(uint8, uint8, uint8)
 
fn {tk : tkind}
rgb24_make_uint_uint_uint :
(g0uint tk, g0uint tk, g0uint tk) -<> rgb24
 
fn {tk : tkind}
rgb24_make_int_int_int :
(g0int tk, g0int tk, g0int tk) -<> rgb24
 
fn {}
rgb24_make_tuple : @(uint8, uint8, uint8) -<> rgb24
 
fn {}
rgb24_values : rgb24 -<> @(uint8, uint8, uint8)
 
overload rgb24_make with rgb24_make_uint_uint_uint
overload rgb24_make with rgb24_make_int_int_int
overload rgb24_make with rgb24_make_tuple
 
(*------------------------------------------------------------------*)
</syntaxhighlight>
 
===The ATS dynamic file===
This file should be called <code>bitmap_task.dats</code>.
<syntaxhighlight lang="ats">
(*------------------------------------------------------------------*)
 
#define ATS_DYNLOADFLAG 0
#define ATS_PACKNAME "Rosetta_Code.bitmap_task"
 
#include "share/atspre_staload.hats"
 
staload "bitmap_task.sats"
 
(*------------------------------------------------------------------*)
 
(* The actual type, normally not seen by the user, is a boxed
record. *)
datavtype _pixmap (a : t@ype, w : int, h : int, p : addr) =
| _pixmap of
@{
pf = array_v (a, p, w * h) |
w = size_t w,
h = size_t h,
p = ptr p
}
 
(* Here is one of the ways to tie an abstract type to its
implementation: *)
assume pixmap (a, w, h, p) = _pixmap (a, w, h, p)
(* Another way is to use casts. *)
 
(*------------------------------------------------------------------*)
 
implement {}
pixmap_width pix =
case+ pix of _pixmap record => record.w
 
implement {}
pixmap_height pix =
case+ pix of _pixmap record => record.h
 
implement {a}
pixmap_make_array (pf | w, h, p) =
_pixmap @{pf = pf | w = w, h = h, p = p}
 
implement {a}
pixmap_unmake pix =
case+ pix of
| ~ _pixmap @{pf = pf | w = w, h = h, p = p} => @(pf | w, h, p)
 
primplement
pixmap_prove_index_bounds {w, h} {x, y} () =
let
prval () = mul_gte_gte_gte {y, w} ()
prval () = mul_gte_gte_gte {h - (y + 1), w} ()
in
end
 
implement {a}
pixmap_make_uninitized {w, h} (w, h) =
let
prval () = lemma_g1uint_param w (* Proves w >= 0. *)
prval () = lemma_g1uint_param h (* Proves h >= 0. *)
prval () = mul_gte_gte_gte {w, h} () (* Proves w*h >= 0. *)
 
val @(pf, pfgc | p) = array_ptr_alloc<a> (w * h)
val pix = pixmap_make<a?> (pf | w, h, p)
in
@(pfgc | pix)
end
 
implement {a}
pixmap_make_elt (w, h, elt) =
let
val @(pfgc | pix) = pixmap_make<a> (w, h)
in
fill<a> (pix, elt);
@(pfgc | pix)
end
 
implement {}
pixmap_free_storage_return pix =
case+ pix of
| ~ _pixmap record => @(record.pf | record.p)
 
implement {}
pixmap_free_storage_free (pfgc | pix) =
let
val @(pf | p) = pixmap_free pix
in
array_ptr_free (pf, pfgc | p)
end
 
implement {a}
pixmap_fill_elt {w, h} {p} (pix, elt) =
case+ pix of
| @ _pixmap record =>
let
prval () = lemma_g1uint_param (record.w)
prval () = lemma_g1uint_param (record.h)
prval () = mul_gte_gte_gte {w, h} ()
stadef n = w * h
val n : size_t n = record.w * record.h
and p : ptr p = record.p
 
fun
loop {i : nat | i <= n}
.<n - i>.
(pf_lft : array_v (a, p, i),
pf_rgt : array_v (a?, p + (i * sizeof a), n - i) |
i : size_t i)
: @(array_v (a, p, n) | ) =
if i = n then
let
prval () = array_v_unnil pf_rgt
in
@(pf_lft | )
end
else
let
prval @(pf_elt, pf_rgt) = array_v_uncons pf_rgt
val () = ptr_set<a> (pf_elt | ptr_add<a> (p, i), elt)
prval pf_lft = array_v_extend (pf_lft, pf_elt)
in
loop (pf_lft, pf_rgt | succ i)
end
 
val @(pf | ) = loop (array_v_nil (), record.pf | i2sz 0)
prval () = record.pf := pf
prval () = fold@ pix
in
end
 
implement {a} {tk}
pixmap_set_at_guint {w, h} {x, y} (pix, x, y, elt) =
case+ pix of
| @ _pixmap record =>
let
prval () = lemma_g1uint_param x
prval () = lemma_g1uint_param y
 
stadef n = w * h
stadef i = x + (y * w)
 
prval () = pixmap_prove_index_bounds {w, h} {x, y} ()
prval () = prop_verify {0 <= i && i < n} ()
 
(* I purposely store the data in an order such that you can
write something such as a PPM without looping separately
over x and y. Also, even if you did do an outer loop over y
and an inner loop over x, you would get the advantage of
data locality. *)
val i : size_t i = g1u2u x + (g1u2u y * record.w)
macdef pixels = !(record.p)
val () = pixels[i] := elt
 
prval () = fold@ pix
in
end
 
implement {a} {tk}
pixmap_set_at_gint (pix, x, y, elt) =
pixmap_set_at_guint<a><sizeknd> (pix, g1i2u x, g1i2u y, elt)
 
implement {a} {tk}
pixmap_get_at_guint {w, h} {x, y} (pix, x, y) =
case+ pix of
| @ _pixmap record =>
let
prval () = lemma_g1uint_param x
prval () = lemma_g1uint_param y
 
stadef n = w * h
stadef i = x + (y * w)
 
prval () = pixmap_prove_index_bounds {w, h} {x, y} ()
prval () = prop_verify {0 <= i && i < n} ()
 
val i : size_t i = g1u2u x + (g1u2u y * record.w)
macdef pixels = !(record.p)
val elt = pixels[i]
 
prval () = fold@ pix
in
elt
end
 
implement {a} {tk}
pixmap_get_at_gint (pix, x, y) =
pixmap_get_at_guint<a><sizeknd> (pix, g1i2u x, g1i2u y)
 
implement {a}
pixmap_dump (outf, pix) =
case+ pix of
| @ _pixmap record =>
let
macdef pixels = !(record.p)
val n = record.w * record.h
val success = pixmap$pixels_dump<a> (outf, pixels, n)
prval () = fold@ pix
in
success
end
 
implement {a}
pixmap_load (inpf, pix, elt) =
case+ pix of
| @ _pixmap record =>
let
macdef pixels = !(record.p)
val n = record.w * record.h
val success = pixmap$pixels_load<a> (inpf, pixels, n, elt)
prval () = fold@ pix
in
success
end
 
(*------------------------------------------------------------------*)
 
typedef FILEstar = $extype"FILE *"
extern castfn FILEref2star : FILEref -<> FILEstar
 
implement
pixmap$pixels_dump<rgb24> (outf, pixels, n) =
let
val num_written =
$extfcall (size_t, "fwrite", addr@ pixels, sizeof<rgb24>, n,
FILEref2star outf)
in
num_written = n
end
 
implement
pixmap$pixels_load<rgb24> (inpf, pixels, n, elt) =
let
prval [n : int] EQINT () = eqint_make_guint n
val num_read =
$extfcall (size_t, "fread", addr@ pixels, sizeof<rgb24>, n,
FILEref2star inpf)
in
if num_read = n then
let
prval () = $UNSAFE.castvwtp2void{@[rgb24][n]} pixels
in
true
end
else
begin
array_initize_elt<rgb24> (pixels, n, elt);
false
end
end
 
(*------------------------------------------------------------------*)
 
assume rgb24 = @(uint8, uint8, uint8)
 
implement {tk}
rgb24_make_uint_uint_uint (r, g, b) =
let
(* The prelude tends to miss implementations for type conversions
to uint8, so let us at least implement conversion from uint to
uint8. (I do not wish to use a general unsafe cast, because
that sort of code has caused me bugs before. C does not always
know how to do a type conversion correctly.) The ats2-xprelude
package has a much more complete set of implementations
(generated en masse by m4 macros), but for this task I am
avoiding such dependencies. *)
implement
g0uint2uint<uintknd,uint8knd> i =
let
extern castfn g0uint2uint_uint_uint8 : uint -<> uint8
in
g0uint2uint_uint_uint8 i
end
in
rgb24_make_tuple @(g0u2u r, g0u2u g, g0u2u b)
end
 
implement {tk}
rgb24_make_int_int_int (r, g, b) =
let
(* See the comment in rgb24_make_uint_uint_uint. *)
implement
g0int2uint<intknd,uint8knd> i =
let
extern castfn g0int2uint_int_uint8 : int -<> uint8
in
g0int2uint_int_uint8 i
end
in
rgb24_make @(g0i2u r, g0i2u g, g0i2u b)
end
 
implement {}
rgb24_make_tuple tup = tup
 
implement {}
rgb24_values rgb = rgb
 
(*------------------------------------------------------------------*)
 
#ifdef BITMAP_TASK_TEST #then
 
%{^
#include <limits.h>
%}
 
fn
test_sizeof_rgb24 () : void =
(* We want to be sure rgb24 takes up exactly 24 bits. Our dump and
load implementations depend on that. (If it prove not the case on
some platform, one can write, for that unanticipated platform,
special implementations of dump and load.) *)
let
val- true = sizeof<rgb24> = i2sz 3
val- true = sizeof<rgb24> * $extval (size_t, "CHAR_BIT") = i2sz 24
in
end
 
fn
test_pixel_load_copy_dump () : void =
(* Test loading, copying, and dumping of raw 24-bit RGB data from
SIPI image "Peppers", 4.2.07.tiff:
https://sipi.usc.edu/database/database.php?volume=misc&image=13#top
I have the data stored as "4.2.07.raw". *)
let
val failure_color = rgb24_make (0xFF, 0x00, 0x00)
 
val @(pfgc1 | pix1) = pixmap_make<rgb24> (i2sz 512, i2sz 512)
val inpf = fileref_open_exn ("4.2.07.raw", file_mode_r)
val success = load<rgb24> (inpf, pix1, failure_color)
val () = fileref_close inpf
val- true = success
 
val @(pfgc2 | pix2) = pixmap_make<rgb24> (i2sz 512, i2sz 512,
failure_color)
fun
copy_pixels {x, y : nat | x <= 512; y <= 512}
.<512 - x, 512 - y>.
(pix1 : !pixmap (rgb24, 512, 512),
pix2 : !pixmap (rgb24, 512, 512),
x : int x,
y : int y) : void =
if x = 512 then
()
else if y = 512 then
copy_pixels (pix1, pix2, succ x, 0)
else
begin
pix2[x, y] := pix1[x, y];
copy_pixels (pix1, pix2, x, succ y)
end
val () = copy_pixels (pix1, pix2, 0, 0)
 
val outf = fileref_open_exn ("4.2.07.raw.dumped", file_mode_w)
val success = dump<rgb24> (outf, pix2)
val () = fileref_close outf
val- true = success
 
val status = $extfcall (int, "system",
"cmp 4.2.07.raw 4.2.07.raw.dumped")
val- true = status = 0
in
free (pfgc1 | pix1);
free (pfgc2 | pix2)
end
 
implement
main0 () =
begin
test_sizeof_rgb24 ();
test_pixel_load_copy_dump ()
end
 
#endif
 
(*------------------------------------------------------------------*)
</syntaxhighlight>
 
A test can be run if one has a 786432-byte file and names it <code>4.2.07.raw</code>. I used the raw data from a commonly used 512x512 test image. You can compile and run the test program thus:
<pre>$ patscc -std=gnu2x -g -O2 -DATS_MEMALLOC_LIBC -DATS BITMAP_TASK_TEST bitmap_task.sats bitmap_task.dats
$ ./a.out</pre>
You should end up with a copy of the data in a file named <code>4.2.07.raw.dumped</code>.
 
=={{header|AutoHotkey}}==
{{works with|AutoHotkey_L}}
Line 516 ⟶ 1,161:
return clr.R << 16 | clr.G << 8 | clr.B
}</syntaxhighlight>
 
=={{header|Axe}}==
All of the functions specified in the task are built in to Axe. Note that bitmaps are always 96x64 black and white. Thus, since each pixel takes 1 bit, a complete bitmap is 768 bytes.
Line 531 ⟶ 1,177:
 
Disp pxl-Test(50,50,Pic1)▶Dec,i</syntaxhighlight>
 
=={{header|BASIC256}}==
=={{header|BASIC}}==
==={{header|BASIC256}}===
[[Image:BASIC256_bitmap.png|right]]
<syntaxhighlight lang="basic256">graphsize 30,30
Line 554 ⟶ 1,202:
<pre>pixel 10,10 is 4278255615
pixel 20,20 is 4294901760</pre>
 
=={{header|BBC BASIC}}==
==={{header|BBC BASIC}}===
{{works with|BBC BASIC for Windows}}
BBC BASIC expects a bitmap always to be associated with a window;
Line 1,218 ⟶ 1,867:
bmp.Free;
end.</syntaxhighlight>
 
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
This is a pure Delphi example using standard Delphi controls and libraries that already have raster and bitmap objects and operations built in.
 
Bascially, Delphi has a powerful set of tools for manipulating graphic objects. All graphic objects including Screens, Printers, Windows or Bitmaps, have a property called a "Canvas." As a consequence, the same code can draw on the Screen, the Printer, a Window or a Bitmap. A Canvas has powerful function that allow you to draw pixels, lines, rectangles, circles, elispes, polygons and text on any graphic object. The code below demonstrates many of the function available in a canvas.
[[File:DelphiBitmaps.png|frame|none]]
<syntaxhighlight lang="Delphi">
 
procedure ShowBitmapFunctions(Image: TImage);
{Code to demonstrate some of the main features the Delphi "TCanvas" object}
var I,X,Y: integer;
var C: TColor;
begin
{Draw red rectangle with 3 pixels wide lines}
Image.Canvas.Pen.Color:=clRed;
Image.Canvas.Pen.Width:=3;
Image.Canvas.Rectangle(50,50,500,300);
{Flood fill rectangle blue}
Image.Canvas.Brush.Color:=clBlue;
Image.Canvas.FloodFill(55,55,clRed,fsBorder);
{Draw random dots on the screen}
for I:=1 to 1000 do
begin
X:=trunc((Random * 450) + 50);
Y:=trunc((Random * 250) + 50);
C:=RGB(Random(255),Random(255),Random(255));
{draw 9 pixels for each point to make dots more visible}
Image.Canvas.Pixels[X-1,Y-1]:=C;
Image.Canvas.Pixels[X ,Y-1]:=C;
Image.Canvas.Pixels[X+1,Y-1]:=C;
Image.Canvas.Pixels[X-1,Y ]:=C;
Image.Canvas.Pixels[X ,Y ]:=C;
Image.Canvas.Pixels[X+1,Y ]:=C;
Image.Canvas.Pixels[X-1,Y+1]:=C;
Image.Canvas.Pixels[X ,Y+1]:=C;
Image.Canvas.Pixels[X+1,Y+1]:=C;
end;
{Draw lime-green line from corner to cornder}
Image.Canvas.Pen.Color:=clLime;
Image.Canvas.MoveTo(50,50);
Image.Canvas.LineTo(500,300);
{Sample pixel color at 51,51}
C:=Image.Canvas.Pixels[51,51];
{Display the color value }
Image.Canvas.Brush.Color:=clAqua;
Image.Canvas.Font.Size:=25;
Image.Canvas.Font.Color:=clRed;
Image.Canvas.TextOut(5,5,IntToHex(C,8));
{Tell Delphi to update the Window}
Image.Repaint;
end;
 
</syntaxhighlight>
{{out}}
<pre>
 
Elapsed Time: 39.038 ms.
 
</pre>
 
=={{header|E}}==
 
Line 2,922 ⟶ 3,634:
Use getPixels to retrieve the colour of a pixel. As with setPixels, this function is optimised to retrieve one row at a time as an array of colour values.
<syntaxhighlight lang="maxscript">local myPixel = getPixels myBitmap [256, 256] 1</syntaxhighlight>
 
=={{header|MiniScript}}==
This GUI implementation is for use with [http://miniscript.org/MiniMicro Mini Micro].
<syntaxhighlight lang="miniscript">
// MiniMicro version of MiniScript has all the
// necessary methods built-in to complete this task.
width = 256
height = 256
colr = color.aqua
 
// Create the image with specified width/heigh. With
// no parameters, it defaults width/height to 64 and
// color to black
img = Image.create(width, height, colr)
 
// Create a diagonal line of multiple colors. Uses
// Cartesian coordinates so (0, 0) is lower left corner.
for i in range(0, 255)
img.setPixel i, i, color.rgb(i, i, i)
end for
 
// Get pixel color as RGBA hex values
print "Color at pixel (100, 100): " + img.pixel(100, 100)
print "Color at pixel (0, 0): " + img.pixel(0, 0)
print "Color at pixel (127, 127): " + img.pixel(127, 127)
print "Color at pixel (255, 255): " + img.pixel(255, 255)
 
// Display the image, resizing it to 127 x 127
gfx.drawImage img, 0, 0, 127, 127
 
// Save the file - accepted file extensions:
// tga, jpg, jpeg, and png (retains transparency)
// Optional third parameter is JPG compression quality.
file.saveImage "/usr/test.png", img
</syntaxhighlight>
 
=={{header|Modula-3}}==
Since this code is for use with other tasks, it uses an interface as well as the implementation module.
Line 3,685 ⟶ 4,433:
=={{header|Python}}==
See [[Basic bitmap storage/Python]]
 
=={{header|QBasic}}==
{{works with|QBasic|1.1}}
<syntaxhighlight lang="qbasic">SUB establecePixel (x AS INTEGER, y AS INTEGER, c AS INTEGER)
PSET (x, y), cyan
END SUB
 
SUB rellenar (c AS INTEGER)
SHARED w, h
LINE (0, 0)-(w / 3, h / 3), red, BF
END SUB
 
SCREEN 13
w = 320: h = 200
CONST cyan = 3, red = 4
 
rellenar (12)
CALL establecePixel(10, 10, cyan)
LOCATE 12
PRINT "pixel 10,10 is "; POINT(10, 10)
PRINT "pixel 20,20 is "; POINT(20, 10)</syntaxhighlight>
 
=={{header|R}}==
{{libheader|pixmap}}
Line 3,823 ⟶ 4,593:
 
The image (raster) created was also written to a file &nbsp; ('''image.PPM''') &nbsp; to show verification of the image.
<syntaxhighlight lang="rexx">/*REXX program demonstrates how to process/display a simple RGB raster graphics image. */
red/* = 'ff 00 00'x /*a methodsimple to defineRGB a raster graphics red valueimage. */
bluered = 'ff 00 00 ff'x /*" " " " "a method to bluedefine a "red value. */
@. blue = '00 00 ff'x /*' ' ' ' ' blue ' /*define entire @. array to nulls. */
outFN pixel= 'image' /*the filenamedefine ofentire the outputpixel. imagearray PPMto nulls.*/
sWidthoutFN = 500; sHeight= 500 'image' /*the screenfilename widthof the andoutput heightimage inPPM pixels*/
sWidth = 500; sHeight= 500 /*the screen width and height in pixels*/
call RGBfill red /*set the entire image to red. */
Call RGBfill x= 10; y= 40 red /*set pixel's coördinates.the entire image to red. */
call RGBset x,=10; y,=40 blue /*set pixel's coördinates. /*set a pixel (at 10,40) to blue. */
colorCall =RGBset RGBget(x, y) ,blue /*get the color ofset a pixel. (at 10,40) to blue. */
hexV color = c2xRGBget(colorx,y) /*get the color of a pixel. /*get hex value of pixel's color. */
binVhexV = x2bc2x(hexVcolor) /*get " binary " " " hex "value of pixel's color. */
binV = x2b(hexV) /* " binary " " " " */
bin3V = left(binV, 8) substr(binV, 9, 8) right(binV, 8)
hex3Vbin3V = left(hexVbinV, 28) substr(hexVbinV, 39, 28) right(hexVbinV, 28)
hex3V = left(hexV,2) substr(hexV,3,2) right(hexV,2)
xy= '(' || x","y')' /*create a handy─dandy literal for SAY.*/
say xy= '('||x','y')' pixel in binary: ' binV /*show the binary value of 20,50 create a handy-dandy literal for SAY.*/
say Say xy ' pixel in binary: ' bin3V binV /*show again, but with spaces. 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. */
saySay xy ' pixel in hex: ' hexV /*show again,a butblank in hexadecimal.between bin & hex. */
say Say xy ' pixel in hex: ' hex3V hexV /*show again, but within spaceshexadecimal. */
Say xy ' pixel in hex: ' hex3V /*show again,but with spaces. */
call PPMwrite outFN, sWidth, sHeight /*create a PPM (output) file of image. */ /* ◄■■■■■■■■ not part of this task.*/
Call PPMwrite outFN,sWidth,sHeight /*create a PPM (output) file */
say /*show a blank. */
/* ?¦¦¦¦¦¦¦¦ not part of this task.*/
say 'The file ' outFN".PPM was created." /*inform user that a file was created. */
exitSay /*show a blank. /*stick a fork in it, we're all done. */
Say 'The file ' outFN'.PPM was created.' /*inform user */
/*──────────────────────────────────────────────────────────────────────────────────────*/
RGBfill:Exit @.=arg(1); return /*stick a fork in it, we're all done. /*fill image with a color.*/
/*---------------------------------------------------------------------*/
RGBget: parse arg px,py; return @.px.py /*get a pixel's color. */
RGBsetRGBfill: parse pixel.=arg px,py,p$(1); @.px.py=p$; Return return /*set " " " fill image with a color.*/
RGBget: Parse arg px,py; Return pixel.px.py /*get a pixel's color. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
RGBset: Parse arg px,py,psep; pixel.px.py=psep; Return /*set a pixel */
PPMwrite: parse arg oFN, width, height /*obtain output filename, width, height*/
/*---------------------------------------------------------------------*/
oFID= oFN'.PPM'; $='9'x; #=255 /*fileID; separator; max color value.*/
PPMwrite: Parse arg oFN,width,height
call charout oFID, , 1 /*set the position of the file's output*/
oFID= oFN'.PPM' /* fileID call charout oFID,'P6'width || $ || height || $ || # || $ /*write hdr info.*/
sep='9'x; /* separator do j=1 for width*/
maxcol=255 do k=1 for height; /* callmax charoutcolor oFID, @value.j.k */
Call charout oFID,,1 end /*k*/set the position /* ↑ writeof the PPM file, ···'s output*/
Call charout oFID,'P6'width||sep||height||sep||maxcol||sep /* header */
end /*j*/ /* └───────── ··· one pixel at a time.*/
Do i=1 To width
call charout oFID; return /*close the output file just to be safe*/</syntaxhighlight>
Do j=1 To height;
Call charout oFID,pixel.i.j
End
End
Call charout oFID /* close the output file just to be safe */
Return</syntaxhighlight>
{{out|output}}
<pre>
Line 4,600 ⟶ 5,376:
{{libheader|DOME}}
The above library's ImageData class fits the bill here (version 1.3.0 or later).
<syntaxhighlight lang="ecmascriptwren">import "graphics" for Canvas, ImageData, Color
import "dome" for Window
 
Line 4,639 ⟶ 5,415:
Color (#29ADFFFF)
</pre>
 
=={{header|Xojo}}==
 
9,482

edits