Skip to content

Commit

Permalink
javascript array export options
Browse files Browse the repository at this point in the history
use javascript createImageData and putImageData to display generated code
  • Loading branch information
RetroNick2020 committed Jul 4, 2021
1 parent 6120b98 commit c7bdb0e
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 5 deletions.
3 changes: 1 addition & 2 deletions RM.lpi
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,8 @@
<IsPartOfProject Value="True"/>
</Unit10>
<Unit11>
<Filename Value="rmamigarwxgf.pas"/>
<Filename Value="wjavascriptarray.pas"/>
<IsPartOfProject Value="True"/>
<UnitName Value="RMAmigaRWXGF"/>
</Unit11>
</Units>
</ProjectOptions>
Expand Down
2 changes: 1 addition & 1 deletion rmabout.pas
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ interface
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls,lclintf;

Const
ProgramName ='Raster Master v1.0 Beta R13';
ProgramName ='Raster Master v1.0 Beta R14';
ProgramLicense = 'Released under MIT License';

type
Expand Down
11 changes: 11 additions & 0 deletions rmmain.lfm
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,17 @@ object RMMainForm: TRMMainForm
end
object MenuItem9: TMenuItem
Caption = 'Export'
object JavaScriptArray: TMenuItem
Caption = 'JavaScript Array'
object TransparentImage: TMenuItem
Caption = 'Transparent Image'
OnClick = javaScriptArrayClick
end
object NonTransparentImage: TMenuItem
Caption = 'Non Transparent Image'
OnClick = javaScriptArrayClick
end
end
object QBasicData: TMenuItem
Caption = 'QuickBasic\QB64 DATA'
OnClick = QBasicDataClick
Expand Down
35 changes: 33 additions & 2 deletions rmmain.pas
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ interface
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
StdCtrls, ComCtrls, Menus, ActnList, StdActns, ColorPalette, Types,
LResources,lclintf, RMTools, RMCore,RMColor,RMColorVGA,RMAmigaColor,
rmabout,RWPAL,RWRAW,RWPCX,RWBMP,RWXGF,WCON,flood,RMAmigaRWXGF,RMThumb;
rmabout,RWPAL,RWRAW,RWPCX,RWBMP,RWXGF,WCON,flood,RMAmigaRWXGF,wjavascriptarray,RMThumb;


type
Expand Down Expand Up @@ -38,6 +38,9 @@ TRMMainForm = class(TForm)
EditResizeTo32: TMenuItem;
EditResizeTo64: TMenuItem;
EditClear: TMenuItem;
JavaScriptArray: TMenuItem;
TransparentImage: TMenuItem;
NonTransparentImage: TMenuItem;
SaveDelete: TMenuItem;
Panel2: TPanel;
ToolEllipseMenu: TMenuItem;
Expand Down Expand Up @@ -151,6 +154,7 @@ TRMMainForm = class(TForm)
procedure FreePascalConstClick(Sender: TObject);
procedure GWBASICClick(Sender: TObject);
procedure EditResizeToNewSize(Sender: TObject);
procedure javaScriptArrayClick(Sender: TObject);
procedure ListView1DblClick(Sender: TObject);
procedure PaletteEditColors(Sender: TObject);
procedure SaveDeleteClick(Sender: TObject);
Expand Down Expand Up @@ -1975,8 +1979,34 @@ procedure TRMMainForm.EditResizeToNewSize(Sender: TObject);
UpdateThumbView;
end;

procedure TRMMainForm.javaScriptArrayClick(Sender: TObject);
var
x,y,x2,y2 : integer;
// pm : integer;
// sourcemode : word;
ext : string;
error : word;
transparent : boolean;
begin
transparent:=true;
if (Sender As TMenuItem).Name = 'NonTransparentImage' then
begin
transparent:=false;
end;


ExportDialog.Filter := 'JavaScript Array|*.jsa';
GetOpenSaveRegion(x,y,x2,y2);
if ExportDialog.Execute then
begin
ext:=UpperCase(ExtractFileExt(ExportDialog.Filename));
error:=WriteJavaScriptArray(x,y,x2,y2,ExportDialog.FileName,transparent);
if (error<>0) then
begin
ShowMessage('Error Saving file!');
exit;
end;
end;
end;

Procedure TRMMainForm.EditColors;
var
Expand Down Expand Up @@ -2494,6 +2524,7 @@ procedure TRMMainForm.EditPasteClick(Sender: TObject);

