{"record":{"id":"e1f5378e52257b9f","repo":"SixLabors/ImageSharp","slug":"component-type-not-supported-for-grayscale-pbm","errorCode":null,"errorMessage":"Component type not supported for Grayscale PBM.","messagePattern":"Component type not supported for Grayscale PBM\\.","errorType":"exception","errorClass":"ImageFormatException","httpStatus":null,"severity":"error","filePath":"src/ImageSharp/Formats/Pbm/BinaryEncoder.cs","lineNumber":51,"sourceCode":"        ImageFrame<TPixel> image,\n        PbmColorType colorType,\n        PbmComponentType componentType,\n        CancellationToken cancellationToken)\n        where TPixel : unmanaged, IPixel<TPixel>\n    {\n        if (colorType == PbmColorType.Grayscale)\n        {\n            if (componentType == PbmComponentType.Byte)\n            {\n                WriteGrayscale(configuration, stream, image, cancellationToken);\n            }\n            else if (componentType == PbmComponentType.Short)\n            {\n                WriteWideGrayscale(configuration, stream, image, cancellationToken);\n            }\n            else\n            {\n                throw new ImageFormatException(\"Component type not supported for Grayscale PBM.\");\n            }\n        }\n        else if (colorType == PbmColorType.Rgb)\n        {\n            if (componentType == PbmComponentType.Byte)\n            {\n                WriteRgb(configuration, stream, image, cancellationToken);\n            }\n            else if (componentType == PbmComponentType.Short)\n            {\n                WriteWideRgb(configuration, stream, image, cancellationToken);\n            }\n            else\n            {\n                throw new ImageFormatException(\"Component type not supported for Color PBM.\");\n            }\n        }\n        else if (componentType == PbmComponentType.Bit)","sourceCodeStart":33,"sourceCodeEnd":69,"githubUrl":"https://github.com/SixLabors/ImageSharp/blob/59ce6af6fc29027cda277ef62d4d1694a8acce91/src/ImageSharp/Formats/Pbm/BinaryEncoder.cs#L33-L69","documentation":"BinaryEncoder.WritePixels throws ImageFormatException when saving a grayscale PBM whose pixel buffer's component type is neither Byte nor Short. The PBM binary encoder only has fast paths for 8-bit and 16-bit grayscale samples, so any other component type reaching the grayscale branch is rejected as an encoder bug or unsupported pixel type.","triggerScenarios":"Saving an Image<TPixel> as PBM with PbmColorType.Grayscale while the underlying pixel type maps to a component type other than Byte/Short (e.g. a bit/bitmap-typed buffer misrouted to the grayscale path, or a float-based pixel type routed incorrectly by metadata).","commonSituations":"Custom encoder plumbing or forks that set PBM metadata manually; pipelines mixing bitonal and grayscale images and mislabeling the color type; encoder regressions after upgrading ImageSharp versions.","solutions":["Ensure the image's pixel type matches the intended PBM flavor: use L8/L16-style grayscale pixels for grayscale PBM, or set PbmEncoder metadata color type correctly.","Convert the image to an 8-bit or 16-bit grayscale image before saving (Clone + pixel-type conversion).","Catch ImageFormatException around SaveAsPbm and fall back to a conversion step or a different format."],"exampleFix":"// before\nusing Image<Rgba32> image = Image.Load<Rgba32>(path);\nimage.SaveAsPbm(stream); // component type not encodable for grayscale PBM\n\n// after\nusing Image<L8> gray = Image.Load<L8>(path);\ngray.SaveAsPbm(stream); // L8 maps to the Byte grayscale path","handlingStrategy":"validation","validationCode":"// Only L8/L16-style grayscale pixels map to PBM Byte/Short components.\nbool isGrayscalePbmSafe = typeof(TPixel) == typeof(L8) || typeof(TPixel) == typeof(L16);\nif (!isGrayscalePbmSafe)\n{\n    // convert to L8 before calling SaveAsPbm\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use L8/L16 pixel types when saving grayscale PBM.","Do not hand-edit PBM encoder metadata; let the pixel type drive the component type.","Test encode paths against every pixel type your pipeline can produce."],"tags":["pbm","encoding","component-type","grayscale"],"backgroundTag":"unsupported-enum-value","analyzedSha":"59ce6af6fc29027cda277ef62d4d1694a8acce91","analyzedAt":"2026-09-13T18:34:59.331Z","contentChangedAt":"2026-09-13T18:34:59.331Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}