/trunk/www/modules/pdf/html2ps_v2042/public_html/ps.l3.image.encoder.stream.inc.php

https://github.com/tsnoad/Irondata · PHP · 250 lines · 145 code · 48 blank · 57 comment · 25 complexity · 5e216dc0640db5a5bb002eaadac27605 MD5 · raw file

  1. <?php
  2. require_once(HTML2PS_DIR.'ps.image.encoder.stream.inc.php');
  3. class PSL3ImageEncoderStream extends PSImageEncoderStream {
  4. function PSL3ImageEncoderStream() {
  5. $this->last_image_id = 0;
  6. }
  7. function auto(&$psdata, $src_img, &$size_x, &$size_y, &$tcolor, &$image, &$mask) {
  8. if (imagecolortransparent($src_img->get_handle()) == -1) {
  9. $id = $this->solid($psdata, $src_img->get_handle(), $size_x, $size_y, $image->get_handle(), $mask);
  10. $tcolor = 0;
  11. return $id;
  12. } else {
  13. $id = $this->transparent($psdata, $src_img->get_handle(), $size_x, $size_y, $image->get_handle(), $mask);
  14. $tcolor = 1;
  15. return $id;
  16. };
  17. }
  18. // Encodes "solid" image without any transparent parts
  19. //
  20. // @param $psdata (in) Postscript file "writer" object
  21. // @param $src_img (in) PHP image resource
  22. // @param $size_x (out) size of image in pixels
  23. // @param $size_y (out) size of image in pixels
  24. // @returns identifier if encoded image to use in postscript file
  25. //
  26. function solid(&$psdata, $src_img, &$size_x, &$size_y, &$image, &$mask) {
  27. // Generate an unique image id
  28. $id = $this->generate_id();
  29. // Determine image size and create a truecolor copy of this image
  30. // (as we don't want to work with palette-based images manually)
  31. $size_x = imagesx($src_img);
  32. $size_y = imagesy($src_img);
  33. $dest_img = imagecreatetruecolor($size_x, $size_y);
  34. imagecopymerge($dest_img, $src_img, 0, 0, 0, 0, $size_x, $size_y, 100);
  35. // write stread header to the postscript file
  36. $psdata->write("/image-{$id}-init { image-{$id}-data 0 setfileposition } def\n");
  37. $psdata->write("/image-{$id}-data currentfile << /Filter /ASCIIHexDecode >> /ReusableStreamDecode filter\n");
  38. // initialize line length counter
  39. $ctr = 0;
  40. for ($y = 0; $y < $size_y; $y++) {
  41. for ($x = 0; $x < $size_x; $x++) {
  42. // Save image pixel to the stream data
  43. $rgb = ImageColorAt($dest_img, $x, $y);
  44. $r = ($rgb >> 16) & 0xFF;
  45. $g = ($rgb >> 8) & 0xFF;
  46. $b = $rgb & 0xFF;
  47. $psdata->write(sprintf("%02X%02X%02X",min(max($r,0),255),min(max($g,0),255),min(max($b,0),255)));
  48. // Increate the line length counter; check if stream line needs to be terminated
  49. $ctr += 6;
  50. if ($ctr > MAX_LINE_LENGTH) {
  51. $psdata->write("\n");
  52. $ctr = 0;
  53. }
  54. };
  55. };
  56. // terminate the stream data
  57. $psdata->write(">\ndef\n");
  58. // return image and mask data references
  59. $image = "image-{$id}-data";
  60. $mask = "";
  61. return $id;
  62. }
  63. // Encodes image containing 100% transparent color (1-bit alpha channel)
  64. //
  65. // @param $psdata (in) Postscript file "writer" object
  66. // @param $src_img (in) PHP image resource
  67. // @param $size_x (out) size of image in pixels
  68. // @param $size_y (out) size of image in pixels
  69. // @returns identifier if encoded image to use in postscript file
  70. //
  71. function transparent(&$psdata, $src_img, &$size_x, &$size_y, &$image, &$mask) {
  72. // Generate an unique image id
  73. $id = $this->generate_id();
  74. // Store transparent color for further reference
  75. $transparent = imagecolortransparent($src_img);
  76. // Determine image size and create a truecolor copy of this image
  77. // (as we don't want to work with palette-based images manually)
  78. $size_x = imagesx($src_img);
  79. $size_y = imagesy($src_img);
  80. $dest_img = imagecreatetruecolor($size_x, $size_y);
  81. imagecopymerge($dest_img, $src_img, 0, 0, 0, 0, $size_x, $size_y, 100);
  82. // write stread header to the postscript file
  83. $psdata->write("/image-{$id}-init { image-{$id}-data 0 setfileposition mask-{$id}-data 0 setfileposition } def\n");
  84. // Create IMAGE data stream
  85. $psdata->write("/image-{$id}-data currentfile << /Filter /ASCIIHexDecode >> /ReusableStreamDecode filter\n");
  86. // initialize line length counter
  87. $ctr = 0;
  88. for ($y = 0; $y < $size_y; $y++) {
  89. for ($x = 0; $x < $size_x; $x++) {
  90. // Save image pixel to the stream data
  91. $rgb = ImageColorAt($dest_img, $x, $y);
  92. $r = ($rgb >> 16) & 0xFF;
  93. $g = ($rgb >> 8) & 0xFF;
  94. $b = $rgb & 0xFF;
  95. $psdata->write(sprintf("%02X%02X%02X",$r,$g,$b));
  96. // Increate the line length counter; check if stream line needs to be terminated
  97. $ctr += 6;
  98. if ($ctr > MAX_LINE_LENGTH) {
  99. $psdata->write("\n");
  100. $ctr = 0;
  101. }
  102. };
  103. };
  104. // terminate the stream data
  105. $psdata->write(">\ndef\n");
  106. // Create MASK data stream
  107. $psdata->write("/mask-{$id}-data currentfile << /Filter /ASCIIHexDecode >> /ReusableStreamDecode filter\n");
  108. // initialize line length counter
  109. $ctr = 0;
  110. // initialize mask bit counter
  111. $bit_ctr = 0;
  112. $mask_data = 0xff;
  113. for ($y = 0; $y < $size_y; $y++) {
  114. for ($x = 0; $x < $size_x; $x++) {
  115. // Check if this pixel should be transparent
  116. if (ImageColorAt($src_img, $x, $y) == $transparent) {
  117. $mask_data = ($mask_data << 1) | 0x1;
  118. } else {
  119. $mask_data = ($mask_data << 1);
  120. };
  121. $bit_ctr ++;
  122. // If we've filled the whole byte, write it into the mask data stream
  123. if ($bit_ctr >= 8 || $x + 1 == $size_x) {
  124. // Pad mask data, in case we have completed the image row
  125. while ($bit_ctr < 8) {
  126. $mask_data = ($mask_data << 1) | 0x01;
  127. $bit_ctr ++;
  128. };
  129. $psdata->write(sprintf("%02X", $mask_data & 0xff));
  130. // Clear mask data after writing
  131. $mask_data = 0xff;
  132. $bit_ctr = 0;
  133. // Increate the line length counter; check if stream line needs to be terminated
  134. $ctr += 1;
  135. if ($ctr > MAX_LINE_LENGTH) {
  136. $psdata->write("\n");
  137. $ctr = 0;
  138. }
  139. };
  140. };
  141. };
  142. // terminate the stream data
  143. // Write any incomplete mask byte to the mask data stream
  144. if ($bit_ctr != 0) {
  145. while ($bit_ctr < 8) {
  146. $mask_data <<= 1;
  147. $mask_data |= 1;
  148. $bit_ctr ++;
  149. }
  150. $psdata->write(sprintf("%02X", $mask_data));
  151. };
  152. $psdata->write(">\ndef\n");
  153. // return image and mask data references
  154. $image = "image-{$id}-data";
  155. $mask = "mask-{$id}-data";
  156. return $id;
  157. }
  158. function alpha(&$psdata, $src_img, &$size_x, &$size_y, &$image, &$mask) {
  159. // Generate an unique image id
  160. $id = $this->generate_id();
  161. // Determine image size
  162. $size_x = imagesx($src_img);
  163. $size_y = imagesy($src_img);
  164. // write stread header to the postscript file
  165. $psdata->write("/image-{$id}-init { image-{$id}-data 0 setfileposition } def\n");
  166. $psdata->write("/image-{$id}-data currentfile << /Filter /ASCIIHexDecode >> /ReusableStreamDecode filter\n");
  167. // initialize line length counter
  168. $ctr = 0;
  169. // Save visible background color
  170. $handler =& CSS::get_handler(CSS_BACKGROUND_COLOR);
  171. $bg = $handler->get_visible_background_color();
  172. for ($y = 0; $y < $size_y; $y++) {
  173. for ($x = 0; $x < $size_x; $x++) {
  174. // Check color/alpha of current pixels
  175. $colors = imagecolorsforindex($src_img, imagecolorat($src_img, $x, $y));
  176. $a = $colors['alpha'];
  177. $r = $colors['red'];
  178. $g = $colors['green'];
  179. $b = $colors['blue'];
  180. // Calculate approximate color
  181. $r = (int)($r + ($bg[0] - $r)*$a/127);
  182. $g = (int)($g + ($bg[1] - $g)*$a/127);
  183. $b = (int)($b + ($bg[2] - $b)*$a/127);
  184. // Save image pixel to the stream data
  185. $psdata->write(sprintf("%02X%02X%02X",$r,$g,$b));
  186. // Increate the line length counter; check if stream line needs to be terminated
  187. $ctr += 6;
  188. if ($ctr > MAX_LINE_LENGTH) {
  189. $psdata->write("\n");
  190. $ctr = 0;
  191. }
  192. };
  193. };
  194. // terminate the stream data
  195. $psdata->write(">\ndef\n");
  196. // return image and mask data references
  197. $image = "image-{$id}-data";
  198. $mask = "";
  199. return $id;
  200. }
  201. }
  202. ?>