Skip to content
This repository was archived by the owner on Nov 18, 2025. It is now read-only.

Commit d7eaf1f

Browse files
authored
Merge pull request #65 from waptaff/integer-scaling
Implement next-power-of-2 image scaling.
2 parents e492f3b + c8f569c commit d7eaf1f

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

cropgtk.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,8 @@ def run(self):
246246
i = Image.open(image_name)
247247
drag.w, drag.h = i.size
248248
scale = 1
249-
scale = max (scale, (drag.w-1)/max_w+1)
250-
scale = max (scale, (drag.h-1)/max_h+1)
249+
scale = max (scale, nextPowerOf2((drag.w-1)/(max_w+1)))
250+
scale = max (scale, nextPowerOf2((drag.h-1)/(max_h+1)))
251251
i.thumbnail((drag.w/scale, drag.h/scale))
252252
except (IOError,) as detail:
253253
m = gtk.MessageDialog(self['window1'],

cropgui_common.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,20 @@ def clamp(value, low, high):
4747
if high < value: return high
4848
return value
4949

50+
def nextPowerOf2(n):
51+
count = 0;
52+
ceiln = math.ceil(n)
53+
# First ceiln in the below condition is for the
54+
# case where ceiln is 0
55+
if (ceiln and not(ceiln & (ceiln - 1))):
56+
return ceiln
57+
58+
while (ceiln != 0):
59+
ceiln >>= 1
60+
count += 1
61+
62+
return 1 << count;
63+
5064
def ncpus():
5165
if os.path.exists("/proc/cpuinfo"):
5266
return open("/proc/cpuinfo").read().count("bogomips") or 1

0 commit comments

Comments
 (0)