Task 575: .PSD File Format
Task 575: .PSD File Format
- List of all the properties of the .PSD file format intrinsic to its file system:
Based on the Adobe Photoshop File Formats Specification, the core properties (fields) in the file header, which are intrinsic to the format's structure, are:
- Signature: 4 bytes, always '8BPS' (string).
- Version: 2 bytes, always 1 for PSD (2 for PSB) (integer).
- Reserved: 6 bytes, must be zero (bytes).
- Number of Channels: 2 bytes, number of color channels in the image, including alpha (1 to 56) (integer).
- Height: 4 bytes, height of the image in pixels (1 to 30,000 for PSD) (integer).
- Width: 4 bytes, width of the image in pixels (1 to 30,000 for PSD) (integer).
- Depth: 2 bytes, bits per channel (1, 8, 16, or 32) (integer).
- Color Mode: 2 bytes, color mode of the image (0=Bitmap, 1=Grayscale, 2=Indexed, 3=RGB, 4=CMYK, 7=Multichannel, 8=Duotone, 9=Lab) (integer).
These are the fundamental header properties. The file format includes additional sections (Color Mode Data, Image Resources, Layer Information, Image Data), but the above are the base properties common to all PSD files.
- Two direct download links for .PSD files:
- http://chomikuj.pl/jilguera/grafika/mock+ups/Minimal_Template.psd (Minimal_Template.psd, approximately 10.79 MB)
- https://filesamples.com/samples/image/psd/sample1.psd (Sample PSD file, small size for testing)
- HTML/JavaScript for drag-and-drop .PSD file to dump properties to screen:
This HTML page allows dragging and dropping a .PSD file, parses the header using JavaScript, and displays the properties in JSON format on the screen.
- Python class for .PSD file:
import struct
import os
class PSDFile:
def __init__(self, filepath):
self.filepath = filepath
self.properties = {}
def read_properties(self):
with open(self.filepath, 'rb') as f:
data = f.read(26) # Header is 26 bytes
if len(data) < 26:
raise ValueError("Invalid PSD file")
self.properties = {
'signature': struct.unpack('>4s', data[0:4])[0].decode('utf-8'),
'version': struct.unpack('>H', data[4:6])[0],
'reserved': data[6:12],
'channels': struct.unpack('>H', data[12:14])[0],
'height': struct.unpack('>I', data[14:18])[0],
'width': struct.unpack('>I', data[18:22])[0],
'depth': struct.unpack('>H', data[22:24])[0],
'color_mode': struct.unpack('>H', data[24:26])[0]
}
def print_properties(self):
for key, value in self.properties.items():
print(f"{key}: {value}")
def write_properties(self, new_filepath):
# For simplicity, copy the original file and overwrite header if needed
# Here, we just copy as 'write' without change
with open(self.filepath, 'rb') as f_in:
data = f_in.read()
with open(new_filepath, 'wb') as f_out:
f_out.write(data)
print(f"Written to {new_filepath}")
# Example usage:
# psd = PSDFile('example.psd')
# psd.read_properties()
# psd.print_properties()
# psd.write_properties('output.psd')
This class opens a .PSD file, decodes and reads the header properties, prints them to console, and writes the file to a new path (simple copy for demonstration; full write would require reconstructing the file).
- Java class for .PSD file:
import java.io.*;
import java.nio.*;
import java.nio.channels.FileChannel;
public class PSDFile {
private String filepath;
private String signature;
private short version;
private byte[] reserved = new byte[6];
private short channels;
private int height;
private int width;
private short depth;
private short colorMode;
public PSDFile(String filepath) {
this.filepath = filepath;
}
public void readProperties() throws IOException {
try (RandomAccessFile raf = new RandomAccessFile(filepath, "r")) {
ByteBuffer buffer = ByteBuffer.allocate(26).order(ByteOrder.BIG_ENDIAN);
FileChannel channel = raf.getChannel();
channel.read(buffer);
buffer.flip();
byte[] sigBytes = new byte[4];
buffer.get(sigBytes);
signature = new String(sigBytes);
version = buffer.getShort();
buffer.get(reserved);
channels = buffer.getShort();
height = buffer.getInt();
width = buffer.getInt();
depth = buffer.getShort();
colorMode = buffer.getShort();
}
}
public void printProperties() {
System.out.println("signature: " + signature);
System.out.println("version: " + version);
System.out.print("reserved: ");
for (byte b : reserved) System.out.print(b + " ");
System.out.println();
System.out.println("channels: " + channels);
System.out.println("height: " + height);
System.out.println("width: " + width);
System.out.println("depth: " + depth);
System.out.println("colorMode: " + colorMode);
}
public void writeProperties(String newFilepath) throws IOException {
// Simple copy for demonstration
try (FileInputStream in = new FileInputStream(filepath);
FileOutputStream out = new FileOutputStream(newFilepath)) {
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
}
System.out.println("Written to " + newFilepath);
}
// Example usage:
// public static void main(String[] args) throws IOException {
// PSDFile psd = new PSDFile("example.psd");
// psd.readProperties();
// psd.printProperties();
// psd.writeProperties("output.psd");
// }
}
This Java class opens a .PSD file, decodes and reads the header properties, prints them to console, and writes the file to a new path (simple copy).
- JavaScript class for .PSD file (Node.js compatible, using fs):
const fs = require('fs');
class PSDFile {
constructor(filepath) {
this.filepath = filepath;
this.properties = {};
}
readProperties() {
const data = fs.readFileSync(this.filepath);
if (data.length < 26) throw new Error('Invalid PSD file');
const view = new DataView(data.buffer);
this.properties = {
signature: String.fromCharCode(view.getUint8(0), view.getUint8(1), view.getUint8(2), view.getUint8(3)),
version: view.getUint16(4),
reserved: data.slice(6, 12),
channels: view.getUint16(12),
height: view.getUint32(14),
width: view.getUint32(18),
depth: view.getUint16(22),
colorMode: view.getUint16(24)
};
}
printProperties() {
console.log(this.properties);
}
writeProperties(newFilepath) {
// Simple copy
fs.copyFileSync(this.filepath, newFilepath);
console.log(`Written to ${newFilepath}`);
}
}
// Example usage:
// const psd = new PSDFile('example.psd');
// psd.readProperties();
// psd.printProperties();
// psd.writeProperties('output.psd');
This JavaScript class (for Node.js) opens a .PSD file, decodes and reads the header properties, prints them to console, and writes the file to a new path (simple copy).
- C class for .PSD file (using C++):
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdint>
class PSDFile {
private:
std::string filepath;
char signature[5];
uint16_t version;
uint8_t reserved[6];
uint16_t channels;
uint32_t height;
uint32_t width;
uint16_t depth;
uint16_t colorMode;
public:
PSDFile(const std::string& fp) : filepath(fp) {
signature[4] = '\0';
}
void readProperties() {
std::ifstream file(filepath, std::ios::binary);
if (!file) {
throw std::runtime_error("Cannot open file");
}
char buffer[26];
file.read(buffer, 26);
if (file.gcount() < 26) {
throw std::runtime_error("Invalid PSD file");
}
memcpy(signature, buffer, 4);
memcpy(&version, buffer + 4, 2);
version = __builtin_bswap16(version); // Big-endian to host
memcpy(reserved, buffer + 6, 6);
memcpy(&channels, buffer + 12, 2);
channels = __builtin_bswap16(channels);
memcpy(&height, buffer + 14, 4);
height = __builtin_bswap32(height);
memcpy(&width, buffer + 18, 4);
width = __builtin_bswap32(width);
memcpy(&depth, buffer + 22, 2);
depth = __builtin_bswap16(depth);
memcpy(&colorMode, buffer + 24, 2);
colorMode = __builtin_bswap16(colorMode);
}
void printProperties() {
std::cout << "signature: " << signature << std::endl;
std::cout << "version: " << version << std::endl;
std::cout << "reserved: ";
for (int i = 0; i < 6; ++i) std::cout << (int)reserved[i] << " ";
std::cout << std::endl;
std::cout << "channels: " << channels << std::endl;
std::cout << "height: " << height << std::endl;
std::cout << "width: " << width << std::endl;
std::cout << "depth: " << depth << std::endl;
std::cout << "colorMode: " << colorMode << std::endl;
}
void writeProperties(const std::string& newFilepath) {
// Simple copy
std::ifstream src(filepath, std::ios::binary);
std::ofstream dst(newFilepath, std::ios::binary);
dst << src.rdbuf();
std::cout << "Written to " << newFilepath << std::endl;
}
};
// Example usage:
// int main() {
// try {
// PSDFile psd("example.psd");
// psd.readProperties();
// psd.printProperties();
// psd.writeProperties("output.psd");
// } catch (const std::exception& e) {
// std::cerr << e.what() << std::endl;
// }
// return 0;
// }
This C++ class opens a .PSD file, decodes and reads the header properties (handling big-endian), prints them to console, and writes the file to a new path (simple copy).