Thanks for the suggestion, Lei. Here's a stab at an implementation (with pypdfium2):
if scale:
px_w, px_h = self.get_px_size() # FPDFImageObj_GetImagePixelSize()
l, b, r, t = self.get_bounds() # FPDFPageObj_GetBounds()
content_w, content_h = r-l, t-b
# align pixel and content width/height relation if swapped due to rotation (e.g. 90°, 270°)
swap = (px_w < px_h) != (content_w < content_h)
if swap:
px_w, px_h = px_h, px_w
orig_mat = self.get_matrix() # FPDFPageObj_GetMatrix()
x_scale, y_scale = px_w/content_w, px_h/content_h
scaled_mat = orig_mat.scale(x_scale, y_scale)
logger.debug(
f"Pixel size: {px_w}, {px_h} (did swap? {swap})\n"
f"Size in page coords: {content_w}, {content_h}\n"
f"X/Y scale: {x_scale}, {y_scale}\n"
f"Current matrix: {orig_mat}\n"
f"Scaled matrix: {scaled_mat}"
)
self.set_matrix(scaled_mat) # FPDFPageObj_SetMatrix()
try:
raw_bitmap = pdfium_c.FPDFImageObj_GetRenderedBitmap(self.pdf, self.page, self)
finally:
if scale: self.set_matrix(orig_mat)