File indexing completed on 2024-05-12 15:59:40

0001 /*
0002  *  SPDX-FileCopyrightText: 2009 Boudewijn Rempt <boud@valdyas.org>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 #include "psd_resource_section.h"
0007 
0008 #include <QBuffer>
0009 #include <QIODevice>
0010 #include <kis_debug.h>
0011 
0012 #include "psd.h"
0013 #include "psd_resource_block.h"
0014 #include "psd_utils.h"
0015 
0016 PSDImageResourceSection::PSDImageResourceSection()
0017 {
0018 }
0019 
0020 PSDImageResourceSection::~PSDImageResourceSection()
0021 {
0022     resources.clear();
0023 }
0024 
0025 bool PSDImageResourceSection::read(QIODevice &io)
0026 {
0027     quint32 resourceSectionLength = 0;
0028     if (!psdread(io, resourceSectionLength)) {
0029         error = "Could not read image resource section length";
0030         return false;
0031     }
0032 
0033     dbgFile << "Image Resource Sectionlength:" << resourceSectionLength << ", starts at:" << io.pos();
0034 
0035     QByteArray ba = io.read(resourceSectionLength);
0036     if ((quint32)ba.size() != resourceSectionLength) {
0037         error = "Could not read all resources";
0038         return false;
0039     }
0040 
0041     QBuffer buf;
0042     buf.setBuffer(&ba);
0043     buf.open(QBuffer::ReadOnly);
0044 
0045     while (!buf.atEnd()) {
0046         PSDResourceBlock *block = new PSDResourceBlock();
0047         if (!block->read(buf)) {
0048             error = "Error reading block: " + block->error;
0049             dbgFile << error << ", skipping.";
0050             delete block;
0051             continue;
0052         }
0053         dbgFile << "resource block created. Type:" << block->identifier << "name" << block->name << "size" << block->dataSize << "," << buf.bytesAvailable()
0054                 << "bytes to go";
0055 
0056         resources[(PSDResourceID)block->identifier] = block;
0057     }
0058 
0059     dbgFile << "Read" << resources.size() << "Image Resource Blocks";
0060 
0061     return valid();
0062 }
0063 
0064 bool PSDImageResourceSection::write(QIODevice &io)
0065 {
0066     Q_UNUSED(io);
0067 
0068     if (!valid()) {
0069         error = "Resource Section is Invalid";
0070         return false;
0071     }
0072     // First write all the sections
0073     QByteArray ba;
0074     QBuffer buf;
0075     buf.setBuffer(&ba);
0076     buf.open(QBuffer::WriteOnly);
0077 
0078     Q_FOREACH (PSDResourceBlock *block, resources) {
0079         if (!block->write(buf)) {
0080             error = block->error;
0081             return false;
0082         }
0083     }
0084 
0085     buf.close();
0086 
0087     // Then get the size
0088     quint32 resourceBlockLength = ba.size();
0089     dbgFile << "resource section has size" << resourceBlockLength;
0090     psdwrite(io, resourceBlockLength);
0091 
0092     // and write the whole buffer;
0093     return (io.write(ba.constData(), ba.size()) == resourceBlockLength);
0094 }
0095 
0096 bool PSDImageResourceSection::valid()
0097 {
0098     return true;
0099 }
0100 
0101 QString PSDImageResourceSection::idToString(PSDImageResourceSection::PSDResourceID id)
0102 {
0103     switch (id) {
0104     case UNKNOWN:
0105         return "Unknown";
0106 
0107     case PS2_IMAGE_INFO:
0108         return "0x03e8 - Obsolete - ps 2.0 image info";
0109     case MAC_PRINT_INFO:
0110         return "0x03e9 - Optional - Mac print manager print info record";
0111     case PS2_COLOR_TAB:
0112         return "0x03eb - Obsolete - ps 2.0 indexed color table";
0113     case RESN_INFO:
0114         return "0x03ed - ResolutionInfo structure";
0115     case ALPHA_NAMES:
0116         return "0x03ee - Alpha channel names";
0117     case DISPLAY_INFO:
0118         return "0x03ef - DisplayInfo structure";
0119     case CAPTION:
0120         return "0x03f0 - Optional - Caption string";
0121     case BORDER_INFO:
0122         return "0x03f1 - Border info";
0123 
0124     case BACKGROUND_COL:
0125         return "0x03f2 - Background color";
0126     case PRINT_FLAGS:
0127         return "0x03f3 - Print flags";
0128     case GREY_HALFTONE:
0129         return "0x03f4 - Greyscale and multichannel halftoning info";
0130     case COLOR_HALFTONE:
0131         return "0x03f5 - Color halftoning info";
0132     case DUOTONE_HALFTONE:
0133         return "0x03f6 - Duotone halftoning info";
0134     case GREY_XFER:
0135         return "0x03f7 - Greyscale and multichannel transfer functions";
0136     case COLOR_XFER:
0137         return "0x03f8 - Color transfer functions";
0138     case DUOTONE_XFER:
0139         return "0x03f9 - Duotone transfer functions";
0140     case DUOTONE_INFO:
0141         return "0x03fa - Duotone image information";
0142     case EFFECTIVE_BW:
0143         return "0x03fb - Effective black & white values for dot range";
0144 
0145     case OBSOLETE_01:
0146         return "0x03fc - Obsolete";
0147     case EPS_OPT:
0148         return "0x03fd - EPS options";
0149     case QUICK_MASK:
0150         return "0x03fe - Quick mask info";
0151     case OBSOLETE_02:
0152         return "0x03ff - Obsolete";
0153     case LAYER_STATE:
0154         return "0x0400 - Layer state info";
0155     case WORKING_PATH:
0156         return "0x0401 - Working path (not saved)";
0157     case LAYER_GROUP:
0158         return "0x0402 - Layers group info";
0159     case OBSOLETE_03:
0160         return "0x0403 - Obsolete";
0161     case IPTC_NAA_DATA:
0162         return "0x0404 - IPTC-NAA record (IMV4.pdf)";
0163     case IMAGE_MODE_RAW:
0164         return "0x0405 - Image mode for raw format files";
0165 
0166     case JPEG_QUAL:
0167         return "0x0406 - JPEG quality";
0168     case GRID_GUIDE:
0169         return "0x0408 - Grid & guide info";
0170     case THUMB_RES:
0171         return "0x0409 - Thumbnail resource";
0172     case COPYRIGHT_FLG:
0173         return "0x040a - Copyright flag";
0174     case URL:
0175         return "0x040b - URL string";
0176     case THUMB_RES2:
0177         return "0x040c - Thumbnail resource";
0178     case GLOBAL_ANGLE:
0179         return "0x040d - Global angle";
0180     case COLOR_SAMPLER:
0181         return "0x040e - Color samplers resource";
0182     case ICC_PROFILE:
0183         return "0x040f - ICC Profile";
0184 
0185     case WATERMARK:
0186         return "0x0410 - Watermark";
0187     case ICC_UNTAGGED:
0188         return "0x0411 - Do not use ICC profile flag";
0189     case EFFECTS_VISIBLE:
0190         return "0x0412 - Show / hide all effects layers";
0191     case SPOT_HALFTONE:
0192         return "0x0413 - Spot halftone";
0193     case DOC_IDS:
0194         return "0x0414 - Document specific IDs";
0195     case ALPHA_NAMES_UNI:
0196         return "0x0415 - Unicode alpha names";
0197     case IDX_COL_TAB_CNT:
0198         return "0x0416 - Indexed color table count";
0199     case IDX_TRANSPARENT:
0200         return "0x0417 - Index of transparent color (if any)";
0201     case GLOBAL_ALT:
0202         return "0x0419 - Global altitude";
0203 
0204     case SLICES:
0205         return "0x041a - Slices";
0206     case WORKFLOW_URL_UNI:
0207         return "0x041b - Workflow URL - Unicode string";
0208     case JUMP_TO_XPEP:
0209         return "0x041c - Jump to XPEP (?)";
0210     case ALPHA_ID:
0211         return "0x041d - Alpha IDs";
0212     case URL_LIST_UNI:
0213         return "0x041e - URL list - unicode";
0214     case VERSION_INFO:
0215         return "0x0421 - Version info";
0216     case EXIF_DATA:
0217         return "0x0422 - (Photoshop 7.0) EXIF data 1. See http://www.kodak.com/global/plugins/acrobat/en/service/digCam/exifStandard2.pdf";
0218     case EXIF_DATA_3:
0219         return "0x0423 - (Photoshop 7.0) EXIF data 3. See http://www.kodak.com/global/plugins/acrobat/en/service/digCam/exifStandard2.pdf";
0220 
0221     case XMP_DATA:
0222         return "0x0424 - XMP data block";
0223     case CAPTION_DIGEST:
0224         return "0x0425 - (Photoshop 7.0) Caption digest. 16 bytes: RSA Data Security, MD5 message-digest algorithm";
0225     case PRINT_SCALE:
0226         return "0x0426 - (Photoshop 7.0) Print scale. 2 bytes style (0 = centered, 1 = size to fit, 2 = user defined). 4 bytes x location (floating point). 4 "
0227                "bytes y location (floating point). 4 bytes scale (floating point)";
0228     case PIXEL_ASPECT_RATION:
0229         return "0x0428 - (Photoshop CS) Pixel Aspect Ratio. 4 bytes (version = 1 or 2), 8 bytes double, x / y of a pixel. Version 2, attempting to correct "
0230                "values for NTSC and PAL, previously off by a factor of approx. 5%.";
0231     case LAYER_COMPS:
0232         return "0x0429 - (Photoshop CS) Layer Comps. 4 bytes (descriptor version = 16), Descriptor (see See Descriptor structure)";
0233     case ALTERNATE_DUOTONE:
0234         return "0x042A - (Photoshop CS) Alternate Duotone Colors. 2 bytes (version = 1), 2 bytes count, following is repeated for each count: [ Color: 2 bytes "
0235                "for space followed by 4 * 2 byte color component ], following this is another 2 byte count, usually 256, followed by Lab colors one byte each "
0236                "for L, a, b. This resource is not read or used by Photoshop.";
0237     case ALTERNATE_SPOT:
0238         return "0x042B - (Photoshop CS)Alternate Spot Colors. 2 bytes (version = 1), 2 bytes channel count, following is repeated for each count: 4 bytes "
0239                "channel ID, Color: 2 bytes for space followed by 4 * 2 byte color component. This resource is not read or used by Photoshop.";
0240     case LAYER_SELECTION_ID:
0241         return "0x042D - (Photoshop CS2) Layer Selection ID(s). 2 bytes count, following is repeated for each count: 4 bytes layer ID";
0242 
0243     case HDR_TONING:
0244         return "0x042E - (Photoshop CS2) HDR Toning information";
0245     case CS2_PRINT_INFO:
0246         return "0x042F - (Photoshop CS2) Print info";
0247     case LAYER_GROUP_ENABLED_ID:
0248         return "0x0430 - (Photoshop CS2) Layer Group(s) Enabled ID. 1 byte for each layer in the document, repeated by length of the resource. NOTE: Layer "
0249                "groups have start and end markers";
0250     case COLOR_SAMPLERS:
0251         return "0x0431 - (Photoshop CS3) Color samplers resource. Also see ID 1038 for old format. See See Color samplers resource format.";
0252     case MEASUREMENT_SCALE:
0253         return "0x0432 - (Photoshop CS3) Measurement Scale. 4 bytes (descriptor version = 16), Descriptor (see See Descriptor structure)";
0254     case TIMELINE_INFO:
0255         return "0x0433 - (Photoshop CS3) Timeline Information. 4 bytes (descriptor version = 16), Descriptor (see See Descriptor structure)";
0256     case SHEET_DISCLOSURE:
0257         return "0x0434 - (Photoshop CS3) Sheet Disclosure. 4 bytes (descriptor version = 16), Descriptor (see See Descriptor structure)";
0258     case CS3_DISPLAY_INFO:
0259         return "0x0435 - (Photoshop CS3) DisplayInfo structure to support floating point clors. Also see ID 1007. See Appendix A in Photoshop API Guide.pdf .";
0260     case ONION_SKINS:
0261         return "0x0436 - (Photoshop CS3) Onion Skins. 4 bytes (descriptor version = 16), Descriptor (see See Descriptor structure)";
0262 
0263     case COUNT_INFO:
0264         return "0x0438 - (Photoshop CS4) Count Information. 4 bytes (descriptor version = 16), Descriptor (see See Descriptor structure) Information about the "
0265                "count in the document. See the Count Tool.";
0266     case CS5_PRINT_INFO:
0267         return "0x043A - (Photoshop CS5) Print Information. 4 bytes (descriptor version = 16), Descriptor (see See Descriptor structure) Information about the "
0268                "current print settings in the document. The color management options.";
0269     case CS5_PRINT_STYLE:
0270         return "0x043B - (Photoshop CS5) Print Style. 4 bytes (descriptor version = 16), Descriptor (see See Descriptor structure) Information about the "
0271                "current print style in the document. The printing marks, labels, ornaments, etc.";
0272     case CS5_NSPrintInfo:
0273         return "0x043C - (Photoshop CS5) Macintosh NSPrintInfo. Variable OS specific info for Macintosh. NSPrintInfo. It is recommended that you do not "
0274                "interpret or use this data.";
0275     case CS5_WIN_DEVMODE:
0276         return "0x043D - (Photoshop CS5) Windows DEVMODE. Variable OS specific info for Windows. DEVMODE. It is recommended that you do not interpret or use "
0277                "this data.";
0278     case CS6_AUTOSAVE_FILE_PATH:
0279         return "0x043E - (Photoshop CS6) Auto Save File Path. Unicode string. It is recommended that you do not interpret or use this data.";
0280     case CS6_AUTOSAVE_FORMAT:
0281         return "0x043F - (Photoshop CS6) Auto Save Format. Unicode string. It is recommended that you do not interpret or use this data.";
0282     case CC_PATH_SELECTION_SATE:
0283         return "0x0440 - (Photoshop CC) Path Selection State. 4 bytes (descriptor version = 16), Descriptor (see See Descriptor structure) Information about "
0284                "the current path selection state.";
0285 
0286     case PATH_INFO_FIRST:
0287         return "0x07d0 - First path info block";
0288     case PATH_INFO_LAST:
0289         return "0x0bb6 - Last path info block";
0290     case CLIPPING_PATH:
0291         return "0x0bb7 - Name of clipping path";
0292 
0293     case CC_ORIGIN_PATH_INFO:
0294         return "0x0BB8 (Photoshop CC) Origin Path Info. 4 bytes (descriptor version = 16), Descriptor (see See Descriptor structure) Information about the "
0295                "origin path data.";
0296 
0297     case PLUGIN_RESOURCE_START:
0298         return "0x0FA0-0x1387 Plug-In resource(s). Resources added by a plug-in. See the plug-in API found in the SDK documentation ";
0299     case PLUGIN_RESOURCE_END:
0300         return "Last plug-in resource";
0301 
0302     case IMAGE_READY_VARS:
0303         return "0x1B58 Image Ready variables. XML representation of variables definition";
0304     case IMAGE_READY_DATA_SETS:
0305         return "0x1B59 Image Ready data sets";
0306 
0307     case LIGHTROOM_WORKFLOW:
0308         return "0x1F40 (Photoshop CS3) Lightroom workflow, if present the document is in the middle of a Lightroom workflow.";
0309 
0310     case PRINT_FLAGS_2:
0311         return "0x2710 - Print flags";
0312     default: {
0313         if (id > PATH_INFO_FIRST && id < PATH_INFO_LAST)
0314             return "Path Info Block";
0315         if (id > PLUGIN_RESOURCE_START && id < PLUGIN_RESOURCE_END)
0316             return "Plug-In Resource";
0317     }
0318     };
0319     return QString("Unknown Resource Block: %1").arg(id);
0320 }