1<?php
2
3require_once(HTML2PS_DIR.'ps.image.encoder.stream.inc.php');
4
5/**
6 * Deprecated. Big. Slow. Causes /limitcheck Ghostcript error on big images. Use
7 * another encoder.
8 * @author Konstantin Bournayev
9 * @version 1.0
10 * @updated 24-���-2006 21:18:30
11 */
12class PSImageEncoderSimple extends PSImageEncoderStream {
13  function PSImageEncoderSimple() {
14  }
15
16  function auto($psdata, $src_img, &$size_x, &$size_y, &$tcolor, &$image, &$mask) {
17    if (imagecolortransparent($src_img) == -1) {
18      $id = $this->solid($psdata,
19                         $src_img->get_handle(),
20                         $size_x,
21                         $size_y,
22                         $image->get_handle(),
23                         $mask);
24      $tcolor = 0;
25      return $id;
26    } else {
27      $id = $this->transparent($psdata,
28                               $src_img->get_handle(),
29                               $size_x,
30                               $size_y,
31                               $image->get_handle(),
32                               $mask);
33      $tcolor = 1;
34      return $id;
35    };
36  }
37
38  function solid($psdata, $src_img, &$size_x, &$size_y, &$image, &$mask) {
39    $id = $this->generate_id();
40
41    $size_x      = imagesx($src_img);
42    $size_y      = imagesy($src_img);
43    $dest_img    = imagecreatetruecolor($size_x, $size_y);
44
45    imagecopymerge($dest_img, $src_img, 0, 0, 0, 0, $size_x, $size_y, 100);
46
47    $ps_image_data = "";
48    $ctr = 1; $row = 1;
49
50    for ($y = 0; $y < $size_y; $y++) {
51      for ($x = 0; $x < $size_x; $x++) {
52        // Image pixel
53        $rgb = ImageColorAt($dest_img, $x, $y);
54        $r = ($rgb >> 16) & 0xFF;
55        $g = ($rgb >> 8) & 0xFF;
56        $b = $rgb & 0xFF;
57        $ps_image_data .= sprintf("\\%03o\\%03o\\%03o",$r,$g,$b);
58
59        // Write image rows
60        $ctr++;
61        if ($ctr > MAX_IMAGE_ROW_LEN || ($x + 1 == $size_x)) {
62          $row_next = ($size_x - $x - 1 + $size_x * ($size_y - $y - 1) == 0) ? 1 : $row+1;
63          $psdata->write("/row-{$id}-{$row} { /image-{$id}-data { row-{$id}-{$row_next} } def ({$ps_image_data}) } def\n");
64
65          $ps_image_data = "";
66          $ctr = 1; $row += 1;
67        };
68      };
69    };
70
71    if ($ps_image_data) {
72      $psdata->write("/row-{$id}-{$row}  { /image-{$id}-data { row-{$id}-1 } def ({$ps_image_data}) } def\n");
73    };
74
75    $psdata->write("/image-{$id}-data { row-{$id}-1 } def\n");
76    $psdata->write("/image-{$id}-init { } def\n");
77
78    // return image and mask data references
79    $image = "{image-{$id}-data}";
80    $mask  = "";
81
82    return $id;
83  }
84
85  function transparent($psdata, $src_img, &$size_x, &$size_y, &$image, &$mask) {
86    $id = $this->generate_id();
87
88    $size_x      = imagesx($src_img);
89    $size_y      = imagesy($src_img);
90    $transparent = imagecolortransparent($src_img);
91    $dest_img    = imagecreatetruecolor($size_x, $size_y);
92
93    imagecopymerge($dest_img, $src_img, 0, 0, 0, 0, $size_x, $size_y, 100);
94
95    $ps_image_data = "";
96    $ps_mask_data  = 0xff;
97    $ctr = 1; $row = 1;
98
99    $handler =& CSS::get_handler(CSS_BACKGROUND_COLOR);
100    $background_color = $handler->get_visible_background_color();
101
102    for ($y = 0; $y < $size_y; $y++) {
103      for ($x = 0; $x < $size_x; $x++) {
104        // Image pixel
105        $rgb = ImageColorAt($dest_img, $x, $y);
106        $r = ($rgb >> 16) & 0xFF;
107        $g = ($rgb >> 8) & 0xFF;
108        $b = $rgb & 0xFF;
109
110        // Mask pixel
111        if (ImageColorAt($src_img, $x, $y) == $transparent) {
112          $ps_mask_data = ($ps_mask_data << 1) | 0x1;
113          // Also, reset the image colors to the visible background to work correctly
114          // while using 'transparency hack'
115          $r = $background_color[0];
116          $g = $background_color[1];
117          $b = $background_color[2];
118        } else {
119          $ps_mask_data = ($ps_mask_data << 1) | 0;
120        };
121
122        $ps_image_data .= sprintf("\\%03o\\%03o\\%03o",$r,$g,$b);
123
124        // Write mask and image rows
125        $ctr++;
126        if ($ctr > MAX_TRANSPARENT_IMAGE_ROW_LEN || ($x + 1 == $size_x)) {
127          while ($ctr <= 8) {
128            $ps_mask_data <<= 1;
129            $ps_mask_data |= 1;
130            $ctr ++;
131          };
132
133          $ps_mask_data_str = sprintf("\\%03o",$ps_mask_data & 0xff);
134
135          $row_next = ($size_x - $x - 1 + $size_x * ($size_y - $y - 1) == 0) ? 1 : $row+1;
136
137          $psdata->write("/row-{$id}-{$row} { /image-{$id}-data { row-{$id}-{$row_next} } def ({$ps_image_data}) } def\n");
138          $psdata->write("/mrow-{$id}-{$row} { /mask-{$id}-data { mrow-{$id}-{$row_next} } def ({$ps_mask_data_str}) } def\n");
139
140          $ps_image_data = "";
141          $ps_mask_data  = 0xff;
142          $ctr = 1; $row += 1;
143        };
144      };
145    };
146
147    if ($ps_image_data) {
148      while ($ctr <= 8) {
149        $ps_mask_data <<= 1;
150        $ps_mask_data |= 1;
151        $ctr ++;
152      };
153      $ps_mask_data_str = sprintf("\\%03o",$ps_mask_data & 0xFF);
154
155      $psdata->write("/row-{$id}-{$row} { /image-{$id}-data { row-{$id}-{$row_next} } def ({$ps_image_data}) } def\n");
156      $psdata->write("/mrow-{$id}-{$row} { /mask-{$id}-data { mrow-{$id}-{$row_next} } def ({$ps_mask_data_str}) } def\n");
157    };
158
159    $psdata->write("/image-{$id}-data { row-{$id}-1 } def\n");
160    $psdata->write("/mask-{$id}-data  { mrow-{$id}-1 } def\n");
161    $psdata->write("/image-{$id}-init { } def\n");
162
163    // return image and mask data references
164    $image = "{image-{$id}-data}";
165    $mask  = "{mask-{$id}-data}";
166
167    return $id;
168  }
169
170  function alpha($psdata, $src_img, &$size_x, &$size_y, &$image, &$mask) {
171    $id = $this->generate_id();
172
173    $size_x      = imagesx($src_img);
174    $size_y      = imagesy($src_img);
175
176    $ps_image_data = "";
177    $ps_mask_data  = 0xff;
178    $ctr = 1; $row = 1;
179
180    for ($y = 0; $y < $size_y; $y++) {
181      for ($x = 0; $x < $size_x; $x++) {
182        // Mask pixel
183        $colors = imagecolorsforindex($src_img, imagecolorat($src_img, $x, $y));
184
185        $a = $colors['alpha'];
186        $r = $colors['red'];
187        $g = $colors['green'];
188        $b = $colors['blue'];
189
190        $handler =& CSS::get_handler(CSS_BACKGROUND_COLOR);
191        $bg = $handler->get_visible_background_color();
192        $r = (int)($r + ($bg[0] - $r)*$a/127);
193        $g = (int)($g + ($bg[1] - $g)*$a/127);
194        $b = (int)($b + ($bg[2] - $b)*$a/127);
195
196        $ps_image_data .= sprintf("\\%03o\\%03o\\%03o",$r,$g,$b);
197
198        // Write mask and image rows
199        $ctr++;
200        if ($ctr > MAX_IMAGE_ROW_LEN || ($x + 1 == $size_x)) {
201          $row_next = ($size_x - $x - 1 + $size_x * ($size_y - $y - 1) == 0) ? 1 : $row+1;
202
203          $psdata->write("/row-{$id}-{$row} { /image-{$id}-data { row-{$id}-{$row_next} } def ({$ps_image_data}) } def\n");
204
205          $ps_image_data = "";
206          $ctr = 1; $row += 1;
207        };
208      };
209    };
210
211    if ($ps_image_data) {
212      $psdata->write("/row-{$id}-{$row} { /image-{$id}-data { row-{$id}-{$row_next} } def ({$ps_image_data}) } def\n");
213    };
214
215    $psdata->write("/image-{$id}-data { row-{$id}-1 } def\n");
216    $psdata->write("/image-{$id}-init { } def\n");
217
218    // return image and mask data references
219    $image = "{image-{$id}-data}";
220    $mask  = "";
221
222    return $id;
223  }
224
225}
226?>