Google Answers Logo
View Question
 
Q: How to calculate pixels per inch from Photoshop EPS header ( Answered 5 out of 5 stars,   0 Comments )
Question  
Subject: How to calculate pixels per inch from Photoshop EPS header
Category: Computers > Graphics
Asked by: uriah-ga
List Price: $15.00
Posted: 22 Oct 2002 10:23 PDT
Expires: 21 Nov 2002 09:23 PST
Question ID: 88091
I wish to calculate PPI (pixels per inch) of a Photoshop EPS file by
parsing the header information. This will save me a lot of time by not
having to open each document, at 8 to 30 MB each, and get the PPI from
Photoshop via AppleScript or VB.

Adobe supplies some file format documentation on page 20 of this PDF
document http://www.csdn.net/Dev/Format/graphics/PSD.pdf

I created the Photoshop EPS document at 300 PPI with a height of 1.5"
(450 pixels) and a width of 2" (600 pixels). The header of the
Photoshop EPS file is:

%!PS-Adobe-3.0 EPSF-3.0
%%Creator: Adobe Photoshop Version v5.0.2
%%Title: 300-GOOGLE.eps
%%CreationDate: 10/22/02 12:11 PM
%%BoundingBox: 0 0 108 144
%%HiResBoundingBox: 0 0 108 144
%%SuppressDotGainCompensation
%%EndComments
%%BeginProlog
%%EndProlog
%%BeginSetup
%%EndSetup
%ImageData: 450 600 8 3 1 450 1 "beginimage"

I'm looking for how to calculate 300 PPI from the above header information. I have
put a copy of  this file at http://sql.widen.com/uriah/300-google.eps

Thanks.
Answer  
Subject: Re: How to calculate pixels per inch from Photoshop EPS header
Answered By: duncan2-ga on 22 Oct 2002 12:33 PDT
Rated:5 out of 5 stars
 
Hello uriah,

That's an interesting question.  I have to admit that I've wondered
the same thing in the past, when placing EPS files into other
documents.  I don't think that the information you're seeking is
available easily online, but I do have a possible solution for you.

As you're probably aware, the EPS file format can contain both raster
(bitmapped) and vector graphic information.  Frequently EPS files are
used for scalable clipart in production.  Similarly, there are a
variety of acceptable data formats for EPS files, including ASCII or
Binary encoding, with and without embedded previews, and including
previews in various color depths.

I note from the file header you provided that you're using Photoshop
5, generating an EPS file compliant with version 3 of the EPS
specification.  If you're curious about the details, you can find a
copy of the EPS 3.0 Specification document mirrored in various
locations on the net, including here:
http://www.isg.inf.ethz.ch/docu/documents/postscript/5002.EPSF_Spec_v3.0.pdf

The key here though, is that the EPS header does not contain the DPI
information for the raster graphic, just the bounding box for the
default resolution.  The actual DPI information is stored with the
image data in the (undocumented?) Photoshop data.

