image.pp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. {
  2. Ported to FPC by Nikolay Nikolov ([email protected])
  3. }
  4. {
  5. Image example for OpenPTC 1.0 C++ implementation
  6. Copyright (c) Glenn Fiedler ([email protected])
  7. This source code is in the public domain
  8. }
  9. program ImageExample;
  10. {$MODE objfpc}
  11. uses
  12. SysUtils, ptc;
  13. procedure load(surface: TPTCSurface; filename: String);
  14. var
  15. F: File;
  16. width, height: Integer;
  17. pixels: PByte = nil;
  18. y: Integer;
  19. img_format: TPTCFormat = nil;
  20. img_palette: TPTCPalette = nil;
  21. begin
  22. { open image file }
  23. AssignFile(F, filename);
  24. Reset(F, 1);
  25. try
  26. { skip header }
  27. Seek(F, 18);
  28. { get surface dimensions }
  29. width := surface.width;
  30. height := surface.height;
  31. { allocate image pixels }
  32. pixels := GetMem(width * height * 3);
  33. { read image pixels one line at a time }
  34. for y := height - 1 DownTo 0 do
  35. BlockRead(F, pixels[width * y * 3], width * 3);
  36. { load pixels to surface }
  37. {$IFDEF FPC_LITTLE_ENDIAN}
  38. img_format := TPTCFormat.Create(24, $00FF0000, $0000FF00, $000000FF);
  39. {$ELSE FPC_LITTLE_ENDIAN}
  40. img_format := TPTCFormat.Create(24, $000000FF, $0000FF00, $00FF0000);
  41. {$ENDIF FPC_LITTLE_ENDIAN}
  42. img_palette := TPTCPalette.Create;
  43. surface.load(pixels, width, height, width * 3, img_format, img_palette);
  44. finally
  45. CloseFile(F);
  46. { free image pixels }
  47. FreeMem(pixels);
  48. img_palette.Free;
  49. img_format.Free;
  50. end;
  51. end;
  52. var
  53. console: TPTCConsole = nil;
  54. format: TPTCFormat = nil;
  55. surface: TPTCSurface = nil;
  56. begin
  57. try
  58. try
  59. { create console }
  60. console := TPTCConsole.Create;
  61. { create format }
  62. format := TPTCFormat.Create(32, $00FF0000, $0000FF00, $000000FF);
  63. try
  64. { try to open the console matching the image resolution }
  65. console.open('Image example', 320, 200, format);
  66. except
  67. on TPTCError do
  68. { fallback to the default resolution }
  69. console.open('Image example', format);
  70. end;
  71. { create surface }
  72. surface := TPTCSurface.Create(320, 200, format);
  73. { load image to surface }
  74. load(surface, 'image.tga');
  75. { copy surface to console }
  76. surface.copy(console);
  77. { update console }
  78. console.update;
  79. { read key }
  80. console.ReadKey;
  81. finally
  82. { close console }
  83. console.close;
  84. console.Free;
  85. surface.Free;
  86. format.Free;
  87. end;
  88. except
  89. on error: TPTCError do
  90. { report error }
  91. error.report;
  92. end;
  93. end.