Changeset 9 for trunk/UGraphic.pas
- Timestamp:
- Sep 21, 2014, 8:11:48 PM (10 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk
- Property svn:ignore
-
old new 2 2 LibrePaint 3 3 lib 4 backup
-
- Property svn:ignore
-
trunk/UGraphic.pas
r8 r9 6 6 7 7 uses 8 Classes, SysUtils, Graphics, Contnrs ;8 Classes, SysUtils, Graphics, Contnrs, UMemory; 9 9 10 10 type … … 13 13 TGColor = class; 14 14 TGCanvas = class; 15 16 TGColorClass = class of TGColor; 15 TGBitmap = class; 17 16 18 17 { TGColorFormat } … … 24 23 function GetChannelBitPos(Channel: TGColorChannel): Integer; virtual; 25 24 function GetChannelBitWidth(Channel: TGColorChannel): Integer; virtual; 25 function GetChannelStateCount(Channel: TGColorChannel): Integer; virtual; 26 26 function ChannelUsed(Channel: TGColorChannel): Boolean; 27 27 function ColorToTColor(Color: TGColor): TColor; virtual; 28 28 procedure ColorFromTColor(GColor: TGColor; Color: TColor); virtual; 29 function GetColorClass: TGColorClass; virtual;30 29 end; 31 30 … … 37 36 private 38 37 FColorFormat: TGColorFormat; 39 FData: PByte;38 FData: TBitMemory; 40 39 function GetChannel(Channel: TGColorChannel): TGColor; 41 40 procedure SetColorFormat(AValue: TGColorFormat); 42 procedure LoadData(Bitmap Data: Pointer); virtual;43 procedure SaveData(Bitmap Data: Pointer); virtual;41 procedure LoadData(Bitmap: TGBitmap; Position: Integer); virtual; 42 procedure SaveData(Bitmap: TGBitmap; Position: Integer); virtual; 44 43 public 45 44 function ToTColor: TColor; … … 48 47 constructor Create; 49 48 property Channels[Channel: TGColorChannel]: TGColor read GetChannel; 50 property Data: PByteread FData;49 property Data: TBitMemory read FData; 51 50 published 52 51 property Format: TGColorFormat read FColorFormat write SetColorFormat; … … 62 61 FDPI: Integer; 63 62 FSize: TPoint; 64 FData: PByte;63 FData: TBitMemory; 65 64 function GetPixel(X, Y: Integer): TGColor; 66 65 function GetSize: TPoint; … … 69 68 procedure SetPixel(X, Y: Integer; AValue: TGColor); 70 69 procedure SetSize(AValue: TPoint); 71 function GetPixelDataPos(X, Y: Integer): Pointer; 70 function GetPixelDataPos(X, Y: Integer): Integer; 71 procedure CheckLimits(X, Y: Integer); 72 72 public 73 73 function GetDataSize: Integer; … … 79 79 constructor Create; virtual; 80 80 destructor Destroy; override; 81 property Data: TBitMemory read FData; 81 82 property BackgroundColor: TGColor read FBackgroundColor write SetBackgroundColor; 82 83 property DPI: Integer read FDPI write FDPI; … … 253 254 end; 254 255 256 function TGColorFormat.GetChannelStateCount(Channel: TGColorChannel): Integer; 257 begin 258 Result := 1 shl GetChannelBitWidth(Channel); 259 end; 260 255 261 function TGColorFormat.ChannelUsed(Channel: TGColorChannel): Boolean; 256 262 begin … … 259 265 260 266 function TGColorFormat.ColorToTColor(Color: TGColor): TColor; 261 begin 262 Result := clBlack; 267 var 268 Channel: TBitMemory; 269 begin 270 Result := 0; 271 Channel := TBitMemory.Create; 272 273 if GetChannelBitWidth(ccRed) > 0 then begin 274 Channel.Size := GetChannelBitWidth(ccRed); 275 Color.Data.ReadBlock(Channel, GetChannelBitPos(ccRed)); 276 Result := Result or (Trunc(Channel.GetInteger * 255 / (GetChannelStateCount(ccRed) - 1)) shl 0); 277 end; 278 279 if GetChannelBitWidth(ccGreen) > 0 then begin 280 Channel.Size := GetChannelBitWidth(ccGreen); 281 Color.Data.ReadBlock(Channel, GetChannelBitPos(ccGreen)); 282 Result := Result or (Trunc(Channel.GetInteger * 255 / (GetChannelStateCount(ccGreen) - 1)) shl 8); 283 end; 284 285 if GetChannelBitWidth(ccBlue) > 0 then begin 286 Channel.Size := GetChannelBitWidth(ccBlue); 287 Color.Data.ReadBlock(Channel, GetChannelBitPos(ccBlue)); 288 Result := Result or (Trunc(Channel.GetInteger * 255 / (GetChannelStateCount(ccBlue) - 1)) shl 16); 289 end; 290 291 if GetChannelBitWidth(ccGray) > 0 then begin 292 Channel.Size := GetChannelBitWidth(ccGray); 293 Color.Data.ReadBlock(Channel, GetChannelBitPos(ccGray)); 294 Result := $010101 * Trunc(Channel.GetInteger * 255 / (GetChannelStateCount(ccGray) - 1)); 295 end; 296 297 Channel.Free; 263 298 end; 264 299 265 300 procedure TGColorFormat.ColorFromTColor(GColor: TGColor; Color: TColor); 266 begin 267 FillChar(GColor.Data^, GetPixelSize, 0); 268 end; 269 270 function TGColorFormat.GetColorClass: TGColorClass; 271 begin 272 Result := TGColor; 301 var 302 Channel: TBitMemory; 303 begin 304 GColor.Data.Clear(0); 305 Channel := TBitMemory.Create; 306 307 if GetChannelBitWidth(ccRed) > 0 then begin 308 Channel.Size := GetChannelBitWidth(ccRed); 309 Channel.SetInteger(((Color shr 0) and $ff) * GetChannelStateCount(ccRed) div 256); 310 GColor.Data.WriteBlock(Channel, GetChannelBitPos(ccRed)); 311 end; 312 313 if GetChannelBitWidth(ccGreen) > 0 then begin 314 Channel.Size := GetChannelBitWidth(ccGreen); 315 Channel.SetInteger(((Color shr 8) and $ff) * GetChannelStateCount(ccGreen) div 256); 316 GColor.Data.WriteBlock(Channel, GetChannelBitPos(ccGreen)); 317 end; 318 319 if GetChannelBitWidth(ccBlue) > 0 then begin 320 Channel.Size := GetChannelBitWidth(ccBlue); 321 Channel.SetInteger(((Color shr 16) and $ff) * GetChannelStateCount(ccBlue) div 256); 322 GColor.Data.WriteBlock(Channel, GetChannelBitPos(ccBlue)); 323 end; 324 325 if GetChannelBitWidth(ccGray) > 0 then begin 326 Channel.Size := GetChannelBitWidth(ccGray); 327 Channel.SetInteger((((Color shr 16) and $ff) + ((Color shr 8) and $ff) + ((Color shr 0) and $ff)) 328 * GetChannelStateCount(ccGray) div (3 * 256)); 329 GColor.Data.WriteBlock(Channel, GetChannelBitPos(ccGray)); 330 end; 331 332 Channel.Free; 273 333 end; 274 334 … … 284 344 if FColorFormat = AValue then Exit; 285 345 FColorFormat := AValue; 286 ReAllocMem(FData, FColorFormat.GetPixelSize);287 end; 288 289 procedure TGColor.LoadData(Bitmap Data: Pointer);290 begin 291 Move(BitmapData^, FData^, FColorFormat.GetPixelSize);292 end; 293 294 procedure TGColor.SaveData(Bitmap Data: Pointer);295 begin 296 Move(FData^, BitmapData^, FColorFormat.GetPixelSize);346 FData.Size := FColorFormat.GetPixelSize; 347 end; 348 349 procedure TGColor.LoadData(Bitmap: TGBitmap; Position: Integer); 350 begin 351 Bitmap.Data.ReadBlock(FData, Position); 352 end; 353 354 procedure TGColor.SaveData(Bitmap: TGBitmap; Position: Integer); 355 begin 356 Bitmap.Data.WriteBlock(FData, Position); 297 357 end; 298 358 … … 313 373 constructor TGColor.Create; 314 374 begin 375 FData := TBitMemory.Create; 315 376 Format := TGColorFormat.Create; 316 377 end; … … 320 381 function TGBitmap.GetPixel(X, Y: Integer): TGColor; 321 382 begin 383 CheckLimits(X, Y); 322 384 Result := TGColor.Create; 323 385 Result.Format := ColorFormat; 324 Result.LoadData( GetPixelDataPos(X, Y));386 Result.LoadData(Self, GetPixelDataPos(X, Y)); 325 387 end; 326 388 … … 340 402 if FColorFormat = AValue then Exit; 341 403 FColorFormat := AValue; 342 ReAllocMem(FData, GetDataSize);404 FData.Size := GetDataSize; 343 405 FBackgroundColor.Format := ColorFormat; 344 406 end; … … 346 408 procedure TGBitmap.SetPixel(X, Y: Integer; AValue: TGColor); 347 409 begin 348 AValue.SaveData(GetPixelDataPos(X, Y)); 410 CheckLimits(X, Y); 411 FData.WriteBlock(AValue.Data, GetPixelDataPos(X, Y)); 349 412 end; 350 413 … … 353 416 if (FSize.X = AValue.X) and (FSize.Y = AValue.Y) then Exit; 354 417 FSize := AValue; 355 ReAllocMem(FData, GetDataSize); 356 end; 357 358 function TGBitmap.GetPixelDataPos(X, Y: Integer): Pointer; 359 begin 360 Result := FData + X * FColorFormat.GetPixelSize + Y * FColorFormat.GetPixelSize * FSize.X; 418 FData.Size := GetDataSize; 419 end; 420 421 function TGBitmap.GetPixelDataPos(X, Y: Integer): Integer; 422 begin 423 Result := X * FColorFormat.GetPixelSize + Y * FColorFormat.GetPixelSize * FSize.X; 424 end; 425 426 procedure TGBitmap.CheckLimits(X, Y: Integer); 427 begin 428 if (X < 0) or (Y < 0) or (X >= Size.X) or (Y >= Size.Y) then 429 raise Exception.Create('Out of range'); 361 430 end; 362 431 … … 399 468 X, Y: Integer; 400 469 Color: TGColor; 470 F: Cardinal; 401 471 begin 402 472 Color := TGColor.Create; … … 405 475 for X := 0 to Size.X - 1 do begin 406 476 Color.FromTColor(System.Random($ffffff)); 477 F := Cardinal(Color.Data.GetInteger); 478 407 479 Pixels[X, Y] := Color; 408 480 end; … … 440 512 constructor TGBitmap.Create; 441 513 begin 442 FData := GetMem(0);514 FData := TBitMemory.Create; 443 515 FBackgroundColor := TGColor.Create; 444 516 ColorFormat := TGColorFormat.Create; … … 451 523 destructor TGBitmap.Destroy; 452 524 begin 525 Size := Point(0, 0); 526 FData.Free; 453 527 inherited Destroy; 454 Size := Point(0, 0);455 528 end; 456 529
Note:
See TracChangeset
for help on using the changeset viewer.