{"record":{"id":"7ad375dcea1a6995","repo":"HMCL-dev/HMCL","slug":"illegal-combination-of-bit-depth-d-and-colour-t","errorCode":null,"errorMessage":"Illegal combination of bit depth (%d) and colour type (%d). See http://www.w3.org/TR/2003/REC-PNG-20031110/#table111 .","messagePattern":"Illegal combination of bit depth \\((.+?)\\) and colour type \\((.+?)\\)\\. See http://www\\.w3\\.org/TR/2003/REC-PNG-20031110/#table111 \\.","errorType":"exception","errorClass":"PngIntegrityException","httpStatus":null,"severity":"error","filePath":"HMCL/src/main/java/org/jackhuang/hmcl/ui/image/apng/chunks/PngHeader.java","lineNumber":203,"sourceCode":"            case 1:\n            case 2:\n            case 4:\n            case 8:\n            case 16:\n                break; // all fine\n            default:\n                throw new PngIntegrityException(\"Invalid bit depth \" + bitDepth);\n        }\n\n        // thanks to pypng\n        if (colourType.isIndexed() && bitDepth > 8) {\n            throw new PngIntegrityException(String.format(\n                    \"Indexed images (colour type %d) cannot have bitdepth > 8 (bit depth %d).\" +\n                            \" See http://www.w3.org/TR/2003/REC-PNG-20031110/#table111 .\", colourType.code, bitDepth));\n        }\n\n        if (bitDepth < 8 && !colourType.supportsSubByteDepth()) {\n            throw new PngIntegrityException(String.format(\n                    \"Illegal combination of bit depth (%d) and colour type (%d).\" +\n                            \" See http://www.w3.org/TR/2003/REC-PNG-20031110/#table111 .\", colourType.code, bitDepth));\n        }\n\n        if (interlaceMethod != 0) {\n            throw new PngFeatureException(\"Interlaced images are not yet supported\");\n        }\n    }\n\n    public static PngHeader from(DataInput dis) throws IOException, PngException {\n        int width = dis.readInt();\n        int height = dis.readInt();\n        byte bitDepth = dis.readByte();\n        PngColourType colourType = PngColourType.fromByte(dis.readByte());\n        byte compressionMethod = dis.readByte();\n        byte filterMethod = dis.readByte();\n        byte interlaceMethod = dis.readByte();\n        checkHeaderParameters(width, height, bitDepth, colourType, compressionMethod, filterMethod, interlaceMethod);","sourceCodeStart":185,"sourceCodeEnd":221,"githubUrl":"https://github.com/HMCL-dev/HMCL/blob/24702dc5a0214034f4c27166d5fd30cad08cec19/HMCL/src/main/java/org/jackhuang/hmcl/ui/image/apng/chunks/PngHeader.java#L185-L221","documentation":"Thrown when the IHDR declares a bit depth/colour type combination not allowed by the PNG spec (table 11.1), e.g. bit depth below 8 for a colour type that does not support sub-byte depths (like truecolour RGB or RGBA).","triggerScenarios":"An IHDR with, say, colour type 2 (truecolour) or 6 (RGBA) and bit depth 1/2/4, or colour type 0 (greyscale) with an illegal depth like 3.","commonSituations":"Corrupted or truncated images, malformed files from buggy encoders, fuzzed inputs.","solutions":["Re-encode the image with a compliant PNG encoder to get a legal depth/colourType pair.","Consult the PNG spec table 11.1 and ensure the producer uses valid combinations (e.g. greyscale 1/2/4/8/16, RGB 8/16, palette 1/2/4/8, grey+alpha 8/16, RGBA 8/16).","Catch PngIntegrityException and treat the input as invalid."],"exampleFix":"// before\nprocessPng(inputStream);\n// after: validate combination first\nPngHeader h = peekHeader(inputStream);\nif (!isValidDepthForColourType(h.bitDepth, h.colourType)) {\n    throw new PngIntegrityException(\"illegal depth/colourType combination\");\n}\nprocessPng(inputStream);","handlingStrategy":"validation","validationCode":"// allowed combos per PNG spec table 11.1\nboolean ok = (ct==0 && Set.of(1,2,4,8,16).contains(bd))\n          || (ct==2 && (bd==8||bd==16))\n          || (ct==3 && Set.of(1,2,4,8).contains(bd))\n          || (ct==4 && (bd==8||bd==16))\n          || (ct==6 && (bd==8||bd==16));\nif (!ok) throw new IllegalArgumentException(\"illegal depth/colourType\");","typeGuard":null,"tryCatchPattern":"try { decode(bytes); } catch (PngIntegrityException e) { rejectImage(\"illegal header combination\"); }","preventionTips":["Enforce spec table 11.1 combos in your own PNG writers.","Pre-validate headers before running the decoder.","Use pngcheck in CI for image fixtures.","Re-encode problem images."],"tags":["png","image-decoding","validation","header"],"backgroundTag":"invalid-enum-value","analyzedSha":"24702dc5a0214034f4c27166d5fd30cad08cec19","analyzedAt":"2026-09-10T12:36:46.680Z","contentChangedAt":"2026-09-10T12:36:46.680Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}