save.pp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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: IPTCSurface; filename: string);
  14. var
  15. F: File;
  16. width, height: Integer;
  17. size: Integer;
  18. y: Integer;
  19. pixels: PUint8 = nil;
  20. format: IPTCFormat;
  21. { generate the header for a true color targa image }
  22. header: array [0..17] of Uint8 =
  23. (0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
  24. begin
  25. { open image file for writing }
  26. AssignFile(F, filename);
  27. Rewrite(F, 1);
  28. try
  29. { get surface dimensions }
  30. width := surface.width;
  31. height := surface.height;
  32. { set targa image width }
  33. header[12] := width and $FF;
  34. header[13] := width shr 8;
  35. { set targa image height }
  36. header[14] := height and $FF;
  37. header[15] := height shr 8;
  38. { set bits per pixel }
  39. header[16] := 24;
  40. { write tga header }
  41. BlockWrite(F, header, 18);
  42. { calculate size of image pixels }
  43. size := width * height * 3;
  44. { allocate image pixels }
  45. pixels := GetMem(size);
  46. {$IFDEF FPC_LITTLE_ENDIAN}
  47. format := TPTCFormatFactory.CreateNew(24, $00FF0000, $0000FF00, $000000FF);
  48. {$ELSE FPC_LITTLE_ENDIAN}
  49. format := TPTCFormatFactory.CreateNew(24, $000000FF, $0000FF00, $00FF0000);
  50. {$ENDIF FPC_LITTLE_ENDIAN}
  51. { save surface to image pixels }
  52. surface.save(pixels, width, height, width * 3, format, TPTCPaletteFactory.CreateNew);
  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. finally
  57. { free image pixels }
  58. FreeMem(pixels);
  59. CloseFile(F);
  60. end;
  61. end;
  62. function calculate(real, imaginary: Single; maximum: Integer): Integer;
  63. var
  64. c_r, c_i: Single;
  65. z_r, z_i: Single;
  66. z_r_squared, z_i_squared: Single;
  67. z_squared_magnitude: Single;
  68. count: Integer;
  69. begin
  70. { complex number 'c' }
  71. c_r := real;
  72. c_i := imaginary;
  73. { complex 'z' }
  74. z_r := 0;
  75. z_i := 0;
  76. { complex 'z' squares }
  77. z_r_squared := 0;
  78. z_i_squared := 0;
  79. { mandelbrot function iteration loop }
  80. for count := 0 to maximum - 1 do
  81. begin
  82. { square 'z' and add 'c' }
  83. z_i := 2 * z_r * z_i + c_i;
  84. z_r := z_r_squared - z_i_squared + c_r;
  85. { update 'z' squares }
  86. z_r_squared := z_r * z_r;
  87. z_i_squared := z_i * z_i;
  88. { calculate squared magnitude of complex 'z' }
  89. z_squared_magnitude := z_r_squared + z_i_squared;
  90. { stop iterating if the magnitude of 'z' is greater than two }
  91. if z_squared_magnitude > 4 then
  92. begin
  93. calculate := Count;
  94. exit;
  95. end;
  96. end;
  97. { maximum }
  98. calculate := 0;
  99. end;
  100. procedure mandelbrot(console: IPTCConsole; surface: IPTCSurface;
  101. x1, y1, x2, y2: Single);
  102. const
  103. { constant values }
  104. entries = 1024;
  105. maximum = 1024;
  106. var
  107. { fractal color table }
  108. table: array [0..entries - 1] of Uint32;
  109. i: Integer;
  110. f_index: Single;
  111. time: Single;
  112. intensity: Single;
  113. pixels, pixel: PUint32;
  114. width, height: Integer;
  115. dx, dy: Single;
  116. real, imaginary: Single;
  117. x, y: Integer;
  118. count: Integer;
  119. index: Integer;
  120. color: Uint32;
  121. area: IPTCArea;
  122. begin
  123. { generate fractal color table }
  124. for i := 0 to entries - 1 do
  125. begin
  126. { calculate normalized index }
  127. f_index := i / entries;
  128. { calculate sine curve time value }
  129. time := f_index * pi - pi / 2;
  130. { lookup sine curve intensity at time and scale to [0,1] }
  131. intensity := (sin(time) + 1) / 2;
  132. { raise the intensity to a power }
  133. intensity := power(intensity, 0.1);
  134. { store intensity as a shade of blue }
  135. table[i] := Trunc(255 * intensity);
  136. end;
  137. { lock surface pixels }
  138. pixels := surface.lock;
  139. try
  140. { get surface dimensions }
  141. width := surface.width;
  142. height := surface.height;
  143. { current pixel pointer }
  144. pixel := pixels;
  145. { calculate real x,y deltas }
  146. dx := (x2 - x1) / width;
  147. dy := (y2 - y1) / height;
  148. { imaginary axis }
  149. imaginary := y1;
  150. { iterate down surface y }
  151. for y := 0 to height - 1 do
  152. begin
  153. { real axis }
  154. real := x1;
  155. { iterate across surface x }
  156. for x := 0 to width - 1 do
  157. begin
  158. { calculate the mandelbrot interation count }
  159. count := calculate(real, imaginary, maximum);
  160. { calculate color table index }
  161. index := count mod entries;
  162. { lookup color from iteration }
  163. color := table[index];
  164. { store color }
  165. pixel^ := color;
  166. { next pixel }
  167. Inc(pixel);
  168. { update real }
  169. real := real + dx;
  170. end;
  171. { update imaginary }
  172. imaginary := imaginary + dy;
  173. { setup line area }
  174. area := TPTCAreaFactory.CreateNew(0, y, width, y + 1);
  175. { copy surface area to console }
  176. surface.copy(console, area, area);
  177. { update console area }
  178. console.update;
  179. end;
  180. finally
  181. { unlock surface }
  182. surface.unlock;
  183. end;
  184. end;
  185. var
  186. console: IPTCConsole;
  187. surface: IPTCSurface;
  188. format: IPTCFormat;
  189. x1, y1, x2, y2: Single;
  190. begin
  191. try
  192. try
  193. { create console }
  194. console := TPTCConsoleFactory.CreateNew;
  195. { create format }
  196. format := TPTCFormatFactory.CreateNew(32, $00FF0000, $0000FF00, $000000FF);
  197. { open the console with a single page }
  198. console.open('Save example', format, 1);
  199. { create surface matching console dimensions }
  200. surface := TPTCSurfaceFactory.CreateNew(console.width, console.height, format);
  201. { setup viewing area }
  202. x1 := -2.00;
  203. y1 := -1.25;
  204. x2 := +1.00;
  205. y2 := +1.25;
  206. { render the mandelbrot fractal }
  207. mandelbrot(console, surface, x1, y1, x2, y2);
  208. { save mandelbrot image }
  209. save(surface, 'save.tga');
  210. { read key }
  211. console.ReadKey;
  212. finally
  213. if Assigned(console) then
  214. console.close;
  215. end;
  216. except
  217. on error: TPTCError do
  218. { report error }
  219. error.report;
  220. end;
  221. end.