00001
00002
00003
00004
00005 from __future__ import division
00006
00007 from PIL import Image
00008
00009 from quadtree import *
00010 from image import *
00011
00012
00013 def linearise(grid):
00014 lst = []
00015 for cell in grid:
00016 lst.append(cell)
00017
00018 return lst
00019
00020
00021 def gridify(lst, dims):
00022 grid = Grid2D(dims, None)
00023
00024 for index, item in zip(grid.index_iter(), lst):
00025 grid[index] = item
00026
00027
00028
00029
00030 def max_tuple(tuple1, tuple2):
00031 lst = []
00032
00033 for el1, el2 in zip(tuple1, tuple2):
00034 lst.append(max(el1, el2))
00035
00036 return tuple(lst)
00037
00038
00039 def max_grid(grids):
00040 new_grid = Grid2D(grids[0].dims, None)
00041
00042 for index in grids[0].index_iter():
00043 new_grid[index] = grids[0][index]
00044
00045 for grid in grids[1:]:
00046 for index in grid.index_iter():
00047
00048 new_grid[index] = max_tuple(new_grid[index], grid[index])
00049
00050 return new_grid
00051
00052
00053 class ChannelMeasure(AbstractMeasure):
00054 def detail(self, grid, corner, dims, bias):
00055 col0 = self.aproximate(grid, corner, dims)
00056 w, h = dims
00057 x, y = corner
00058 end_corner = (x + w, y + h)
00059 col = 0
00060
00061 for cell in grid.window_iter(corner, end_corner):
00062 col1 = cell
00063 col += abs(col1 - col0)
00064
00065 detail = (col) / (w * h)
00066
00067 return detail
00068
00069 def aproximate(self, grid, corner, dims):
00070 w, h = dims
00071 x, y = corner
00072 end_corner = (x + w, y + h)
00073 col = 0
00074
00075 for cell in grid.window_iter(corner, end_corner):
00076 col0 = cell
00077 col += col0
00078
00079 area = w*h
00080
00081 return col / area
00082
00083 def blend(self, col1, col2, ratio):
00084 col = col1 * ratio + col2 * (1 - ratio)
00085
00086 return col
00087
00088 def blend3(self, col1, col2, col3, ratio1, ratio2, ratio3):
00089 col = col1 * ratio1 + col2 * ratio2 + col3 * ratio3
00090
00091 return col
00092
00093
00094 def outline_color(self):
00095 return 1
00096
00097
00098
00099 class ImageMeasure(AbstractMeasure):
00100 def detail(self, grid, corner, dims, bias):
00101 red0, green0, blue0, alpha0 = self.aproximate(grid, corner, dims)
00102 w, h = dims
00103
00104 x, y = corner
00105 end_corner = (x + w, y + h)
00106 red = 0
00107 blue = 0
00108 green = 0
00109
00110 for cell in grid.window_iter(corner, end_corner):
00111 red1, green1, blue1, alpha1 = cell
00112 red += abs(red1 - red0)
00113 green += abs(green1 - green0)
00114 blue += abs(blue1 - blue0)
00115
00116 detail = (red + blue + green) / (3 * w * h)
00117
00118 return detail
00119
00120 def aproximate(self, grid, corner, dims):
00121 w, h = dims
00122 x, y = corner
00123 end_corner = (x + w, y + h)
00124 red = 0
00125 blue = 0
00126 green = 0
00127 alpha = 0
00128
00129 for cell in grid.window_iter(corner, end_corner):
00130 red0, green0, blue0, alpha0 = cell
00131
00132 red += red0
00133 green += green0
00134 blue += blue0
00135 alpha += alpha0
00136 area = w*h
00137
00138 return (red / area, green / area, blue / area, alpha / area)
00139
00140 def blend(self, col1, col2, ratio):
00141 ratio_i = 1 - ratio
00142
00143 red1, green1, blue1, alpha1 = col1
00144 red2, green2, blue2, alpha2 = col2
00145
00146 red = red1 * ratio + red2 * ratio_i
00147 green = green1 * ratio + green2 * ratio_i
00148 blue = blue1 * ratio + blue2 * ratio_i
00149 alpha = alpha1 * ratio + alpha2 * ratio_i
00150
00151 return (red, green, blue, alpha)
00152
00153 def blend3(self, col1, col2, col3, ratio1, ratio2, ratio3):
00154 red1, green1, blue1, alpha1 = col1
00155 red2, green2, blue2, alpha2 = col2
00156 red3, green3, blue3, alpha3 = col3
00157
00158 red = red1 * ratio1 + red2 * ratio2 + red3 * ratio3
00159 green = green1 * ratio1 + green2 * ratio2 + green3 * ratio3
00160 blue = blue1 * ratio1 + blue2 * ratio2 + blue3 * ratio3
00161 alpha = alpha1 * ratio1 + alpha2 * ratio2 + alpha3 * ratio3
00162
00163 return (red, green, blue, alpha)
00164
00165
00166 def outline_color(self):
00167 return (1, 1, 1, 1)
00168
00169
00170
00171 def render_node(grid, node, outline=False, interpolate=False):
00172 if node.value == None:
00173 for child in node.children:
00174 render_node(grid, child, outline, interpolate)
00175 else:
00176 for index in node.index_iter():
00177 if outline and (index[0] == node.x or index[1] == node.y):
00178 grid[index] = node.tree.measure.outline_color()
00179 else:
00180 if interpolate:
00181 grid[index] = node.interpolate3(index)
00182 else:
00183 grid[index] = node.tree[index]
00184
00185
00186
00187 def render_outline(tree, factor, outline=False, interpolate=False):
00188 new_grid = Grid2D(tree.dims, (0, 0, 0, 1))
00189
00190 render_node(new_grid, tree.root, outline, interpolate)
00191 return new_grid