Changeset 20 for branches/gbitmap/GImage.pas
- Timestamp:
- Dec 21, 2016, 3:49:27 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/gbitmap/GImage.pas
r19 r20 9 9 10 10 type 11 TBColor = class 12 end; 13 14 TBColorGray1 = class(TBColor) 11 TColorFormat = (cfNone, cfGray1, cfGray2, cfGray4, cfGray8, cfGray16, cfGray32, 12 cfRGB8, cfRGB16); 13 14 TColorName = (cnBlack, cnWhite, cnBlue, cnRed, cnGreen, cnGray, cnSilver); 15 16 IBColor = interface 17 procedure SetColorName(ColorName: TColorName); 18 procedure SetRandom; 19 end; 20 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) 15 31 Value: TColorGray1; 16 end; 17 18 TBColorGray2 = class(TBColor) 32 constructor Create(Color: TColorGray1); 33 procedure SetColorName(ColorName: TColorName); 34 procedure SetRandom; 35 end; 36 37 { TBColorGray2 } 38 39 TBColorGray2 = class(TInterfacedObject, IBColor) 19 40 Value: TColorGray2; 20 end; 21 22 TBColorRGB8 = class(TBColor) 41 constructor Create(Color: TColorGray2); 42 procedure SetColorName(ColorName: TColorName); 43 procedure SetRandom; 44 end; 45 46 { TBColorRGB8 } 47 48 TBColorRGB8 = class(TInterfacedObject, IBColor) 23 49 Value: TColorRGB8; 50 constructor Create(Color: TColorRGB8); 51 procedure SetColorName(ColorName: TColorName); 52 procedure SetRandom; 24 53 end; 25 54 … … 29 58 public 30 59 type 31 TGetColorPos = function (Position: TPoint): TBColor of object;60 TGetColorPos = function (Position: TPoint): IBColor of object; 32 61 private 33 62 FSize: TPoint; 63 function GetPixel(X, Y: Integer): IBColor; virtual; 64 procedure SetPixel(X, Y: Integer; AValue: IBColor); virtual; 34 65 protected 35 66 procedure SetSize(AValue: TPoint); virtual; 36 67 public 37 procedure Fill(Color: TBColor); virtual; overload;68 procedure Fill(Color: IBColor); virtual; overload; 38 69 procedure Fill(Func: TGetColorPos); virtual; overload; 39 70 procedure PaintToCanvas(Canvas: TCanvas); virtual; 71 function GetDataSize: Integer; virtual; 40 72 property Size: TPoint read FSize write SetSize; 73 property Pixels[X, Y: Integer]: IBColor read GetPixel write SetPixel; 41 74 end; 42 75 … … 44 77 45 78 TBImageGray1 = class(TBImage) 79 private 80 FillCallBack: TGetColorPos; 81 function FillGetColor(Position: TPoint): TColorGray1; 46 82 protected 47 83 procedure SetSize(AValue: TPoint); override; 84 function GetPixel(X, Y: Integer): IBColor; override; 85 procedure SetPixel(X, Y: Integer; AValue: IBColor); override; 48 86 public 49 87 Pixmap: TPixmapGray1; 50 procedure Fill(Color: TBColor); override;88 procedure Fill(Color: IBColor); override; 51 89 procedure Fill(Func: TGetColorPos); override; 52 90 procedure PaintToCanvas(Canvas: TCanvas); override; 91 function GetDataSize: Integer; override; 53 92 constructor Create; 54 93 destructor Destroy; override; … … 58 97 59 98 TBImageGray2 = class(TBImage) 99 private 100 FillCallBack: TGetColorPos; 101 function FillGetColor(Position: TPoint): TColorGray1; 60 102 protected 61 103 procedure SetSize(AValue: TPoint); override; 104 function GetPixel(X, Y: Integer): IBColor; override; 105 procedure SetPixel(X, Y: Integer; AValue: IBColor); override; 62 106 public 63 107 Pixmap: TPixmapGray2; 64 procedure Fill(Color: TBColor); override; 108 procedure Fill(Color: IBColor); override; 109 procedure Fill(Func: TGetColorPos); override; 65 110 procedure PaintToCanvas(Canvas: TCanvas); override; 111 function GetDataSize: Integer; override; 66 112 constructor Create; 67 113 destructor Destroy; override; … … 75 121 public 76 122 Pixmap: TGPixmap<TColorRGB8>; 77 procedure Fill(Color: TBColor); override;123 procedure Fill(Color: IBColor); override; 78 124 constructor Create; 79 125 destructor Destroy; override; 80 126 end; 81 127 82 83 TColorFormat = (cfNone, cfGray1, cfGray2, cfGray4, cfGray8, cfGray16, cfGray32, 84 cfRGB8, cfRGB16); 85 86 { TGImage } 87 88 TGImage = class 128 { TColor } 129 130 TColor = class 131 private 132 FBackend: IBColor; 133 FColorFormat: TColorFormat; 134 procedure SetColorFormat(AValue: TColorFormat); 135 public 136 property Backend: IBColor read FBackend; 137 property ColorFormat: TColorFormat read FColorFormat write SetColorFormat; 138 procedure SetColorName(ColorName: TColorName); 139 procedure SetRandom; 140 constructor Create; overload; 141 constructor Create(ColorFormat: TColorFormat; ColorName: TColorName); overload; 142 destructor Destroy; override; 143 end; 144 145 { TPixmap } 146 147 TPixmap = class 89 148 public 90 149 type 91 TGetColorPos = function (Position: TPoint ): TBColor of object;150 TGetColorPos = function (Position: TPoint; ColorFormat: TColorFormat): TColor of object; 92 151 private 93 152 FBackend: TBImage; 94 153 FColorFormat: TColorFormat; 95 154 FSize: TPoint; 155 FillCallBack: TGetColorPos; 156 function FillGetColor(Position: TPoint): IBColor; 157 function GetPixel(X, Y: Integer): TColor; 96 158 procedure SetColorFormat(AValue: TColorFormat); 159 procedure SetPixel(X, Y: Integer; AValue: TColor); 97 160 procedure SetSize(AValue: TPoint); 98 161 public 162 procedure Fill(Color: TColor); overload; 163 procedure Fill(Func: TGetColorPos); overload; 164 procedure PaintToCanvas(Canvas: TCanvas); 165 function GetDataSize: Integer; 166 constructor Create; 167 destructor Destroy; override; 99 168 property Backend: TBImage read FBackend; 100 169 property ColorFormat: TColorFormat read FColorFormat write SetColorFormat; 101 170 property Size: TPoint read FSize write SetSize; 102 procedure Fill(Color: TBColor); overload; 103 procedure Fill(Func: TGetColorPos); overload; 104 procedure PaintToCanvas(Canvas: TCanvas); 105 constructor Create; 106 destructor Destroy; override; 171 property Pixels[X, Y: Integer]: TColor read GetPixel write SetPixel; 107 172 end; 108 173 … … 110 175 implementation 111 176 177 { TBColorNone } 178 179 procedure TBColorNone.SetColorName(ColorName: TColorName); 180 begin 181 end; 182 183 procedure TBColorNone.SetRandom; 184 begin 185 end; 186 187 { TColor } 188 189 procedure TColor.SetColorFormat(AValue: TColorFormat); 190 begin 191 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'); 196 FColorFormat := AValue; 197 end; 198 199 procedure TColor.SetColorName(ColorName: TColorName); 200 begin 201 FBackend.SetColorName(ColorName); 202 end; 203 204 procedure TColor.SetRandom; 205 begin 206 FBackend.SetRandom; 207 end; 208 209 constructor TColor.Create; 210 begin 211 ColorFormat := cfNone; 212 end; 213 214 constructor TColor.Create(ColorFormat: TColorFormat; ColorName: TColorName); 215 begin 216 Self.ColorFormat := ColorFormat; 217 Backend.SetColorName(ColorName); 218 end; 219 220 destructor TColor.Destroy; 221 begin 222 inherited Destroy; 223 end; 224 225 { TBColorRGB8 } 226 227 constructor TBColorRGB8.Create(Color: TColorRGB8); 228 begin 229 Value := Color; 230 end; 231 232 procedure TBColorRGB8.SetColorName(ColorName: TColorName); 233 begin 234 case ColorName of 235 cnBlack: Value := TColorRGB8.Create(0, 0, 0); 236 cnGray: Value := TColorRGB8.Create($80, $80, $80); 237 cnSilver: Value := TColorRGB8.Create($C0, $C0, $C0); 238 cnWhite: Value := TColorRGB8.Create($ff, $ff, $ff); 239 else Value := TColorRGB8.Create(0, 0, 0);; 240 end; 241 end; 242 243 procedure TBColorRGB8.SetRandom; 244 begin 245 Value := TColorRGB8.Create(Random(256), Random(256), Random(256)); 246 end; 247 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 112 292 { TBImageGray2 } 293 294 function TBImageGray2.FillGetColor(Position: TPoint): TColorGray1; 295 begin 296 Result := (FillCallBack(Position) as TBColorGray2).Value; 297 end; 113 298 114 299 procedure TBImageGray2.SetSize(AValue: TPoint); … … 118 303 end; 119 304 120 procedure TBImageGray2.Fill(Color: TBColor); 305 function TBImageGray2.GetPixel(X, Y: Integer): IBColor; 306 begin 307 Result := TBColorGray2.Create(Pixmap.Pixels[X, Y]); 308 end; 309 310 procedure TBImageGray2.SetPixel(X, Y: Integer; AValue: IBColor); 311 begin 312 Pixmap.Pixels[X, Y] := (AValue as TBColorGray2).Value; 313 end; 314 315 procedure TBImageGray2.Fill(Color: IBColor); 121 316 begin 122 317 if Color is TBColorGray2 then … … 124 319 end; 125 320 321 procedure TBImageGray2.Fill(Func: TGetColorPos); 322 begin 323 FillCallBack := Func; 324 Pixmap.Fill(FillGetColor); 325 end; 326 126 327 procedure TBImageGray2.PaintToCanvas(Canvas: TCanvas); 127 328 begin … … 129 330 end; 130 331 332 function TBImageGray2.GetDataSize: Integer; 333 begin 334 Result := Pixmap.GetDataSize; 335 end; 336 131 337 constructor TBImageGray2.Create; 132 338 begin 133 339 Pixmap := TPixmapGray2.Create; 340 Pixmap.BitsPerPixel := 2; 134 341 end; 135 342 136 343 destructor TBImageGray2.Destroy; 137 344 begin 138 Pixmap.Free;139 inherited Destroy;345 FreeAndNil(Pixmap); 346 inherited; 140 347 end; 141 348 … … 149 356 end; 150 357 151 procedure TBImageRGB8.Fill(Color: TBColor);358 procedure TBImageRGB8.Fill(Color: IBColor); 152 359 begin 153 360 if Color is TBColorRGB8 then … … 162 369 destructor TBImageRGB8.Destroy; 163 370 begin 164 Pixmap.Free;371 FreeAndNil(Pixmap); 165 372 inherited Destroy; 166 373 end; 167 374 168 375 { TBImage } 376 377 function TBImage.GetPixel(X, Y: Integer): IBColor; 378 begin 379 Result := TBColorNone.Create; 380 end; 381 382 procedure TBImage.SetPixel(X, Y: Integer; AValue: IBColor); 383 begin 384 end; 169 385 170 386 procedure TBImage.SetSize(AValue: TPoint); … … 174 390 end; 175 391 176 procedure TBImage.Fill(Color: TBColor);392 procedure TBImage.Fill(Color: IBColor); 177 393 begin 178 394 end; … … 186 402 end; 187 403 404 function TBImage.GetDataSize: Integer; 405 begin 406 Result := 0; 407 end; 408 188 409 { TBImageGray1 } 410 411 function TBImageGray1.FillGetColor(Position: TPoint): TColorGray1; 412 begin 413 Result := (FillCallBack(Position) as TBColorGray1).Value; 414 end; 189 415 190 416 procedure TBImageGray1.SetSize(AValue: TPoint); … … 194 420 end; 195 421 196 procedure TBImageGray1.Fill(Color: TBColor); 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); 197 433 begin 198 434 if Color is TBColorGray1 then … … 202 438 procedure TBImageGray1.Fill(Func: TGetColorPos); 203 439 begin 204 //Pixmap.Fill(); 440 FillCallBack := Func; 441 Pixmap.Fill(FillGetColor); 205 442 end; 206 443 … … 210 447 end; 211 448 449 function TBImageGray1.GetDataSize: Integer; 450 begin 451 Result := Pixmap.GetDataSize; 452 end; 453 212 454 constructor TBImageGray1.Create; 213 455 begin 214 456 Pixmap := TPixmapGray1.Create; 457 Pixmap.BitsPerPixel := 1; 215 458 end; 216 459 217 460 destructor TBImageGray1.Destroy; 218 461 begin 219 Pixmap.Free;220 inherited Destroy;221 end; 222 223 { T GImage}224 225 procedure T GImage.SetColorFormat(AValue: TColorFormat);462 FreeAndNil(Pixmap); 463 inherited; 464 end; 465 466 { TPixmap } 467 468 procedure TPixmap.SetColorFormat(AValue: TColorFormat); 226 469 begin 227 470 if FColorFormat = AValue then Exit; 228 471 FBackend.Free; 229 472 if AValue = cfGray1 then FBackend := TBImageGray1.Create 230 else if AValue = cfGray2 then FBackend := TBImageGray2.Create231 else if AValue = cfRGB8 then FBackend := TBImageRGB8.Create232 else FBackend := nil;233 if Assigned(FBackend) thenFBackend.Size := FSize;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'); 476 FBackend.Size := FSize; 234 477 FColorFormat := AValue; 235 478 end; 236 479 237 procedure TGImage.SetSize(AValue: TPoint); 480 function TPixmap.FillGetColor(Position: TPoint): IBColor; 481 begin 482 Result := FillCallBack(Position, ColorFormat).Backend; 483 end; 484 485 function TPixmap.GetPixel(X, Y: Integer): TColor; 486 begin 487 Result := TColor.Create; 488 Result.ColorFormat := ColorFormat; 489 Result.FBackend := FBackend.Pixels[X, Y]; 490 end; 491 492 procedure TPixmap.SetPixel(X, Y: Integer; AValue: TColor); 493 begin 494 FBackend.Pixels[X, Y] := AValue.FBackend; 495 end; 496 497 procedure TPixmap.SetSize(AValue: TPoint); 238 498 begin 239 499 if (FSize.X = AValue.X) and (FSize.Y = AValue.Y) then Exit; 240 500 FSize := AValue; 241 if Assigned(FBackend) then 242 FBackend.Size := AValue; 243 end; 244 245 procedure TGImage.Fill(Color: TBColor); 246 begin 247 if Assigned(FBackend) then FBackend.Fill(Color); 248 end; 249 250 procedure TGImage.Fill(Func: TGetColorPos); 251 begin 252 // if Assigned(FBackend) then FBackend.Fill(Func: TGetColorPos); 253 end; 254 255 procedure TGImage.PaintToCanvas(Canvas: TCanvas); 256 begin 257 if Assigned(FBackend) then FBackend.PaintToCanvas(Canvas); 258 end; 259 260 constructor TGImage.Create; 261 begin 262 FBackend := nil; 263 end; 264 265 destructor TGImage.Destroy; 266 begin 267 if Assigned(FBackend) then FreeAndNil(FBackend); 501 FBackend.Size := AValue; 502 end; 503 504 procedure TPixmap.Fill(Color: TColor); 505 begin 506 FBackend.Fill(Color.Backend); 507 end; 508 509 procedure TPixmap.Fill(Func: TGetColorPos); 510 begin 511 FillCallBack := Func; 512 FBackend.Fill(FillGetColor); 513 end; 514 515 procedure TPixmap.PaintToCanvas(Canvas: TCanvas); 516 begin 517 FBackend.PaintToCanvas(Canvas); 518 end; 519 520 function TPixmap.GetDataSize: Integer; 521 begin 522 FBackend.GetDataSize; 523 end; 524 525 constructor TPixmap.Create; 526 begin 527 FBackend := TBImage.Create; 528 end; 529 530 destructor TPixmap.Destroy; 531 begin 532 FreeAndNil(FBackend); 268 533 inherited Destroy; 269 534 end; 270 535 271 536 end. 272 537 3
Note:
See TracChangeset
for help on using the changeset viewer.