- Timestamp:
- Sep 22, 2014, 3:07:02 AM (10 years ago)
- Location:
- trunk
- Files:
-
- 1 added
- 10 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/ColorFormats/UColorGray1.pas
r9 r10 42 42 case Channel of 43 43 ccGray: Result := 0; 44 else raise Exception.Create('Unsupported color channel');44 else Result := 0; 45 45 end; 46 46 end; -
trunk/Forms/UFormMain.lfm
r7 r10 2 2 Left = 664 3 3 Height = 640 4 Top = 4434 Top = 382 5 5 Width = 920 6 6 Caption = 'LibrePaint' … … 8 8 ClientWidth = 920 9 9 Menu = MainMenu1 10 OnActivate = FormActivate 11 OnCreate = FormCreate 12 OnDestroy = FormDestroy 13 OnShow = FormShow 10 14 LCLVersion = '1.3' 11 15 object PaintBox1: TPaintBox … … 15 19 Width = 920 16 20 Align = alClient 21 OnMouseDown = PaintBox1MouseDown 22 OnMouseMove = PaintBox1MouseMove 23 OnMouseUp = PaintBox1MouseUp 24 OnMouseLeave = PaintBox1MouseLeave 25 OnMouseWheelDown = PaintBox1MouseWheelDown 26 OnMouseWheelUp = PaintBox1MouseWheelUp 17 27 OnPaint = PaintBox1Paint 18 28 OnResize = PaintBox1Resize … … 23 33 Top = 588 24 34 Width = 920 25 Panels = <> 35 Panels = < 36 item 37 Width = 50 38 end> 39 SimplePanel = False 26 40 end 27 41 object MainMenu1: TMainMenu … … 85 99 Action = Core.AImageFlip 86 100 end 101 object MenuItem19: TMenuItem 102 Action = Core.AImageGradient 103 end 87 104 end 88 105 end -
trunk/Forms/UFormMain.pas
r7 r10 7 7 uses 8 8 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Menus, 9 ExtCtrls, ComCtrls ;9 ExtCtrls, ComCtrls, types; 10 10 11 11 type … … 23 23 MenuItem17: TMenuItem; 24 24 MenuItem18: TMenuItem; 25 MenuItem19: TMenuItem; 25 26 MenuItemRecentFiles: TMenuItem; 26 27 MenuItem15: TMenuItem; … … 37 38 StatusBar1: TStatusBar; 38 39 Timer1: TTimer; 40 procedure FormActivate(Sender: TObject); 41 procedure FormCreate(Sender: TObject); 42 procedure FormDestroy(Sender: TObject); 43 procedure FormShow(Sender: TObject); 44 procedure PaintBox1MouseDown(Sender: TObject; Button: TMouseButton; 45 Shift: TShiftState; X, Y: Integer); 46 procedure PaintBox1MouseLeave(Sender: TObject); 47 procedure PaintBox1MouseMove(Sender: TObject; Shift: TShiftState; X, 48 Y: Integer); 49 procedure PaintBox1MouseUp(Sender: TObject; Button: TMouseButton; 50 Shift: TShiftState; X, Y: Integer); 51 procedure PaintBox1MouseWheelDown(Sender: TObject; Shift: TShiftState; 52 MousePos: TPoint; var Handled: Boolean); 53 procedure PaintBox1MouseWheelUp(Sender: TObject; Shift: TShiftState; 54 MousePos: TPoint; var Handled: Boolean); 39 55 procedure PaintBox1Paint(Sender: TObject); 40 56 procedure PaintBox1Resize(Sender: TObject); 41 57 procedure Timer1Timer(Sender: TObject); 42 58 private 43 { private declarations } 59 TempBitmap: TBitmap; 60 StartMousePoint: TPoint; 61 StartViewPoint: TPoint; 62 MoveActive: Boolean; 63 MousePos: TPoint; 64 Activated: Boolean; 44 65 public 66 procedure UpdateStatusBar; 45 67 procedure Redraw; 46 68 end; … … 60 82 procedure TFormMain.PaintBox1Resize(Sender: TObject); 61 83 begin 62 84 Redraw; 63 85 end; 64 86 65 87 procedure TFormMain.Timer1Timer(Sender: TObject); 66 var67 Bitmap: TBitmap;68 88 begin 69 89 Timer1.Enabled := False; 70 try 71 Bitmap := TBitmap.Create; 72 Bitmap.SetSize(Core.Project.Bitmap.Size.X, Core.Project.Bitmap.Size.Y); 73 Bitmap.BeginUpdate(True); 74 Core.Project.Bitmap.PaintToCanvas(Bitmap.Canvas); 75 Bitmap.EndUpdate(False); 76 PaintBox1.Canvas.Draw(0, 0, Bitmap); 77 finally 78 Bitmap.Free; 90 PaintBox1.Repaint; 91 UpdateStatusBar; 92 end; 93 94 procedure TFormMain.UpdateStatusBar; 95 var 96 Pos: TPoint; 97 begin 98 with Core.Project do begin 99 Pos := View.DestToSrcPos(MousePos); 100 StatusBar1.Panels[0].Text := '[' + IntToStr(Pos.X) + ', ' + IntToStr(Pos.Y) + '] Zoom:' + 101 FloatToStr(Core.Project.View.Zoom); 79 102 end; 80 103 end; … … 87 110 procedure TFormMain.PaintBox1Paint(Sender: TObject); 88 111 begin 89 Redraw; 112 with Core.Project do begin 113 TempBitmap.SetSize(View.SrcRect.Right - View.SrcRect.Left, 114 View.SrcRect.Bottom - View.SrcRect.Top); 115 TempBitmap.BeginUpdate(True); 116 TempBitmap.Canvas.Brush.Color := clBlack; 117 TempBitmap.Canvas.FillRect(0, 0, TempBitmap.Width, TempBitmap.Height); 118 View.DestRect := Bounds(0, 0, PaintBox1.Width, PaintBox1.Height); 119 Bitmap.PaintToCanvas(TempBitmap.Canvas, View.SrcRect); 120 TempBitmap.EndUpdate(False); 121 PaintBox1.Canvas.StretchDraw(View.DestRect, TempBitmap); 122 //PaintBox1.Canvas.Draw(0, 0, TempBitmap); 123 end; 124 end; 125 126 procedure TFormMain.FormCreate(Sender: TObject); 127 begin 128 TempBitmap := TBitmap.Create; 129 end; 130 131 procedure TFormMain.FormActivate(Sender: TObject); 132 begin 133 if not Activated then begin 134 Activated := True; 135 Core.Init; 136 end; 137 end; 138 139 procedure TFormMain.FormDestroy(Sender: TObject); 140 begin 141 TempBitmap.Free; 142 end; 143 144 procedure TFormMain.FormShow(Sender: TObject); 145 begin 146 end; 147 148 procedure TFormMain.PaintBox1MouseDown(Sender: TObject; Button: TMouseButton; 149 Shift: TShiftState; X, Y: Integer); 150 begin 151 if Button = mbLeft then begin 152 StartMousePoint := Point(X, Y); 153 StartViewPoint := Core.Project.View.SrcRect.TopLeft; 154 MoveActive := True; 155 end; 156 end; 157 158 procedure TFormMain.PaintBox1MouseLeave(Sender: TObject); 159 begin 160 MoveActive := False; 161 end; 162 163 procedure TFormMain.PaintBox1MouseMove(Sender: TObject; Shift: TShiftState; X, 164 Y: Integer); 165 begin 166 MousePos := Point(X, Y); 167 if Assigned(Core.Project) then begin 168 if MoveActive then 169 with Core.Project do begin 170 View.SrcRect := Bounds(Trunc(StartViewPoint.X + (StartMousePoint.X - X) / View.Zoom), 171 Trunc(StartViewPoint.Y + (StartMousePoint.Y - Y) / View.Zoom), 172 View.SrcRect.Right - View.SrcRect.Left, 173 View.SrcRect.Bottom - View.SrcRect.Top); 174 Redraw; 175 end; 176 end; 177 UpdateStatusBar; 178 end; 179 180 procedure TFormMain.PaintBox1MouseUp(Sender: TObject; Button: TMouseButton; 181 Shift: TShiftState; X, Y: Integer); 182 begin 183 MoveActive := False; 184 end; 185 186 procedure TFormMain.PaintBox1MouseWheelDown(Sender: TObject; 187 Shift: TShiftState; MousePos: TPoint; var Handled: Boolean); 188 begin 189 Core.AZoomOut.Execute; 190 end; 191 192 procedure TFormMain.PaintBox1MouseWheelUp(Sender: TObject; Shift: TShiftState; 193 MousePos: TPoint; var Handled: Boolean); 194 begin 195 Core.AZoomIn.Execute; 90 196 end; 91 197 -
trunk/Forms/UFormNew.pas
r8 r10 66 66 67 67 // Default 68 SpinEditWidth.Value := 800;69 SpinEditHeight.Value := 600;68 SpinEditWidth.Value := 200; 69 SpinEditHeight.Value := 100; 70 70 SpinEditDPI.Value := 72; 71 71 end; -
trunk/LibrePaint.lpi
r9 r10 71 71 </Item1> 72 72 </RequiredPackages> 73 <Units Count="1 0">73 <Units Count="11"> 74 74 <Unit0> 75 75 <Filename Value="LibrePaint.lpr"/> … … 92 92 <Filename Value="UProject.pas"/> 93 93 <IsPartOfProject Value="True"/> 94 <UnitName Value="UProject"/> 94 95 </Unit3> 95 96 <Unit4> … … 99 100 <HasResources Value="True"/> 100 101 <ResourceBaseClass Value="Form"/> 102 <UnitName Value="UFormNew"/> 101 103 </Unit4> 102 104 <Unit5> 103 105 <Filename Value="ColorFormats/UColorRGBA8.pas"/> 104 106 <IsPartOfProject Value="True"/> 105 <UnitName Value="UColorRGBA8"/>106 107 </Unit5> 107 108 <Unit6> … … 111 112 <HasResources Value="True"/> 112 113 <ResourceBaseClass Value="Form"/> 114 <UnitName Value="UFormMain"/> 113 115 </Unit6> 114 116 <Unit7> 115 117 <Filename Value="ColorFormats/UColorGray8.pas"/> 116 118 <IsPartOfProject Value="True"/> 117 <UnitName Value="UColorGray8"/>118 119 </Unit7> 119 120 <Unit8> 120 121 <Filename Value="ColorFormats/UColorGray1.pas"/> 121 122 <IsPartOfProject Value="True"/> 122 <UnitName Value="UColorGray1"/>123 123 </Unit8> 124 124 <Unit9> 125 125 <Filename Value="UMemory.pas"/> 126 126 <IsPartOfProject Value="True"/> 127 <UnitName Value="UMemory"/>128 127 </Unit9> 128 <Unit10> 129 <Filename Value="ColorFormats/UColorGray4.pas"/> 130 <IsPartOfProject Value="True"/> 131 <UnitName Value="UColorGray4"/> 132 </Unit10> 129 133 </Units> 130 134 </ProjectOptions> -
trunk/LibrePaint.lpr
r9 r10 9 9 Interfaces, // this includes the LCL widgetset 10 10 Forms, UCore, UGraphic, UProject, UBitStream, UMemory, UFormNew, UFormMain, 11 UColorRGBA8, UColorGray8, UColorGray1 11 UColorRGBA8, UColorGray8, UColorGray1, UColorGray4 12 12 { you can add units after this }; 13 13 -
trunk/UCore.lfm
r7 r10 39 39 Category = 'View' 40 40 Caption = 'Zoom all' 41 OnExecute = AZoomAllExecute 41 42 end 42 43 object AZoomNormal: TAction … … 76 77 OnExecute = AImageMirrorExecute 77 78 end 79 object AImageGradient: TAction 80 Category = 'Image' 81 Caption = 'Gradient' 82 OnExecute = AImageGradientExecute 83 end 78 84 end 79 85 object ImageList1: TImageList -
trunk/UCore.pas
r9 r10 8 8 Classes, SysUtils, FileUtil, ActnList, UProject, UGraphic, Controls, Graphics; 9 9 10 const 11 ZoomFactor = 1.5; 12 10 13 type 14 TFloatPoint = record 15 X, Y: Double; 16 end; 11 17 12 18 { TCore } 13 19 14 20 TCore = class(TDataModule) 21 AImageGradient: TAction; 15 22 AImageMirror: TAction; 16 23 AImageFlip: TAction; … … 32 39 procedure AImageClearExecute(Sender: TObject); 33 40 procedure AImageFlipExecute(Sender: TObject); 41 procedure AImageGradientExecute(Sender: TObject); 34 42 procedure AImageMirrorExecute(Sender: TObject); 35 43 procedure AImageRandomExecute(Sender: TObject); 36 44 procedure AFileNewExecute(Sender: TObject); 45 procedure AZoomAllExecute(Sender: TObject); 37 46 procedure AZoomInExecute(Sender: TObject); 38 47 procedure AZoomNormalExecute(Sender: TObject); … … 43 52 public 44 53 Project: TProject; 54 procedure Init; 45 55 end; 46 56 … … 53 63 54 64 uses 55 UFormNew, UFormMain, Forms, UColorRGBA8, UColorGray8, UColorGray1; 65 UFormNew, UFormMain, Forms, UColorRGBA8, UColorGray8, UColorGray1, UColorGray4; 66 67 function FloatPoint(AX, AY: Double): TFloatPoint; 68 begin 69 Result.X := AX; 70 Result.Y := AY; 71 end; 56 72 57 73 { TCore } … … 63 79 ColorManager.RegisterFormat(TGColorFormatRGBA8); 64 80 ColorManager.RegisterFormat(TGColorFormatGray8); 81 ColorManager.RegisterFormat(TGColorFormatGray4); 65 82 ColorManager.RegisterFormat(TGColorFormatGray1); 83 end; 66 84 85 procedure TCore.Init; 86 begin 67 87 // Set default 68 88 Project.Bitmap.Size := Point(200, 100); 69 89 if ColorManager.FormatCount > 0 then 70 90 Project.Bitmap.ColorFormat := ColorManager.Formats[0]; 91 Project.View.DestRect := Bounds(0, 0, FormMain.PaintBox1.Width, FormMain.PaintBox1.Height); 92 Core.AZoomAll.Execute; 71 93 end; 72 94 … … 81 103 Project.Bitmap.BackgroundColor.FromTColor(clBlack); 82 104 Project.Bitmap.DPI := FormNew.SpinEditDPI.Value; 83 FormMain.Redraw;105 AZoomAll.Execute;; 84 106 end; 107 end; 108 109 procedure TCore.AZoomAllExecute(Sender: TObject); 110 var 111 Factor: TFloatPoint; 112 begin 113 with Core.Project, View do begin 114 Factor := FloatPoint((DestRect.Right - DestRect.Left) / Bitmap.Size.X, 115 (DestRect.Bottom - DestRect.Top) / Bitmap.Size.Y); 116 if Factor.X < Factor.Y then Zoom := Factor.X 117 else Zoom := Factor.Y; 118 Center(Rect(0, 0, Bitmap.Size.X, Bitmap.Size.Y)); 119 end; 120 FormMain.Redraw; 85 121 end; 86 122 87 123 procedure TCore.AZoomInExecute(Sender: TObject); 88 124 begin 89 Project.View Port.Zoom := Project.ViewPort.Zoom * 1.3;125 Project.View.Zoom := Project.View.Zoom * ZoomFactor; 90 126 FormMain.Redraw; 91 127 end; … … 93 129 procedure TCore.AZoomNormalExecute(Sender: TObject); 94 130 begin 95 Project.View Port.Zoom := 1;131 Project.View.Zoom := 1; 96 132 FormMain.Redraw; 97 133 end; … … 99 135 procedure TCore.AZoomOutExecute(Sender: TObject); 100 136 begin 101 Project.View Port.Zoom := Project.ViewPort.Zoom / 1.3;137 Project.View.Zoom := Project.View.Zoom / ZoomFactor; 102 138 FormMain.Redraw; 103 139 end; … … 124 160 end; 125 161 162 procedure TCore.AImageGradientExecute(Sender: TObject); 163 begin 164 Core.Project.Bitmap.Gradient; 165 FormMain.Redraw; 166 end; 167 126 168 procedure TCore.AImageMirrorExecute(Sender: TObject); 127 169 begin -
trunk/UGraphic.pas
r9 r10 72 72 public 73 73 function GetDataSize: Integer; 74 procedure PaintToCanvas(Canvas: TCanvas );74 procedure PaintToCanvas(Canvas: TCanvas; Rect: TRect); 75 75 procedure Clear; 76 76 procedure Random; 77 procedure Gradient; 77 78 procedure Flip; 78 79 procedure Mirror; … … 435 436 end; 436 437 437 procedure TGBitmap.PaintToCanvas(Canvas: TCanvas );438 procedure TGBitmap.PaintToCanvas(Canvas: TCanvas; Rect: TRect); 438 439 var 439 440 X, Y: Integer; … … 442 443 try 443 444 Canvas.Lock; 444 for Y := 0 to Size.Y - 1 do 445 for X := 0 to Size.X - 1 do begin 445 for Y := Rect.Top to Rect.Bottom - 1 do 446 for X := Rect.Left to Rect.Right - 1 do 447 if (X >= 0) and (X < Size.X) and (Y >= 0) and (Y < Size.Y) then begin 446 448 Pixel := Pixels[X, Y]; 447 Canvas.Pixels[X , Y] := Pixel.ToTColor;449 Canvas.Pixels[X - Rect.Left, Y - Rect.Top] := Pixel.ToTColor; 448 450 Pixel.Free; 449 451 end; … … 475 477 for X := 0 to Size.X - 1 do begin 476 478 Color.FromTColor(System.Random($ffffff)); 479 F := Cardinal(Color.Data.GetInteger); 480 481 Pixels[X, Y] := Color; 482 end; 483 Color.Free; 484 end; 485 486 procedure TGBitmap.Gradient; 487 var 488 X, Y: Integer; 489 Color: TGColor; 490 F: Cardinal; 491 N: Integer; 492 begin 493 Color := TGColor.Create; 494 Color.Format := ColorFormat; 495 for Y := 0 to Size.Y - 1 do 496 for X := 0 to Size.X - 1 do begin 497 if Y < Size.Y div 4 then N := $000001 498 else if Y < Size.Y div 4 * 2 then N := $000100 499 else if Y < Size.Y div 4 * 3 then N := $010000 500 else N := $010101; 501 502 Color.FromTColor(N * (X * 255 div Size.X)); 477 503 F := Cardinal(Color.Data.GetInteger); 478 504 -
trunk/UProject.pas
r6 r10 10 10 type 11 11 12 { TView Port}12 { TView } 13 13 14 TViewPort = class 14 TView = class 15 private 16 FSrcRect: TRect; 17 FDestRect: TRect; 18 FZoom: Double; 19 procedure SetDestRect(AValue: TRect); 20 procedure SetSrcRect(AValue: TRect); 21 procedure SetZoom(AValue: Double); 15 22 public 16 Pos: TPoint; 17 Zoom: Double; 18 ViewSize: TPoint; 23 procedure Center(Rect: TRect); 24 constructor Create; 25 function DestToSrcPos(Pos: TPoint): TPoint; 26 function SrcToDestPos(Pos: TPoint): TPoint; 27 property DestRect: TRect read FDestRect write SetDestRect; 28 property Zoom: Double read FZoom write SetZoom; 29 property SrcRect: TRect read FSrcRect write SetSrcRect; 19 30 end; 20 31 … … 24 35 FileName: string; 25 36 Bitmap: TGBitmap; 26 View Port: TViewPort;37 View: TView; 27 38 constructor Create; 28 39 destructor Destroy; override; … … 30 41 31 42 implementation 43 44 function RectEquals(A, B: TRect): Boolean; 45 begin 46 Result := (A.Left = B.Left) and (A.Top = B.Top) and 47 (A.Right = B.Right) and (A.Bottom = B.Bottom); 48 end; 49 50 51 { TView } 52 53 procedure TView.SetDestRect(AValue: TRect); 54 var 55 Diff: TPoint; 56 begin 57 if RectEquals(FDestRect, AValue) then Exit; 58 Diff := Point(Trunc((FDestRect.Right - FDestRect.Left) / FZoom - 59 (AValue.Right - AValue.Left) / FZoom) div 2, 60 Trunc((FDestRect.Bottom - FDestRect.Top) / FZoom - 61 (AValue.Bottom - AValue.Top) / FZoom) div 2); 62 FDestRect := AValue; 63 SrcRect := Bounds(FSrcRect.Left + Diff.X, FSrcRect.Top + Diff.Y, 64 Trunc((FDestRect.Right - FDestRect.Left) / FZoom), 65 Trunc((FDestRect.Bottom - FDestRect.Top) / FZoom)); 66 end; 67 68 procedure TView.SetSrcRect(AValue: TRect); 69 begin 70 if RectEquals(FSrcRect, AValue) then Exit; 71 FSrcRect := AValue; 72 end; 73 74 procedure TView.SetZoom(AValue: Double); 75 const 76 ZoomMin = 1/10000; 77 ZoomMax = 10000; 78 begin 79 if FZoom = AValue then Exit; 80 if (FZoom > ZoomMax) or (FZoom < ZoomMin) then Exit; 81 FZoom := AValue; 82 FSrcRect := Bounds(Trunc(FSrcRect.Left + (FSrcRect.Right - FSrcRect.Left) div 2 - 83 (DestRect.Right - DestRect.Left) / FZoom / 2), 84 Trunc(FSrcRect.Top + (FSrcRect.Bottom - FSrcRect.Top) div 2 - 85 (DestRect.Bottom - DestRect.Top) / FZoom / 2), 86 Trunc((DestRect.Right - DestRect.Left) / FZoom), 87 Trunc((DestRect.Bottom - DestRect.Top) / FZoom)); 88 end; 89 90 procedure TView.Center(Rect: TRect); 91 begin 92 FSrcRect := Bounds(Rect.Left + (Rect.Right - Rect.Left) div 2 - (FSrcRect.Right - FSrcRect.Left) div 2, 93 Rect.Top + (Rect.Bottom - Rect.Top) div 2 - (FSrcRect.Bottom - FSrcRect.Top) div 2, 94 FSrcRect.Right - FSrcRect.Left, 95 FSrcRect.Bottom - FSrcRect.Top); 96 end; 97 98 constructor TView.Create; 99 begin 100 FZoom := 1; 101 end; 102 103 function TView.DestToSrcPos(Pos: TPoint): TPoint; 104 begin 105 Result := Point(Trunc(Pos.X / FZoom + FSrcRect.Left), 106 Trunc(Pos.Y / FZoom + FSrcRect.Top)); 107 end; 108 109 function TView.SrcToDestPos(Pos: TPoint): TPoint; 110 begin 111 Result := Point(Trunc((Pos.X - FSrcRect.Left) * FZoom), 112 Trunc((Pos.Y - FSrcRect.Top) * FZoom)); 113 end; 32 114 33 115 … … 37 119 begin 38 120 Bitmap := TGBitmap.Create; 39 View Port := TViewPort.Create;121 View := TView.Create; 40 122 end; 41 123 42 124 destructor TProject.Destroy; 43 125 begin 44 View Port.Free;126 View.Free; 45 127 Bitmap.Free; 46 128 inherited Destroy;
Note:
See TracChangeset
for help on using the changeset viewer.