UpdateActualArea;
UpdateZoomArea;
UpdateThumbView;
if clipstatus = 1 then
begin
RMDrawTools.DrawClipArea(ZoomBox.Canvas,ColorBox.brush.color,1);
Expand Down
143 changes: 143 additions & 0 deletions wjavascriptarray.pas
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
{$mode objfpc}{$H+}
{$PACKRECORDS 1}

Unit wjavascriptarray;
Interface
uses RMCore,SysUtils,FileUtil,Bits;

Function WriteJavaScriptArray(x,y,x2,y2 : word;filename:string; transparent : boolean):word;
Implementation

type
BufferRec = Record
fText : Text;
datalist : array[1..128] of Byte;
count : integer;
maxsize : longint;
bcount : longint;
end;

//Action 0 = init ncounter/buffer,Action 1 = write byte to buffer, action 2= flush buffer
ArrayWriterProc = Procedure(inByte : Byte; var Buffer : BufferRec; action : integer);



procedure ArrayWriter(inByte : Byte; var Buffer : BufferRec;action : integer);
var
i : integer;
begin
if action = 0 then
begin
buffer.Count:=0;
buffer.bcount:=0;
buffer.maxsize:=0;
end
else if action = 1 then
begin
inc(buffer.count);
buffer.datalist[buffer.count]:=inbyte;
if buffer.count = 10 then //every 10 bytes write to data statement
begin
//write the data statement
write(buffer.ftext,' ');
for i:=1 to 10 do
begin
write(buffer.ftext,buffer.datalist[i]);
inc(buffer.bcount);
if buffer.bcount<>buffer.maxsize then write(buffer.ftext,',');
end;
writeln(buffer.ftext);
buffer.count:=0;
end;
end
else if action = 2 then //write the remaining data
begin
if buffer.count > 0 then
begin
write(buffer.ftext,' ');
for i:=1 to buffer.count do
begin
write(buffer.ftext,buffer.datalist[i]);
inc(buffer.bcount);
if buffer.bcount<>buffer.maxsize then write(buffer.ftext,',');
end;
// writeln(buffer.ftext);
buffer.count:=0;
end;
end;
end;

//we emulator graph's getmaxcolor way of counting colors
function GetMaxColor : integer;
begin
GetMaxColor:=RMCoreBase.Palette.GetColorCount-1;
end;

function GetArraySize(width,height : integer) : longint;
begin
GetArraySize:=width*height*4;
end;

Function WriteJavaScriptArray(x,y,x2,y2 : word;filename:string; transparent : boolean):word;
var
width,height : integer;
data :BufferRec;
i,j : word;
ImageName : string;
asize : longint;
r,g,b,a : byte;
ColorIndex : integer;
begin
width:=x2-x+1;
height:=y2-y+1;
asize:=GetArraySize(width,height);

ArrayWriter(0,data,0); //init
data.maxsize:=asize;

Assign(data.ftext,filename);
{$I-}
Rewrite(data.ftext);

Imagename:=ExtractFileName(ExtractFileNameWithoutExt(filename));
writeln(data.ftext,'//',' JavaScript Array ',Imagename,' Size= ', asize,' Width= ',width,' Height= ',height, ' Colors= ',GetMaxColor+1);
writeln(data.ftext,'var ', Imagename,'Image ',' = new Uint8ClampedArray([');

for j:=y to y2 do
begin
for i:=x to x2 do
begin
colorIndex:=RMCoreBase.GetPixel(i,j);

r:=RMCoreBase.palette.GetRed(ColorIndex);
g:=RMCoreBase.palette.GetGreen(ColorIndex);
b:=RMCoreBase.palette.GetBlue(ColorIndex);
a:=255;
if transparent then
begin
if ColorIndex = 0 then a:=0;
if (r=255) and (g=0) and (b=255) then a:=0;
end;
ArrayWriter(r,data,1);
ArrayWriter(g,data,1);
ArrayWriter(b,data,1);
ArrayWriter(a,data,1);
end;
end;

ArrayWriter(0,data,2); //flush it
writeln(data.ftext,']);');

Close(data.ftext);
{$I+}
WriteJavaScriptArray:=IORESULT;
end;






begin
end.

0 comments on commit c7bdb0e

Please sign in to comment.