Skip to content

Commit

Permalink
Merge pull request #65 from waptaff/integer-scaling
Browse files Browse the repository at this point in the history
Implement next-power-of-2 image scaling.
  • Loading branch information
jepler authored Mar 9, 2020
2 parents e492f3b + c8f569c commit d7eaf1f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
4 changes: 2 additions & 2 deletions cropgtk.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,8 @@ def run(self):
i = Image.open(image_name)
drag.w, drag.h = i.size
scale = 1
scale = max (scale, (drag.w-1)/max_w+1)
scale = max (scale, (drag.h-1)/max_h+1)
scale = max (scale, nextPowerOf2((drag.w-1)/(max_w+1)))
scale = max (scale, nextPowerOf2((drag.h-1)/(max_h+1)))
i.thumbnail((drag.w/scale, drag.h/scale))
except (IOError,) as detail:
m = gtk.MessageDialog(self['window1'],
Expand Down
14 changes: 14 additions & 0 deletions cropgui_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,20 @@ def clamp(value, low, high):
if high < value: return high
return value

def nextPowerOf2(n):
count = 0;
ceiln = math.ceil(n)
# First ceiln in the below condition is for the
# case where ceiln is 0
if (ceiln and not(ceiln & (ceiln - 1))):
return ceiln

while (ceiln != 0):
ceiln >>= 1
count += 1

return 1 << count;

def ncpus():
if os.path.exists("/proc/cpuinfo"):
return open("/proc/cpuinfo").read().count("bogomips") or 1
Expand Down

0 comments on commit d7eaf1f

Please sign in to comment.