This data is the Photoshop-specific information that is entered into
the file, starting with the %BeginPhotoshop header.  I did a simple
test, generating two identical blank eps files from Photoshop, at
different resolutions and then compared the files for differences
using the free utility WinMerge ( http://winmerge.sourceforge.net/ )

The first line after the %BeginPhotoshop varies slightly!

(image at 72 dpi)
%BeginPhotoshop: 1624
% 3842494D03ED0A5265736F6C7574696F6E000000001000480000000100010048 
                                                ^^              ^^
(image at 600 dpi)
%BeginPhotoshop: 1624
% 3842494D03ED0A5265736F6C7574696F6E000000001002580000000100010258
                                               ^^^             ^^^
So what are these numbers?  Well, 72 in Hexadecimal is 48, and 600 in
hexadecimal is 258.  Bingo!

So the simplest answer to your question is probably, read the last 4
characters of the line immediately after %BeginPhotoshop, and convert
from Hexadecimal.  Naturally this solution is particular to
Photoshop-generated EPS files.  And of course, if the EPS has been
saved in Binary encoding instead of ASCII, text processing of the file
is unlikely to be feasible.

I hope this answers your question satisfactorily.  If you need
additional information about what I've discussed, please don't
hesitate to post a request for clarification prior to rating.

Thanks,
duncan2-ga

Request for Answer Clarification by uriah-ga on 22 Oct 2002 13:12 PDT
Hello duncan2, very nice, thank you. What OS and version Photoshop did
you use for your tests? In my tests the hex-encoded ppi is line 5
chars 36,37,38 in the %BeginPhotoshop section. (The underscores are my
emphasis.)

[72 DPI File]
%BeginPhotoshop: 516
% 3842494D03E900000000007800030000004800480000000002D80228FFE1FFE2
% 02F902460347052803FC00020000004800480000000002D80228000100000064
% 000000010003030300000001270F000100010000000000000000000000006008
% 0019019000000000000000000000000000000000000000000000000000000000
% 000000003842494D03ED0000000000100_048_0000000100010048000000010001
% 3842494D040D000000000004000000783842494D03F300000000000800000000
% 000000003842494D040A00000000000100003842494D271000000000000A0001
% 00000000000000023842494D03F5000000000048002F66660001006C66660006
% 000000000001002F6666000100A1999A0006000000000001003200000001005A
% 00000006000000000001003500000001002D000000060000000000013842494D
% 03F80000000000700000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
% 03E800000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF03E80000
% 0000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF03E800000000FFFF
% FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF03E800003842494D04080000
% 00000010000000010000024000000240000000003842494D0411000000000001
% 01003842494D0414000000000004000000023842494D03FD0000000000060201
% 00000000
%EndPhotoshop

I'm fairly sure this will work for my situation.

Clarification of Answer by duncan2-ga on 22 Oct 2002 13:14 PDT
After further testing, it seems that this might not be as simple as I
had initially proposed.  The DPI information does appear to be in the
BeginPhotoshop data, but may not appear at the end of the line.  I'll
do some more testing to see if I can verify the exact formula for
finding the dpi and/or some resources that illustrate how the
BeginPhotoshop data is written.

Clarification of Answer by duncan2-ga on 22 Oct 2002 13:55 PDT
Ok, after looking at this further I think I’m closer.  First, I did my
test in Photoshop 6.  My test images worked fine, but subsequently
I've managed to generate files that don't have the dpi info at the end
of the line (similar to the one from version 5 which you provided.) 
This indicates that the header is of variable length, not static as I
had previously assumed. Oops.  Sorry.

The good news however is that the contents of the %BeginPhotoshop
section are indeed hexadecimal, and the first few characters when
translated from Hex to ASCII read "8BIM", which is a standard header
indicator for Photoshop.

It's likely that this data is a Photoshop image resource block; see
page 7 of the PDF you referenced in your question.

The following page may also be of use, as it gives some details about
parsing 8BIM headers:
http://www.codeproject.com/bitmap/iptc.asp

Clarification of Answer by duncan2-ga on 22 Oct 2002 14:14 PDT
The DPI is likely found in the ResolutionInfo structure, mentioned
half-way down page 7 of the PDF you linked.  The structure begins with
03ED (you can see this in each of the samples I pasted and in the file
you provided).  To actually parse the structure correctly for DPI,
it's likely that you'll need to find the Photoshop SDK Guide or get
the information from Adobe's Developer Relations.

Clarification of Answer by duncan2-ga on 22 Oct 2002 15:32 PDT
Ok, this will probably be my last post on this question.  I just
wanted to point out that since the opensource program GIMP (the Gnu
Image Manipulation Program) reads Photoshop documents, there is
already available source code that demonstrates parsing the Photoshop
image header.  If you're curious you can download the source code from
various mirrors mentioned at www.gimp.org.  In particular, from the
PSD.C file of the source code, I find:

case 0x03ed:
	{
	  IFDBG printf ("\t\tResolution Info:\n");
	  psd_image.resolution_is_set = 1;

	  psd_image.resolution.hRes = getglong(fd, "hRes");
	  psd_image.resolution.hRes_unit = getgshort(fd, "hRes_unit");
	  psd_image.resolution.widthUnit = getgshort(fd, "WidthUnit");
	  psd_image.resolution.vRes = getglong(fd, "vRes");
	  psd_image.resolution.vRes_unit = getgshort(fd, "vRes_unit");
	  psd_image.resolution.heightUnit = getgshort(fd, "HeightUnit");
	  (*offset) += Size;
	  IFDBG  printf("\t\t\tres = %f, %f\n",
			psd_image.resolution.hRes / 65536.0, 
			psd_image.resolution.vRes / 65536.0);
	} break;

So it looks like there are 6 pieces of information in the
ResolutionInfo structure that follows the 0x03ED flag.

Notice how my first answer showed the DPI in two places?  That because
one's the horizontal resolution and one's the vertical.  (I'd imagine
for any Photoshop-generated bitmap that the DPI would be equal for
horizontal and vertical.)

With this information you may be able to write a program or script
that can properly parse the ResolutionInfo structure after you locate
it in the header.

Regards,
duncan2-ga
uriah-ga rated this answer:5 out of 5 stars

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy