Adapted from upstream PR #1320 (not yet merged). Replaces scattered free()/delete cleanup with RAII Cleanup struct that guarantees resource release on all return paths. Changes rowCount from uint16_t* to uint32_t* to prevent overflow on large images. If/when #1320 is merged upstream, this commit should be dropped during the next sync and the upstream version used instead. Made-with: Cursor