Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A few quick blitbuffer fixes #1440

Merged
merged 2 commits into from
Dec 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion ffi/blitbuffer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2148,11 +2148,17 @@ function BB.compat(oldbuffer)
return ffi.cast("BlitBuffer4*", oldbuffer)[0]
end

function BB.fromstring(width, height, buffertype, str, stride)
function BB.fromstring(width, height, buffertype, str, stride, rotation, inverse)
local dataptr = C.malloc(#str)
ffi.copy(dataptr, str, #str)
local bb = BB.new(width, height, buffertype, dataptr, stride)
bb:setAllocated(1)
if rotation ~= nil then
bb:setRotation(rotation)
end
if inverse ~= nil then
bb:setInverse(inverse)
end
return bb
end

Expand Down
22 changes: 14 additions & 8 deletions ffi/mupdf.lua
Original file line number Diff line number Diff line change
Expand Up @@ -826,13 +826,19 @@ end
--[[--
Scales a blitbuffer.

Quality of scaling done by MuPDF is better than the one done in blitbuffer.lua
MµPDF's scaling is of much better quality than the very naive implementation in blitbuffer.lua.
(see fz_scale_pixmap_cached() in mupdf/source/fitz/draw-scale-simple.c).
Same arguments as BlitBuffer:scale() for easy replacement.

Unlike BlitBuffer:scale(), this *ignores* the blitbuffer's rotation
(i.e., where possible, we simply wrap the BlitBuffer's data in a fitz pixmap,
with no data copy, so the buffer's *native* memory layout is followed).
If you actually want to preserve the rotation, you'll have to fudge
with the width & height arguments and tweak the returned buffer's rotation flag,
or go through a temporary copy to ensure that the buffer's memory is laid out accordingly.
--]]
function mupdf.scaleBlitBuffer(bb, width, height)
-- We need first to convert our BlitBuffer to a pixmap
local orig_w, orig_h = bb:getWidth(), bb:getHeight()
local bbtype = bb:getType()
local colorspace
local converted_bb
Expand Down Expand Up @@ -862,15 +868,15 @@ function mupdf.scaleBlitBuffer(bb, width, height)
end
alpha = 1
elseif bbtype == BlitBuffer.TYPE_BB4 then
converted_bb = BlitBuffer.new(orig_w, orig_h, BlitBuffer.TYPE_BB8)
converted_bb:blitFrom(bb, 0, 0, 0, 0, orig_w, orig_h)
converted_bb = BlitBuffer.new(bb.w, bb.h, BlitBuffer.TYPE_BB8)
converted_bb:blitFrom(bb, 0, 0, 0, 0, bb.w, bb.h)
bb = converted_bb -- we don't free() the provided bb, but we'll have to free our converted_bb
colorspace = M.fz_device_gray(context())
alpha = 0
stride = orig_w
stride = bb.w
else
converted_bb = BlitBuffer.new(orig_w, orig_h, BlitBuffer.TYPE_BBRGB32)
converted_bb:blitFrom(bb, 0, 0, 0, 0, orig_w, orig_h)
converted_bb = BlitBuffer.new(bb.w, bb.h, BlitBuffer.TYPE_BBRGB32)
converted_bb:blitFrom(bb, 0, 0, 0, 0, bb.w, bb.h)
bb = converted_bb -- we don't free() the provided bb, but we'll have to free our converted_bb
if mupdf.bgr then
colorspace = M.fz_device_bgr(context())
Expand All @@ -881,7 +887,7 @@ function mupdf.scaleBlitBuffer(bb, width, height)
end
-- We can now create a pixmap from this bb of correct type
local pixmap = W.mupdf_new_pixmap_with_data(context(), colorspace,
orig_w, orig_h, nil, alpha, stride, ffi.cast("unsigned char*", bb.data))
bb.w, bb.h, nil, alpha, stride, ffi.cast("unsigned char*", bb.data))
if pixmap == nil then
if converted_bb then converted_bb:free() end -- free our home made bb
merror("could not create pixmap from blitbuffer")
Expand Down