{"record":{"id":"e321a11baa967c95","repo":"lovell/sharp","slug":"expected-width-height-and-channels-for-raw-pixel","errorCode":null,"errorMessage":"Expected width, height and channels for raw pixel input","messagePattern":"Expected width, height and channels for raw pixel input","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"lib/input.mjs","lineNumber":222,"sourceCode":"            break;\n          case Uint32Array:\n            inputDescriptor.rawDepth = 'uint';\n            break;\n          case Int32Array:\n            inputDescriptor.rawDepth = 'int';\n            break;\n          case Float32Array:\n            inputDescriptor.rawDepth = 'float';\n            break;\n          case Float64Array:\n            inputDescriptor.rawDepth = 'double';\n            break;\n          default:\n            inputDescriptor.rawDepth = 'uchar';\n            break;\n        }\n      } else {\n        throw new Error('Expected width, height and channels for raw pixel input');\n      }\n      inputDescriptor.rawPremultiplied = false;\n      if (is.defined(inputOptions.raw.premultiplied)) {\n        if (is.bool(inputOptions.raw.premultiplied)) {\n          inputDescriptor.rawPremultiplied = inputOptions.raw.premultiplied;\n        } else {\n          throw is.invalidParameterError('raw.premultiplied', 'boolean', inputOptions.raw.premultiplied);\n        }\n      }\n      inputDescriptor.rawPageHeight = 0;\n      if (is.defined(inputOptions.raw.pageHeight)) {\n        if (is.integer(inputOptions.raw.pageHeight) && inputOptions.raw.pageHeight > 0 && inputOptions.raw.pageHeight <= inputOptions.raw.height) {\n          if (inputOptions.raw.height % inputOptions.raw.pageHeight !== 0) {\n            throw new Error(`Expected raw.height ${inputOptions.raw.height} to be a multiple of raw.pageHeight ${inputOptions.raw.pageHeight}`);\n          }\n          inputDescriptor.rawPageHeight = inputOptions.raw.pageHeight;\n        } else {\n          throw is.invalidParameterError('raw.pageHeight', 'positive integer', inputOptions.raw.pageHeight);","sourceCodeStart":204,"sourceCodeEnd":240,"githubUrl":"https://github.com/lovell/sharp/blob/56676c69180a32b468123e090f87bfee30539b49/lib/input.mjs#L204-L240","documentation":"Thrown when raw pixel input is requested (inputOptions.raw is defined) but the raw descriptor is missing width, height, or channels. Raw input means sharp must interpret a flat byte buffer as an image, so it needs all three dimensions to decode the bytes correctly; without them the buffer length is ambiguous. The error fires in the else branch after the per-field validations.","triggerScenarios":"Calling sharp(buffer, { raw: { width: 100, height: 100 } }) with channels omitted, or any combination missing one of the three. Passing raw: {} with no fields. Providing width and channels but forgetting height.","commonSituations":"Adapting canvas/webgl pixel data (which provides Uint8ClampedArray) where the developer forgets channels: 4 for RGBA. Feeding a raw buffer from a sensor or decoder without propagating dimensions. Copying a raw example and deleting a field by accident.","solutions":["Always provide all three: raw: { width, height, channels } where channels is 1-4.","Verify channels matches the buffer layout (RGBA = 4, RGB = 3, grayscale = 1).","Assert buffer.length === width * height * channels before calling sharp to catch mismatches early."],"exampleFix":"// before\nsharp(rgbaBuffer, { raw: { width: 100, height: 100 } })\n\n// after\nsharp(rgbaBuffer, { raw: { width: 100, height: 100, channels: 4 } })","handlingStrategy":"validation","validationCode":"function rawInput(buffer, { width, height, channels }) {\n  if (!Number.isInteger(width) || !Number.isInteger(height) || ![1,2,3,4].includes(channels)) {\n    throw new Error('raw requires integer width, height, and channels (1-4)');\n  }\n  if (buffer.length !== width * height * channels) {\n    throw new Error(`raw buffer length ${buffer.length} != width*height*channels (${width*height*channels})`);\n  }\n  return sharp(buffer, { raw: { width, height, channels } });\n}","typeGuard":"function isCompleteRawSpec(raw) {\n  return raw && Number.isInteger(raw.width) && Number.isInteger(raw.height) && Number.isInteger(raw.channels);\n}","tryCatchPattern":null,"preventionTips":["Always specify width, height, and channels together for raw input.","Match channels to the buffer (4 = RGBA).","Assert buffer.length === width*height*channels to catch layout errors early."],"tags":["raw","input","validation","dimensions"],"backgroundTag":null,"analyzedSha":"56676c69180a32b468123e090f87bfee30539b49","analyzedAt":"2026-08-13T04:44:31.201Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}