{"record":{"id":"4cb12607410de766","repo":"wmjordan/PDFPatcher","slug":"the-image-you-are-attempting-to-quantize-does-not","errorCode":null,"errorMessage":"The image you are attempting to quantize does not contain a 32 bit ARGB palette. This image has a bit depth of {0} with {1} colors.","messagePattern":"The image you are attempting to quantize does not contain a 32 bit ARGB palette\\. This image has a bit depth of (.+?) with (.+?) colors\\.","errorType":"validation","errorClass":"QuantizationException","httpStatus":null,"severity":"error","filePath":"App/Processor/Imaging/WuColorQuantizer.cs","lineNumber":73,"sourceCode":"\t\t\tfinally {\r\n\t\t\t\tif (targetData != null)\r\n\t\t\t\t\tresult.UnlockBits(targetData);\r\n\t\t\t}\r\n\t\t\tresult.SetResolution(source.HorizontalResolution, source.VerticalResolution);\r\n\t\t\treturn result;\r\n\t\t}\r\n\r\n\t\tstatic ColorData BuildHistogram(Bitmap sourceImage) {\r\n\t\t\tint bitmapWidth = sourceImage.Width;\r\n\t\t\tint bitmapHeight = sourceImage.Height;\r\n\r\n\t\t\tvar data = sourceImage.LockBits(false);\r\n\t\t\tvar colorData = new ColorData(__MaxSideIndex, bitmapWidth, bitmapHeight);\r\n\r\n\t\t\ttry {\r\n\t\t\t\tvar bitDepth = Image.GetPixelFormatSize(sourceImage.PixelFormat);\r\n\t\t\t\tif (bitDepth != 32 && bitDepth != 24)\r\n\t\t\t\t\tthrow new QuantizationException(string.Format(\"The image you are attempting to quantize does not contain a 32 bit ARGB palette. This image has a bit depth of {0} with {1} colors.\", bitDepth, sourceImage.Palette.Entries.Length));\r\n\t\t\t\tvar byteLength = data.Stride < 0 ? -data.Stride : data.Stride;\r\n\t\t\t\tvar offset = 0;\r\n\t\t\t\tvar buffer = new Byte[byteLength * sourceImage.Height];\r\n\r\n\t\t\t\tSystem.Runtime.InteropServices.Marshal.Copy(data.Scan0, buffer, 0, buffer.Length);\r\n\t\t\t\tvar t = new int[__MaxColor];\r\n\t\t\t\tfor (var i = 0; i < __MaxColor; ++i) {\r\n\t\t\t\t\tt[i] = i * i;\r\n\t\t\t\t}\r\n\t\t\t\tint pos;\r\n\t\t\t\tbyte vr, vg, vb, r, g, b;\r\n\t\t\t\tvar w = colorData.Weights;\r\n\t\t\t\tvar mr = colorData.MomentsRed;\r\n\t\t\t\tvar mg = colorData.MomentsGreen;\r\n\t\t\t\tvar mb = colorData.MomentsBlue;\r\n\t\t\t\tvar m = colorData.Moments;\r\n\t\t\t\tfor (int y = 0; y < bitmapHeight; y++) {\r\n\t\t\t\t\tvar index = 0;\r","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/wmjordan/PDFPatcher/blob/4782bbd9ada850b56258f29999b7a1c9076c9b1b/App/Processor/Imaging/WuColorQuantizer.cs#L55-L91","documentation":"WuColorQuantizer.BuildHistogram throws QuantizationException when the source bitmap's pixel bit depth is not 32 or 24 (checked via Image.GetPixelFormatSize). The Wu quantizer only operates on 24/32-bit RGB/ARGB data; other depths (1/4/8-bit indexed, 16-bit, etc.) are rejected because the histogram buffer access assumes 3-4 bytes per pixel.","triggerScenarios":"Calling the quantizer (BuildHistogram / Quantize) on a Bitmap whose PixelFormat is not 32bppRgb/32bppArgb or 24bppRgb — e.g. an 8-bit indexed image, a 1-bit黑白 image, or a 16-bit image passed directly without prior promotion.","commonSituations":"Loading an indexed-color PNG/GIF or a 1-bit scanned image and passing it to the quantizer without converting its pixel format first; extracting images from a PDF in indexed formats; a bitmap created with a default format that isn't 24/32-bit.","solutions":["Convert the source Bitmap to 32bppArgb (or 24bppRgb) via Clone(..., PixelFormat.Format32bppArgb) before quantizing.","Guard the quantizer entry point with a pixel-format check and promote non-conforming bitmaps.","For indexed sources, expand to full color before invoking WuColorQuantizer."],"exampleFix":"// before\nvar result = WuColorQuantizer.Quantize(sourceImage);\n\n// after\nif (Image.GetPixelFormatSize(sourceImage.PixelFormat) is not (32 or 24)) {\n  sourceImage = sourceImage.Clone(\n    new Rectangle(0, 0, sourceImage.Width, sourceImage.Height),\n    PixelFormat.Format32bppArgb);\n}\nvar result = WuColorQuantizer.Quantize(sourceImage);","handlingStrategy":"validation","validationCode":"int bd = Image.GetPixelFormatSize(src.PixelFormat);\nif (bd != 32 && bd != 24)\n    src = src.Clone(new Rectangle(0,0,src.Width,src.Height), PixelFormat.Format32bppArgb);","typeGuard":"static bool IsQuantizable(Bitmap b) {\n  int d = Image.GetPixelFormatSize(b.PixelFormat);\n  return d == 32 || d == 24;\n}","tryCatchPattern":null,"preventionTips":["Always promote source bitmaps to 32bppArgb before quantizing.","Guard the quantizer entry with a pixel-format check.","Expand indexed/1-bit images to full color first."],"tags":["image","quantization","pixel-format","validation"],"backgroundTag":null,"analyzedSha":"4782bbd9ada850b56258f29999b7a1c9076c9b1b","analyzedAt":"2026-08-13T17:44:50.812Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}