save.pp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. {
  2. Ported to FPC by Nikolay Nikolov ([email protected])
  3. }
  4. {
  5. Save 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 SaveExample;
  10. {$MODE objfpc}
  11. Uses
  12. ptc, Math;
  13. Procedure save(surface : TPTCSurface; filename : String);
  14. Const
  15. { generate the header for a true color targa image }
  16. header : Array[0..17] Of char8 =
  17. (0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
  18. Var
  19. F : File;
  20. width, height : Integer;
  21. size : Integer;
  22. y : Integer;
  23. pixels : Pchar8;
  24. format : TPTCFormat;
  25. palette : TPTCPalette;
  26. Begin
  27. { open image file for writing }
  28. ASSign(F, filename);
  29. Rewrite(F, 1);
  30. { get surface dimensions }
  31. width := surface.width;
  32. height := surface.height;
  33. { set targa image width }
  34. header[12] := width And $FF;
  35. header[13] := width Shr 8;
  36. { set targa image height }
  37. header[14] := height And $FF;
  38. header[15] := height Shr 8;
  39. { set bits per pixel }
  40. header[16] := 24;
  41. { write tga header }
  42. BlockWrite(F, header, 18);
  43. { calculate size of image pixels }
  44. size := width * height * 3;
  45. { allocate image pixels }
  46. pixels := GetMem(size);
  47. format := TPTCFormat.Create(24, $00FF0000, $0000FF00, $000000FF);
  48. palette := TPTCPalette.Create;
  49. { save surface to image pixels }
  50. surface.save(pixels, width, height, width * 3, format, palette);
  51. palette.Free;
  52. format.Free;
  53. { write image pixels one line at a time }
  54. For y := height - 1 DownTo 0 Do
  55. BlockWrite(F, pixels[width * y * 3], width * 3);
  56. { free image pixels }
  57. FreeMem(pixels);
  58. Close(F);
  59. End;
  60. Function calculate(real, imaginary : Single; maximum : Integer) : Integer;
  61. Var
  62. c_r, c_i : Single;
  63. z_r, z_i : Single;
  64. z_r_squared, z_i_squared : Single;
  65. z_squared_magnitude : Single;
  66. count : Integer;
  67. Begin
  68. { complex number 'c' }
  69. c_r := real;
  70. c_i := imaginary;
  71. { complex 'z' }
  72. z_r := 0;
  73. z_i := 0;
  74. { complex 'z' squares }
  75. z_r_squared := 0;
  76. z_i_squared := 0;
  77. { mandelbrot function iteration loop }
  78. For count := 0 To maximum - 1 Do
  79. Begin
  80. { square 'z' and add 'c' }
  81. z_i := 2 * z_r * z_i + c_i;
  82. z_r := z_r_squared - z_i_squared + c_r;
  83. { update 'z' squares }
  84. z_r_squared := z_r * z_r;
  85. z_i_squared := z_i * z_i;
  86. { calculate squared magnitude of complex 'z' }
  87. z_squared_magnitude := z_r_squared + z_i_squared;
  88. { stop iterating if the magnitude of 'z' is greater than two }
  89. If z_squared_magnitude > 4 Then
  90. Begin
  91. calculate := Count;
  92. Exit;
  93. End;
  94. End;
  95. { maximum }
  96. calculate := 0;
  97. End;
  98. Procedure mandelbrot(console : TPTCConsole; surface : TPTCSurface;
  99. x1, y1, x2, y2 : Single);
  100. Const
  101. { constant values }
  102. entries = 1024;
  103. maximum = 1024;
  104. Var
  105. { fractal color table }
  106. table : Array[0..entries - 1] Of int32;
  107. i : Integer;
  108. f_index : Single;
  109. time : Single;
  110. intensity : Single;
  111. pixels, pixel : Pint32;
  112. width, height : Integer;
  113. dx, dy : Single;
  114. real, imaginary : Single;
  115. x, y : Integer;
  116. count : Integer;
  117. index : Integer;
  118. color : int32;
  119. area : TPTCArea;
  120. Begin
  121. { generate fractal color table }
  122. For i := 0 To entries - 1 Do
  123. Begin
  124. { calculate normalized index }
  125. f_index := i / entries;
  126. { calculate sine curve time value }
  127. time := f_index * pi - pi / 2;
  128. { lookup sine curve intensity at time and scale to [0,1] }
  129. intensity := (sin(time) + 1) / 2;
  130. { raise the intensity to a power }
  131. intensity := power(intensity, 0.1);
  132. { store intensity as a shade of blue }
  133. table[i] := Trunc(255 * intensity);
  134. End;
  135. { lock surface pixels }
  136. pixels := surface.lock;
  137. Try
  138. { get surface dimensions }
  139. width := surface.width;
  140. height := surface.height;
  141. { current pixel pointer }
  142. pixel := pixels;
  143. { calculate real x,y deltas }
  144. dx := (x2 - x1) / width;
  145. dy := (y2 - y1) / height;
  146. { imaginary axis }
  147. imaginary := y1;
  148. { iterate down surface y }
  149. For y := 0 To height - 1 Do
  150. Begin
  151. { real axis }
  152. real := x1;
  153. { iterate across surface x }
  154. For x := 0 To width - 1 Do
  155. Begin
  156. { calculate the mandelbrot interation count }
  157. count := calculate(real, imaginary, maximum);
  158. { calculate color table index }
  159. index := count Mod entries;
  160. { lookup color from iteration }
  161. color := table[index];
  162. { store color }
  163. pixel^ := color;
  164. { next pixel }
  165. Inc(pixel);
  166. { update real }
  167. real := real + dx;
  168. End;
  169. { update imaginary }
  170. imaginary := imaginary + dy;
  171. { setup line area }
  172. area := TPTCArea.Create(0, y, width, y + 1);
  173. Try
  174. { copy surface area to console }
  175. surface.copy(console, area, area);
  176. Finally
  177. area.Free;
  178. End;
  179. { update console area }
  180. console.update;
  181. End;
  182. Finally
  183. { unlock surface }
  184. surface.unlock;
  185. End;
  186. End;
  187. Var
  188. console : TPTCConsole;
  189. surface : TPTCSurface;
  190. format : TPTCFormat;
  191. x1, y1, x2, y2 : Single;
  192. Begin
  193. format := Nil;
  194. surface := Nil;
  195. console := Nil;
  196. Try
  197. Try
  198. { create console }
  199. console := TPTCConsole.Create;
  200. { create format }
  201. format := TPTCFormat.Create(32, $00FF0000, $0000FF00, $000000FF);
  202. { open the console with a single page }
  203. console.open('Save example', format, 1);
  204. { create surface matching console dimensions }
  205. surface := TPTCSurface.Create(console.width, console.height, format);
  206. { setup viewing area }
  207. x1 := -2.00;
  208. y1 := -1.25;
  209. x2 := +1.00;
  210. y2 := +1.25;
  211. { render the mandelbrot fractal }
  212. mandelbrot(console, surface, x1, y1, x2, y2);
  213. { save mandelbrot image }
  214. save(surface, 'save.tga');
  215. { read key }
  216. console.ReadKey;
  217. Finally
  218. console.close;
  219. console.Free;
  220. surface.Free;
  221. format.Free;
  222. End;
  223. Except
  224. On error : TPTCError Do
  225. { report error }
  226. error.report;
  227. End;
  228. End.