2
0

SimpleImageLoader.pas 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. (********************************************)
  2. (* *)
  3. (* DelphiCL *)
  4. (* *)
  5. (* created by : Maksym Tymkovych *)
  6. (* (niello) *)
  7. (* *)
  8. (* headers versions: 0.07 *)
  9. (* file name : SimpleImageLoader.pas*)
  10. (* last modify : 10.12.11 *)
  11. (* license : BSD *)
  12. (* *)
  13. (* Site : www.niello.org.ua *)
  14. (* e-mail : [email protected] *)
  15. (* ICQ : 446-769-253 *)
  16. (* *)
  17. (*********Copyright (c) niello 2008-2011*****)
  18. unit SimpleImageLoader;
  19. interface
  20. uses
  21. CL_platform,
  22. CL,
  23. Graphics;
  24. type
  25. PArrayByte = ^TArrayByte;
  26. TArrayByte = array [0..0] of TCL_uchar4;
  27. TImageLoader = class
  28. private
  29. FWidth: Integer;
  30. FHeight: Integer;
  31. FPtr: PArrayByte;
  32. FFormat: TCL_image_format;
  33. procedure SetPointer(const Ptr: Pointer);
  34. function GetPointer: Pointer;
  35. function GetFormat: PCL_image_format;
  36. public
  37. constructor Create(const FileName: String); overload;
  38. constructor Create; overload;
  39. destructor Destroy; override;
  40. procedure SaveToFile(const FileName: String);
  41. procedure Resize(const Width, Height: Integer);
  42. property Width: Integer read FWidth;
  43. property Height: Integer read FHeight;
  44. property Pointer: Pointer read GetPointer write SetPointer;
  45. property Format: PCL_image_format read GetFormat;
  46. end;
  47. implementation
  48. { TImageLoader }
  49. constructor TImageLoader.Create(const FileName: String);
  50. var
  51. bmp: Graphics.TBitmap;
  52. i,j: Integer;
  53. row: PArrayByte;
  54. begin
  55. inherited Create;
  56. bmp := TBitmap.Create;
  57. bmp.LoadFromFile(FileName);
  58. FWidth := bmp.Width;
  59. FHeight := bmp.Height;
  60. bmp.PixelFormat := pf32bit;
  61. FPtr:=GetMemory(FWidth*FHeight*4);
  62. for i:=0 to FHeight-1 do
  63. begin
  64. row := bmp.ScanLine[i];
  65. for j:=0 to FWidth-1 do
  66. begin
  67. FPtr^[i*FWidth+j].u8 := row^[j].u8;
  68. end;
  69. end;
  70. FFormat.Image_channel_order := CL_BGRA; //BMP - BGRA file
  71. FFormat.Image_channel_data_type := CL_UNSIGNED_INT8;
  72. bmp.Free;
  73. end;
  74. constructor TImageLoader.Create;
  75. begin
  76. inherited Create;
  77. FFormat.Image_channel_order := CL_BGRA; //BMP - BGRA file
  78. FFormat.Image_channel_data_type := CL_UNSIGNED_INT8;
  79. end;
  80. destructor TImageLoader.Destroy;
  81. begin
  82. Dispose(FPtr);
  83. inherited;
  84. end;
  85. procedure TImageLoader.SaveToFile(const FileName: String);
  86. var
  87. bmp: TBitmap;
  88. i,j: integer;
  89. row: PArrayByte;
  90. begin
  91. bmp:=TBitmap.Create;
  92. bmp.Width := Width;
  93. bmp.Height := Height;
  94. bmp.PixelFormat := pf32bit;
  95. for i:=0 to FHeight-1 do
  96. begin
  97. row := bmp.ScanLine[i];
  98. for j:=0 to FWidth-1 do
  99. begin
  100. row^[j].u8 := FPtr^[(i*FWidth+j)].u8;
  101. end;
  102. end;
  103. bmp.SaveToFile(FileName);
  104. bmp.Free;
  105. end;
  106. function TImageLoader.GetFormat: PCL_image_format;
  107. begin
  108. Result := @FFormat;
  109. end;
  110. function TImageLoader.GetPointer: Pointer;
  111. begin
  112. Result := FPtr;
  113. end;
  114. procedure TImageLoader.SetPointer(const Ptr: Pointer);
  115. begin
  116. FPtr := Ptr;
  117. end;
  118. procedure TImageLoader.Resize(const Width, Height: Integer);
  119. begin
  120. Dispose(FPtr);
  121. FPtr := GetMemory(FWidth * FHeight * 4);
  122. end;
  123. end.