Recognize psd file masquerading as jpg

I’ve got an image file with a jpg name but in reality it’s a psd file with an embedded jpg. I know how to recognize a jpg. But how do I do that with a psd file? I don’t need to know any details about the psd file. Just like I can do with a jpg

if StartOfPicture.MidB(6,4) = "JFIF" or StartOfPicture.MidB(6,4) = "Exif" then 'is jpg

I want to make sure that the file can be displayed. As usual plugin solutions are welcome.

OpenAsPicture() followed by if thereturnedpicture <> nil then

Is how I usually handle it.

@Jeff Tullin: that part of my code is working. I check if I can do a preview. If there is no preview then I get the icon for the file extension. But how do I get a preview for the file/data?

You may read a 200 MB file from a network volume just to check whether it may be a readable image?
Block the app for maybe 10 seconds here?

For PSD, a check in the first bytes may also decide this.

From my C++ code library:

bool IsPSD(void* data, size_t len) { return (len>6) && (memcmp(data, "8BPS", 4) == 0); }

Only if Im trying to actually use it. :slight_smile:
The file open dialog usually has a preview available.
I wouldn’t use that method if I was trying to write a ‘preview the contents of this folder’ application.

@Christian Schmitz : this doesn’t seem to fit my file.

Do you have other files of this type? Do those first 6 bytes match for all of them?

All photoshop files I have here show 8BPS as first bytes.

I don’t think that this file is a normal PSD.

If you’re targeting the macOS only, you can use CGImageSource to read/create the preview, I’m pretty certain that you have the MBS plugin, so check with Christian on how to do. You can also use Spotlight to read the preview.

But my gut is telling me that this file is wrapped in some kind of container and the option I suggested may not work if that’s the case.

@Christian Schmitz: 8BPS seems to be the normal PSD marker.

I think that Sam is correct. The file seems to be special. It came in a special mail:

[quote]Content-Type: text/plain
Content-ID: B9C738D7CA24964EA7623AAA79BDD982@tamhsc.edu
Content-Disposition: inline; filename=“SPH Mission Sign (DS).jpg”
Content-Transfer-Encoding: base64[/quote]

First my software tried to show this as text. Then after fixing this problem Graphics magic borked on the image. CGImageSource also doesn’t like the file. The image can be opened with GraphicsConverter but my software isn’t a graphics software. The user gets the file and I’ll close the ticket.