Changeset 21 for branches/gbitmap/GImage.pas
- Timestamp:
- Dec 22, 2016, 1:01:41 PM (8 years ago)
- Location:
- branches/gbitmap
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/gbitmap
- Property svn:ignore
-
old new 4 4 project1.res 5 5 project1.exe 6 heaptrclog.trc
-
- Property svn:ignore
-
branches/gbitmap/GImage.pas
r20 r21 6 6 7 7 uses 8 Classes, SysUtils, Graphics, GPixmap, UPixmapSpecialized ;8 Classes, SysUtils, Graphics, GPixmap, UPixmapSpecialized, Contnrs; 9 9 10 10 type 11 TColorFormat = (cfNone, cfGray1, cfGray2, cfGray4, cfGray8, cfGray16, cfGray32,12 cfRGB8, cfRGB16);13 14 11 TColorName = (cnBlack, cnWhite, cnBlue, cnRed, cnGreen, cnGray, cnSilver); 15 12 … … 19 16 end; 20 17 21 { TBColorNone } 22 23 TBColorNone = class(TInterfacedObject, IBColor) 24 procedure SetColorName(ColorName: TColorName); 25 procedure SetRandom; 26 end; 27 28 { TBColorGray1 } 29 30 TBColorGray1 = class(TInterfacedObject, IBColor) 31 Value: TColorGray1; 32 constructor Create(Color: TColorGray1); 33 procedure SetColorName(ColorName: TColorName); 34 procedure SetRandom; 35 end; 36 37 { TBColorGray2 } 38 39 TBColorGray2 = class(TInterfacedObject, IBColor) 40 Value: TColorGray2; 41 constructor Create(Color: TColorGray2); 42 procedure SetColorName(ColorName: TColorName); 43 procedure SetRandom; 44 end; 18 { TBColor } 19 20 TBColor = class(TInterfacedObject, IBColor) 21 procedure SetColorName(ColorName: TColorName); virtual; 22 procedure SetRandom; virtual; 23 end; 24 25 IBColorClass = class of TBColor; 45 26 46 27 { TBColorRGB8 } 47 28 48 TBColorRGB8 = class(T InterfacedObject, IBColor)29 TBColorRGB8 = class(TBColor) 49 30 Value: TColorRGB8; 50 31 constructor Create(Color: TColorRGB8); 51 procedure SetColorName(ColorName: TColorName); 52 procedure SetRandom; 32 procedure SetColorName(ColorName: TColorName); override; 33 procedure SetRandom; override; 53 34 end; 54 35 … … 61 42 private 62 43 FSize: TPoint; 44 protected 63 45 function GetPixel(X, Y: Integer): IBColor; virtual; 64 46 procedure SetPixel(X, Y: Integer; AValue: IBColor); virtual; 65 protected66 47 procedure SetSize(AValue: TPoint); virtual; 67 48 public … … 70 51 procedure PaintToCanvas(Canvas: TCanvas); virtual; 71 52 function GetDataSize: Integer; virtual; 53 constructor Create; virtual; 72 54 property Size: TPoint read FSize write SetSize; 73 55 property Pixels[X, Y: Integer]: IBColor read GetPixel write SetPixel; 74 56 end; 75 57 76 { TBImageGray1 } 77 78 TBImageGray1 = class(TBImage) 79 private 80 FillCallBack: TGetColorPos; 81 function FillGetColor(Position: TPoint): TColorGray1; 82 protected 83 procedure SetSize(AValue: TPoint); override; 84 function GetPixel(X, Y: Integer): IBColor; override; 85 procedure SetPixel(X, Y: Integer; AValue: IBColor); override; 86 public 87 Pixmap: TPixmapGray1; 88 procedure Fill(Color: IBColor); override; 89 procedure Fill(Func: TGetColorPos); override; 90 procedure PaintToCanvas(Canvas: TCanvas); override; 91 function GetDataSize: Integer; override; 92 constructor Create; 93 destructor Destroy; override; 94 end; 95 96 { TBImageGray2 } 97 98 TBImageGray2 = class(TBImage) 99 private 100 FillCallBack: TGetColorPos; 101 function FillGetColor(Position: TPoint): TColorGray1; 102 protected 103 procedure SetSize(AValue: TPoint); override; 104 function GetPixel(X, Y: Integer): IBColor; override; 105 procedure SetPixel(X, Y: Integer; AValue: IBColor); override; 106 public 107 Pixmap: TPixmapGray2; 108 procedure Fill(Color: IBColor); override; 109 procedure Fill(Func: TGetColorPos); override; 110 procedure PaintToCanvas(Canvas: TCanvas); override; 111 function GetDataSize: Integer; override; 112 constructor Create; 113 destructor Destroy; override; 114 end; 58 TBPixmapClass = class of TBImage; 115 59 116 60 { TBImageRGB8 } … … 124 68 constructor Create; 125 69 destructor Destroy; override; 70 end; 71 72 { TColorFormatChannel } 73 74 TColorFormatChannel = record 75 Name: string; 76 Position: Integer; 77 BitWidth: Integer; 78 end; 79 80 { TColorFormat } 81 82 TColorFormat = class 83 Name: string; 84 BitDepth: Integer; 85 Channels: array of TColorFormatChannel; 86 BackendColorClass: IBColorClass; 87 BackendImageClass: TBPixmapClass; 88 procedure AddChannel(Name: string; Position, BitWidth: Integer); 89 function GetBackendColor: IBColor; 90 function GetBackendImage: TBImage; 91 constructor Create; virtual; 92 function GetChannelStateCount(Channel: Integer): Integer; 93 end; 94 95 TColorFormatClass = class of TColorFormat; 96 97 { TColorFormatManager } 98 99 TColorFormatManager = class 100 private 101 FFormats: TObjectList; // TList<TColorFormat> 102 function GetFormat(Index: Integer): TColorFormat; 103 public 104 constructor Create; virtual; 105 destructor Destroy; override; 106 procedure RegisterFormat(Format: TColorFormatClass); 107 function FormatCount: Integer; 108 property Formats[Index: Integer]: TColorFormat read GetFormat; 126 109 end; 127 110 … … 172 155 end; 173 156 157 var 158 ColorFormatManager: TColorFormatManager; 159 174 160 175 161 implementation 176 162 177 { TBColorNone } 178 179 procedure TBColorNone.SetColorName(ColorName: TColorName); 180 begin 181 end; 182 183 procedure TBColorNone.SetRandom; 184 begin 163 { TBColor } 164 165 procedure TBColor.SetColorName(ColorName: TColorName); 166 begin 167 end; 168 169 procedure TBColor.SetRandom; 170 begin 171 end; 172 173 { TColorFormatManager } 174 175 function TColorFormatManager.GetFormat(Index: Integer): TColorFormat; 176 begin 177 Result := TColorFormat(FFormats[Index]); 178 end; 179 180 constructor TColorFormatManager.Create; 181 begin 182 FFormats := TObjectList.Create; 183 end; 184 185 destructor TColorFormatManager.Destroy; 186 begin 187 FreeAndNil(FFormats); 188 inherited Destroy; 189 end; 190 191 procedure TColorFormatManager.RegisterFormat(Format: TColorFormatClass); 192 begin 193 FFormats.Add(Format.Create); 194 end; 195 196 function TColorFormatManager.FormatCount: Integer; 197 begin 198 Result := FFormats.Count; 199 end; 200 201 { TColorFormat } 202 203 procedure TColorFormat.AddChannel(Name: string; Position, BitWidth: Integer); 204 begin 205 SetLength(Channels, Length(Channels) + 1); 206 Channels[Length(Channels) - 1].Name := Name; 207 Channels[Length(Channels) - 1].Position := Position; 208 Channels[Length(Channels) - 1].BitWidth := BitWidth; 209 end; 210 211 function TColorFormat.GetBackendColor: IBColor; 212 begin 213 Result := BackendColorClass.Create; 214 end; 215 216 function TColorFormat.GetBackendImage: TBImage; 217 begin 218 Result := BackendImageClass.Create; 219 end; 220 221 constructor TColorFormat.Create; 222 begin 223 Name := 'None'; 224 BitDepth := 0; 225 BackendColorClass := TBColor; 226 BackendImageClass := TBImage; 227 end; 228 229 function TColorFormat.GetChannelStateCount(Channel: Integer): Integer; 230 begin 231 Result := 1 shl Channels[Channel].BitWidth; 185 232 end; 186 233 … … 190 237 begin 191 238 if FColorFormat = AValue then Exit; 192 if AValue = cfGray1 then FBackend := TBColorGray1.Create(0) 193 else if AValue = cfGray2 then FBackend := TBColorGray2.Create(0) 194 else if AValue = cfRGB8 then FBackend := TBColorRGB8.Create(TColorRGB8.Create(0, 0, 0)) 195 else raise Exception.Create('Missing color backend for specified color format'); 239 FBackend := AValue.GetBackendColor; 196 240 FColorFormat := AValue; 197 241 end; … … 209 253 constructor TColor.Create; 210 254 begin 211 ColorFormat := cfNone;255 ColorFormat := ColorFormatManager.GetFormat(0); 212 256 end; 213 257 … … 246 290 end; 247 291 248 { TBColorGray2 } 249 250 constructor TBColorGray2.Create(Color: TColorGray2); 251 begin 252 Value := Color; 253 end; 254 255 procedure TBColorGray2.SetColorName(ColorName: TColorName); 256 begin 257 case ColorName of 258 cnBlack: Value := 0; 259 cnGray: Value := 1; 260 cnSilver: Value := 2; 261 cnWhite: Value := 3; 262 else Value := 0; 263 end; 264 end; 265 266 procedure TBColorGray2.SetRandom; 267 begin 268 Random(4); 269 end; 270 271 { TBColorGray1 } 272 273 constructor TBColorGray1.Create(Color: TColorGray1); 274 begin 275 Value := Color; 276 end; 277 278 procedure TBColorGray1.SetColorName(ColorName: TColorName); 279 begin 280 case ColorName of 281 cnBlack: Value := 0; 282 cnWhite: Value := 1; 283 else Value := 0; 284 end; 285 end; 286 287 procedure TBColorGray1.SetRandom; 288 begin 289 Value := Random(2); 290 end; 291 292 { TBImageGray2 } 293 294 function TBImageGray2.FillGetColor(Position: TPoint): TColorGray1; 295 begin 296 Result := (FillCallBack(Position) as TBColorGray2).Value; 297 end; 298 299 procedure TBImageGray2.SetSize(AValue: TPoint); 292 { TBImageRGB8 } 293 294 procedure TBImageRGB8.SetSize(AValue: TPoint); 300 295 begin 301 296 inherited; … … 303 298 end; 304 299 305 function TBImageGray2.GetPixel(X, Y: Integer): IBColor;306 begin307 Result := TBColorGray2.Create(Pixmap.Pixels[X, Y]);308 end;309 310 procedure TBImageGray2.SetPixel(X, Y: Integer; AValue: IBColor);311 begin312 Pixmap.Pixels[X, Y] := (AValue as TBColorGray2).Value;313 end;314 315 procedure TBImageGray2.Fill(Color: IBColor);316 begin317 if Color is TBColorGray2 then318 Pixmap.Fill((Color as TBColorGray2).Value);319 end;320 321 procedure TBImageGray2.Fill(Func: TGetColorPos);322 begin323 FillCallBack := Func;324 Pixmap.Fill(FillGetColor);325 end;326 327 procedure TBImageGray2.PaintToCanvas(Canvas: TCanvas);328 begin329 Pixmap.PaintToCanvas(Canvas, Pixmap.Gray2ToColor);330 end;331 332 function TBImageGray2.GetDataSize: Integer;333 begin334 Result := Pixmap.GetDataSize;335 end;336 337 constructor TBImageGray2.Create;338 begin339 Pixmap := TPixmapGray2.Create;340 Pixmap.BitsPerPixel := 2;341 end;342 343 destructor TBImageGray2.Destroy;344 begin345 FreeAndNil(Pixmap);346 inherited;347 end;348 349 350 { TBImageRGB8 }351 352 procedure TBImageRGB8.SetSize(AValue: TPoint);353 begin354 inherited;355 Pixmap.Size := AValue;356 end;357 358 300 procedure TBImageRGB8.Fill(Color: IBColor); 359 301 begin … … 377 319 function TBImage.GetPixel(X, Y: Integer): IBColor; 378 320 begin 379 Result := TBColor None.Create;321 Result := TBColor.Create; 380 322 end; 381 323 … … 407 349 end; 408 350 409 { TBImageGray1 } 410 411 function TBImageGray1.FillGetColor(Position: TPoint): TColorGray1; 412 begin 413 Result := (FillCallBack(Position) as TBColorGray1).Value; 414 end; 415 416 procedure TBImageGray1.SetSize(AValue: TPoint); 417 begin 418 inherited; 419 Pixmap.Size := AValue; 420 end; 421 422 function TBImageGray1.GetPixel(X, Y: Integer): IBColor; 423 begin 424 Result := TBColorGray1.Create(Pixmap.Pixels[X, Y]); 425 end; 426 427 procedure TBImageGray1.SetPixel(X, Y: Integer; AValue: IBColor); 428 begin 429 Pixmap.Pixels[X, Y] := (AValue as TBColorGray1).Value; 430 end; 431 432 procedure TBImageGray1.Fill(Color: IBColor); 433 begin 434 if Color is TBColorGray1 then 435 Pixmap.Fill((Color as TBColorGray1).Value); 436 end; 437 438 procedure TBImageGray1.Fill(Func: TGetColorPos); 439 begin 440 FillCallBack := Func; 441 Pixmap.Fill(FillGetColor); 442 end; 443 444 procedure TBImageGray1.PaintToCanvas(Canvas: TCanvas); 445 begin 446 Pixmap.PaintToCanvas(Canvas, Pixmap.Gray1ToColor); 447 end; 448 449 function TBImageGray1.GetDataSize: Integer; 450 begin 451 Result := Pixmap.GetDataSize; 452 end; 453 454 constructor TBImageGray1.Create; 455 begin 456 Pixmap := TPixmapGray1.Create; 457 Pixmap.BitsPerPixel := 1; 458 end; 459 460 destructor TBImageGray1.Destroy; 461 begin 462 FreeAndNil(Pixmap); 463 inherited; 351 constructor TBImage.Create; 352 begin 353 Size := Point(0, 0); 464 354 end; 465 355 … … 470 360 if FColorFormat = AValue then Exit; 471 361 FBackend.Free; 472 if AValue = cfGray1 then FBackend := TBImageGray1.Create 473 else if AValue = cfGray2 then FBackend := TBImageGray2.Create 474 else if AValue = cfRGB8 then FBackend := TBImageRGB8.Create 475 else raise Exception.Create('Missing image backend for specified color format'); 362 FBackend := AValue.GetBackendImage; 476 363 FBackend.Size := FSize; 477 364 FColorFormat := AValue; … … 525 412 constructor TPixmap.Create; 526 413 begin 414 ColorFormat := ColorFormatManager.GetFormat(0); 527 415 FBackend := TBImage.Create; 528 416 end; … … 534 422 end; 535 423 424 425 initialization 426 427 ColorFormatManager := TColorFormatManager.Create; 428 ColorFormatManager.RegisterFormat(TColorFormat); 429 //TColorFormat = (cfNone, cfGray1, cfGray2, cfGray4, cfGray8, cfGray16, cfGray32, 430 // cfRGB8, cfRGB16); 431 432 433 finalization 434 435 FreeAndNil(ColorFormatManager); 436 437 536 438 end. 537 3 439
Note:
See TracChangeset
for help on using the changeset viewer.