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

PDF-viewer: Calculate overlaps when excluding images #365

Merged
merged 3 commits into from
Aug 4, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 54 additions & 4 deletions app/pdf-viewer/buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,17 +340,67 @@ def get_page_pixmap(self, index, scale):
pixmap.invertIRect(pixmap.irect)

# exclude images
imagelist = page.getImageList()
imagelist = page.getImageList(full=True)
imagebboxlist = []
for image in imagelist:
try:
# image[7] is the name of the picture
imagerect = page.getImageBbox(image[7])
imagerect = page.getImageBbox(image)
if imagerect.isInfinite or imagerect.isEmpty:
continue
pixmap.invertIRect(imagerect * self.scale)
else:
imagebboxlist.append(imagerect)
except Exception:
pass

newly_added_overlapbboxlist = imagebboxlist

# Nth time of loop represents N+1 rectanges' intesects' overlaps
time = 0
while len(newly_added_overlapbboxlist) > 1:
temp_overlapbboxlist = []
time += 1
# calculate overlap
for i in range(len(newly_added_overlapbboxlist)):
for j in range(i+1,len(newly_added_overlapbboxlist)):
x0a = newly_added_overlapbboxlist[i].x0
y0a = newly_added_overlapbboxlist[i].y0
x1a = newly_added_overlapbboxlist[i].x1
y1a = newly_added_overlapbboxlist[i].y1
x0b = newly_added_overlapbboxlist[j].x0
y0b = newly_added_overlapbboxlist[j].y0
x1b = newly_added_overlapbboxlist[j].x1
y1b = newly_added_overlapbboxlist[j].y1
x0c = max(x0a,x0b)
y0c = max(y0a,y0b)
x1c = min(x1a,x1b)
y1c = min(y1a,y1b)
if x0c < x1c and y0c < y1c:
temp_overlapbboxlist.append(fitz.Rect(x0c,y0c,x1c,y1c))
# remove duplicate overlaps for one time
for item in set(temp_overlapbboxlist):
if temp_overlapbboxlist.count(item) % 2 == 0:
while item in temp_overlapbboxlist:
temp_overlapbboxlist.remove(item)
else:
while temp_overlapbboxlist.count(item) > 1:
temp_overlapbboxlist.remove(item)
newly_added_overlapbboxlist = temp_overlapbboxlist
imagebboxlist.extend(newly_added_overlapbboxlist)
if time%2 == 1 and time//2 > 0:
imagebboxlist.extend(newly_added_overlapbboxlist)

if len(imagebboxlist) != len(set(imagebboxlist)):
# remove duplicate to make it run faster
for item in set(imagebboxlist):
if imagebboxlist.count(item) % 2 == 0:
while item in imagebboxlist:
imagebboxlist.remove(item)
else:
while imagebboxlist.count(item) > 1:
imagebboxlist.remove(item)
for bbox in imagebboxlist:
pixmap.invertIRect(bbox * self.scale)

img = QImage(pixmap.samples, pixmap.width, pixmap.height, pixmap.stride, QImage.Format_RGB888)
qpixmap = QPixmap.fromImage(img)

Expand Down