OK here's my solution for the general EPS density problem:
INFO: I'm posting this to users & developers list of imagemagick since
this topic was already posted on users list :-)

It solves the following issue:

Consider a User has a EPS (single image, single boundingbox) image with
and wants this to be rendered to use inside ImageMagick

$filename is the filename of input
The EPS contains a boundingbox (in points)
and the user has a X and Y in pixels ($size_x_px, $size_y_px)
$density Is the resulting density

In this case: density is a directly derivated value.

I implemented the following code to parse the EPS, extract the bounding
box parts and calculate the optimum density.

Tested and works within my solution great.

The following source is PHP (and very fast written code)... ;-)

$density = null;
$right = strtolower(substr($filename, -4));

if($right=='.eps')
  {
  # openfile
  $epsh = fopen($source, 'rb');
  $bboxarr = NULL;
  $found = FALSE;
  $i=0;
  $bufferend = '';
  while (!feof($epsh))
    {
    # pass to "%!PS-Adobe-3.0 EPSF-3.0"
    if(!$found)
      {
      $buffer = fread($epsh, 4096);
      #header will be fetched in junks which could be misaligned!
      if(($pos = strpos($bufferend.$buffer, '%!PS-Adobe-3.0
EPSF-3.0'))!==FALSE)
        {
        $i = 0;
        # position curser at comments begin!
        fseek($epsh, -(4096-$pos), SEEK_CUR);
        $found = TRUE;
        }
      if($i>200) { break; } #800k header is too much
      }
    else
      {
      $buffer = fgets($epsh, 1024);
      if($i>20) { break; } #take only some lines of comment
            # pass to "%%BoundingBox:" or "%%HiResBoundingBox:"
            if(strpos($buffer, '%%BoundingBox:')!==FALSE)
              { $bboxarr = split(' ', $buffer); }
            if(strpos($buffer, '%%HiResBoundingBox:')!==FALSE)
              {
              $bboxarr = split(' ', $buffer);
              # nothing more needed!
              break;
              }
      }
    $bufferend = substr($buffer, -25);
    $i++;
    }
  fclose($epsh);
  if($bboxarr!=NULL)
    {
    #Res[dpi] = image_x[pixels]/(bbox_x[points]/72) = 350
    $density_x = $size_x/($bboxarr[3]-$bboxarr[1])*72;
    $density_y = $size_y/($bboxarr[4]-$bboxarr[2])*72;
    #the smaller is dominating! longer side is being reduced
    $density = round(min($density_x, $density_y));
    }
  }

Now this is the $density you need ;-)

I would suggest the ImageMagick team to extract the bbox data and
dynamically calculate the density, if a -size command was given before a
eps file was passed! (modifying coders/ps.c)
Is there anyone who would implement this? (since I'm not having a IM dev
env currently)
But if you like me to suggest a patch, tell me and I'll do it after the
return of my holydays in 3 weeks ;-)

Many thanks to all people improving this great project :-)
(also via online discussions) .-)

--
GrEeZ! Miro


_______________________________________________
Magick-developers mailing list
Magick-developers@imagemagick.org
http://studio.imagemagick.org/mailman/listinfo/magick-developers

Reply via email to