{"record":{"id":"3551402a40701b62","repo":"parallax/jsPDF","slug":"invalid-bmp-file","errorCode":null,"errorMessage":"Invalid BMP File","messagePattern":"Invalid BMP File","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/libs/BMPDecoder.js","lineNumber":20,"sourceCode":" * @author shaozilee\n *\n * Bmp format decoder,support 1bit 4bit 8bit 24bit bmp\n *\n */\n\nimport { console } from \"./console.js\";\n\nfunction BmpDecoder(buffer, is_with_alpha) {\n  this.pos = 0;\n  this.buffer = buffer;\n  this.datav = new DataView(buffer.buffer);\n  this.is_with_alpha = !!is_with_alpha;\n  this.bottom_up = true;\n  this.flag =\n    String.fromCharCode(this.buffer[0]) + String.fromCharCode(this.buffer[1]);\n  this.pos += 2;\n  if ([\"BM\", \"BA\", \"CI\", \"CP\", \"IC\", \"PT\"].indexOf(this.flag) === -1)\n    throw new Error(\"Invalid BMP File\");\n  this.parseHeader();\n  this.parseBGR();\n}\n\nBmpDecoder.prototype.parseHeader = function() {\n  this.fileSize = this.datav.getUint32(this.pos, true);\n  this.pos += 4;\n  this.reserved = this.datav.getUint32(this.pos, true);\n  this.pos += 4;\n  this.offset = this.datav.getUint32(this.pos, true);\n  this.pos += 4;\n  this.headerSize = this.datav.getUint32(this.pos, true);\n  this.pos += 4;\n  this.width = this.datav.getUint32(this.pos, true);\n  this.pos += 4;\n  this.height = this.datav.getInt32(this.pos, true);\n  this.pos += 4;\n  this.planes = this.datav.getUint16(this.pos, true);","sourceCodeStart":2,"sourceCodeEnd":38,"githubUrl":"https://github.com/parallax/jsPDF/blob/a3930ce03a585a26b2c76d12a0f413ce96f6d1a3/src/libs/BMPDecoder.js#L2-L38","documentation":"Thrown by the BMPDecoder constructor when the first two bytes of the buffer do not form a recognized BMP signature. Valid signatures are BM, BA, CI, CP, IC, PT (the standard OS/2 and Windows BMP magic markers). Any other leading bytes indicate the data is not a BMP, is truncated, or is a different format.","triggerScenarios":"Passing a PNG/JPEG/WebP buffer to BmpDecoder; passing a base64 string instead of a decoded byte array; buffer that is shorter than 2 bytes; byte order swapped or offset misalignment before decoding.","commonSituations":"Generic image-upload handler that routes all images through BmpDecoder; mislabelled file extension; forgetting to strip a data-URI prefix before slicing the byte buffer.","solutions":["Verify the file is actually a BMP (check magic bytes 0x42 0x4D for 'BM') before decoding.","Use the correct decoder for the actual format (PNGDecoder, JPEG path) or a format-sniffing dispatcher.","Ensure you pass a decoded byte array/Uint8Array, not a base64 string; strip any data-URI prefix."],"exampleFix":"// before\nvar bmp = new BmpDecoder(someBuffer);\n// after\nif (someBuffer[0] === 0x42 && someBuffer[1] === 0x4D) {\n  var bmp = new BmpDecoder(someBuffer);\n}","handlingStrategy":"type-guard","validationCode":"function isBmp(buf){ return buf && buf.length >= 2 && ['BM','BA','CI','CP','IC','PT'].indexOf(String.fromCharCode(buf[0],buf[1])) !== -1; }\nif (isBmp(buffer)) new BmpDecoder(buffer);","typeGuard":"function isBmpBuffer(buf){ return buf instanceof Uint8Array && buf.length >= 2 && buf[0]===0x42 && buf[1]===0x4D; }","tryCatchPattern":"try { var bmp = new BmpDecoder(buffer); } catch (e) { if (/Invalid BMP File/.test(e.message)) { /* route to other decoder */ } else throw e; }","preventionTips":["Sniff magic bytes before choosing a decoder","Decode base64/strip data-URI prefix before decoding"],"tags":["bmp","image","file-format","validation","magic-bytes"],"backgroundTag":null,"analyzedSha":"a3930ce03a585a26b2c76d12a0f413ce96f6d1a3","analyzedAt":"2026-08-13T05:33:39.648Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}