File indexing completed on 2024-12-22 04:15:57

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