image.pp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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: IPTCSurface; filename: String);
  14. var
  15. F: File;
  16. width, height: Integer;
  17. pixels: PByte = nil;
  18. y: Integer;
  19. img_format: IPTCFormat;
  20. begin
  21. { open image file }
  22. AssignFile(F, filename);
  23. Reset(F, 1);
  24. try
  25. { skip header }
  26. Seek(F, 18);
  27. { get surface dimensions }
  28. width := surface.width;
  29. height := surface.height;
  30. { allocate image pixels }
  31. pixels := GetMem(width * height * 3);
  32. { read image pixels one line at a time }
  33. for y := height - 1 DownTo 0 do
  34. BlockRead(F, pixels[width * y * 3], width * 3);
  35. { load pixels to surface }
  36. {$IFDEF FPC_LITTLE_ENDIAN}
  37. img_format := TPTCFormatFactory.CreateNew(24, $00FF0000, $0000FF00, $000000FF);
  38. {$ELSE FPC_LITTLE_ENDIAN}
  39. img_format := TPTCFormatFactory.CreateNew(24, $000000FF, $0000FF00, $00FF0000);
  40. {$ENDIF FPC_LITTLE_ENDIAN}
  41. surface.Load(pixels, width, height, width * 3, img_format, TPTCPaletteFactory.CreateNew);
  42. finally
  43. CloseFile(F);
  44. { free image pixels }
  45. FreeMem(pixels);
  46. end;
  47. end;
  48. var
  49. console: IPTCConsole;
  50. format: IPTCFormat;
  51. surface: IPTCSurface;
  52. begin
  53. try
  54. try
  55. { create console }
  56. console := TPTCConsoleFactory.CreateNew;
  57. { create format }
  58. format := TPTCFormatFactory.CreateNew(32, $00FF0000, $0000FF00, $000000FF);
  59. try
  60. { try to open the console matching the image resolution }
  61. console.open('Image example', 320, 200, format);
  62. except
  63. on TPTCError do
  64. { fallback to the default resolution }
  65. console.open('Image example', format);
  66. end;
  67. { create surface }
  68. surface := TPTCSurfaceFactory.CreateNew(320, 200, format);
  69. { load image to surface }
  70. load(surface, 'image.tga');
  71. { copy surface to console }
  72. surface.copy(console);
  73. { update console }
  74. console.update;
  75. { read key }
  76. console.ReadKey;
  77. finally
  78. { close console }
  79. if Assigned(console) then
  80. console.close;
  81. end;
  82. except
  83. on error: TPTCError do
  84. { report error }
  85. error.report;
  86. end;
  87. end.