1 { Copyright (C) 2005 Bas Steendijk and Peter Green
\r
2 For conditions of distribution and use, see copyright notice in zlib_license.txt
\r
3 which is included in the package
\r
4 ----------------------------------------------------------------------------- }
\r
6 //main unit for graph drawing demo
\r
8 //draws a sine and cosine graph into a 2 bit per pixel
\r
10 //Copies it into a truecolor tbitmap (that is visible on the main form).
\r
11 //saves the graph from the original buffer to a 2 bit palleted png
\r
12 //saves the graph from the original buffer to a 2 bit greyscale png
\r
13 //saves the graph from the tbitmap into a 24 bit truecolor bitmap
\r
20 Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
\r
21 ExtCtrls,pngwrite,pngwritetbitmap;
\r
24 TForm1 = class(TForm)
\r
26 procedure FormCreate(Sender: TObject);
\r
28 { Private declarations }
\r
30 { Public declarations }
\r
35 pal : array[0..11] of byte;
\r
41 tlinedata = array[word] of byte;
\r
42 plinedata = ^tlinedata;
\r
43 timagedata = array[word] of plinedata;
\r
44 pimagedata = ^timagedata;
\r
46 imagedata : pimagedata;
\r
47 procedure TForm1.FormCreate(Sender: TObject);
\r
52 bitmapscanline : plinedata;
\r
53 tempfloat : extended;
\r
54 currentindex : integer;
\r
57 stream : tfilestream;
\r
79 imagedata := allocmem(image1.height*sizeof(plinedata));
\r
80 image1.width := (image1.width div 4)*4;
\r
81 image1.width := image1.width;
\r
82 for counter := 0 to image1.height-1 do begin
\r
84 imagedata[counter] := allocmem(image1.width div 4);
\r
85 imagedata[counter][image1.width div 8] := imagedata[counter][image1.width div 8] or $C0;
\r
88 fillchar(imagedata[image1.height div 2]^,image1.width div 4,#$FF);
\r
89 for counter := 0 to image1.width-1 do begin
\r
91 for fracpart := 0 to 15 do begin
\r
92 y := round( -sin((( (counter+(fracpart/15)) / image1.width){+0.5}) *2*pi ) *((image1.height-20)div 2) )+((image1.height)div 2);
\r
94 imagedata[y][counter div 4] := (imagedata[y][counter div 4]) or (1 shl (((counter and $3)xor$3)*2) );
\r
96 y := round( -cos((( (counter+(fracpart/15)) / image1.width){+0.5}) *2*pi ) *((image1.height-20)div 2) )+((image1.height)div 2);
\r
98 imagedata[y][counter div 4] := (imagedata[y][counter div 4]) or (2 shl (((counter and $3)xor$3)*2) );
\r
101 image1.Picture.Bitmap.PixelFormat :=pf24bit;
\r
102 image1.picture.bitmap.width := image1.width;
\r
103 image1.picture.bitmap.height := image1.height;
\r
106 for counter := 0 to image1.height-1 do begin
\r
107 bitmapscanline := image1.picture.bitmap.ScanLine[counter];
\r
108 for inner := 0 to image1.width-1 do begin
\r
110 currentindex := (imagedata[counter][inner div 4] shr (((inner and $3)xor$3)*2) ) and $3;
\r
111 //if (counter=0) and (imagedata[counter][inner div 4] <> 0) then begin
\r
112 // writeln(imagedata[counter][inner div 4]);
\r
113 // writeln(currentindex);
\r
116 bitmapscanline[(inner*3) ] := pal[(currentindex*3)+2];
\r
117 bitmapscanline[(inner*3)+1] := pal[(currentindex*3)+1];
\r
118 bitmapscanline[(inner*3)+2] := pal[(currentindex*3) ];
\r
124 stream := tfilestream.Create('truecolor.png',fmCreate{fmOpenWrite} or fmShareDenyNone );
\r
126 savetbitmaptopng(image1.picture.Bitmap,stream);
\r
131 stream := tfilestream.Create('4grey.png',fmCreate{fmOpenWrite} or fmShareDenyNone );
\r
133 pngstart(f,stream,2,ctgreyscale ,image1.picture.Bitmap.Height,image1.Picture.Bitmap.Width);
\r
136 for counter := 0 to image1.picture.Bitmap.Height-1 do begin
\r
137 pngwritescanline(f,imagedata[counter]);
\r
145 stream := tfilestream.Create('4color.png',fmCreate{fmOpenWrite} or fmShareDenyNone );
\r
147 pngstart(f,stream,2,ctpallette ,image1.picture.Bitmap.Height,image1.Picture.Bitmap.Width);
\r
148 pngwritepal(f,@pal,4);
\r
151 for counter := 0 to image1.picture.Bitmap.Height-1 do begin
\r
152 pngwritescanline(f,imagedata[counter]);
\r