Bitmap: Difference between revisions

2,990 bytes added ,  1 month ago
m
(Added QBasic)
 
(8 intermediate revisions by 6 users not shown)
Line 20:
{{trans|Python}}
 
<syntaxhighlight lang="11l">T Colour = BVec3
Byte r, g, b
 
F (r, g, b)
.r = r
.g = g
.b = b
 
F ==(other)
R .r == other.r & .g == other.g & .b == other.b
 
V black = Colour(0, 0, 0)
Line 88 ⟶ 79:
+--------------------+
</pre>
 
=={{header|Action!}}==
Part of the solution can be found in [http://www.rosettacode.org/wiki/Category:Action!_Bitmap_tools#RGBIMAGE.ACT RGBIMAGE.ACT]
Line 462 ⟶ 454:
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}
Line 471 ⟶ 475:
(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}
Line 588 ⟶ 613:
(FILEref, &array (a?, n) >> array (a, n), size_t n, a) ->
bool (* success *)
 
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
 
overload pixmap_make with pixmap_make_array
Line 690 ⟶ 703:
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}
Line 769 ⟶ 805:
in
end
 
prfn
prove_index_bounds
{w, h : int}
{x, y : nat | x < w; y < h}
()
:<prf> [0 <= x + (y * w);
x + (y * w) < w * h]
void =
let
prval () = mul_gte_gte_gte {y, w} ()
prval () = mul_gte_gte_gte {h - (y + 1), w} ()
in
end
 
implement {a} {tk}
Line 795 ⟶ 817:
stadef i = x + (y * w)
 
prval () = prove_index_boundspixmap_prove_index_bounds {w, h} {x, y} ()
prval () = prop_verify {0 <= i && i < n} ()
 
Line 826 ⟶ 848:
stadef i = x + (y * w)
 
prval () = prove_index_boundspixmap_prove_index_bounds {w, h} {x, y} ()
prval () = prop_verify {0 <= i && i < n} ()
 
Line 903 ⟶ 925:
end
end
 
(*------------------------------------------------------------------*)
 
implement {}
pixmap_width pix =
case+ pix of _pixmap record => record.w
 
implement {}
pixmap_height pix =
case+ pix of _pixmap record => record.h
 
(*------------------------------------------------------------------*)
Line 1,847 ⟶ 1,859:
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 3,551 ⟶ 3,626:
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 4,474 ⟶ 4,585:
 
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 5,251 ⟶ 5,368:
{{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 5,290 ⟶ 5,407:
Color (#29ADFFFF)
</pre>
 
=={{header|Xojo}}==
 
1,481

edits