invert zl meaning; zl=0 is 100%, zl=+n is zoomed out
authorTero Marttila <terom@fixme.fi>
Sat, 10 Apr 2010 22:18:41 +0300
changeset 127 df89d13f2354
parent 126 2e0f7cbe528f
child 128 66c95c2d212c
child 129 305f6d590440
invert zl meaning; zl=0 is 100%, zl=+n is zoomed out
pngtile/render.py
src/lib/png.c
static/tiles2.js
--- a/pngtile/render.py	Wed Jan 27 02:18:58 2010 +0200
+++ b/pngtile/render.py	Sat Apr 10 22:18:41 2010 +0300
@@ -127,7 +127,7 @@
         </div>
 
         <script type="text/javascript">
-            var tile_source = new Source("%(tile_url)s", %(tile_width)d, %(tile_height)d, -4, 0, %(img_width)d, %(img_height)d);
+            var tile_source = new Source("%(tile_url)s", %(tile_width)d, %(tile_height)d, 0, 4, %(img_width)d, %(img_height)d);
             var main = new Viewport(tile_source, "viewport");
         </script>
     </body>
@@ -150,6 +150,9 @@
 def scale_by_zoom (val, zoom) :
     """
         Scale dimension by zoom factor
+
+        zl > 0 -> bigger
+        zl < 0 -> smaller
     """
 
     if zoom > 0 :
@@ -167,7 +170,7 @@
         Checks if a tile with the given dimensions should be cached
     """
 
-    return (scale_by_zoom(width, -zl) * scale_by_zoom(height, -zl)) > CACHE_THRESHOLD
+    return (scale_by_zoom(width, zl) * scale_by_zoom(height, zl)) > CACHE_THRESHOLD
 
 def render_raw (image, width, height, x, y, zl) :
     """
@@ -213,8 +216,8 @@
     """
 
     # remap coordinates by zoom
-    x = scale_by_zoom(x, -zoom)
-    y = scale_by_zoom(y, -zoom)
+    x = scale_by_zoom(x, zoom)
+    y = scale_by_zoom(y, zoom)
 
     # do we want to cache this?
     if check_cache_threshold(TILE_WIDTH, TILE_HEIGHT, zoom) :
@@ -230,8 +233,8 @@
         Render arbitrary tile, returning PNG data
     """
 
-    x = scale_by_zoom(cx - width / 2, -zoom)
-    y = scale_by_zoom(cy - height / 2, -zoom)
+    x = scale_by_zoom(cx - width / 2, zoom)
+    y = scale_by_zoom(cy - height / 2, zoom)
 
     # safely limit
     if width * height > MAX_PIXELS :
--- a/src/lib/png.c	Wed Jan 27 02:18:58 2010 +0200
+++ b/src/lib/png.c	Sat Apr 10 22:18:41 2010 +0300
@@ -440,11 +440,11 @@
 static int pt_png_encode_zoomed (struct pt_png_img *img, const struct pt_png_header *header, const uint8_t *data, const struct pt_tile_info *ti)
 {
     // size of the image data in px
-    size_t data_width = scale_by_zoom_factor(ti->width, -ti->zoom);
-    size_t data_height = scale_by_zoom_factor(ti->height, -ti->zoom);
+    size_t data_width = scale_by_zoom_factor(ti->width, ti->zoom);
+    size_t data_height = scale_by_zoom_factor(ti->height, ti->zoom);
 
     // input pixels per output pixel
-    size_t pixel_size = scale_by_zoom_factor(1, -ti->zoom);
+    size_t pixel_size = scale_by_zoom_factor(1, ti->zoom);
 
     // bytes per output pixel
     size_t pixel_bytes = 3;
@@ -462,7 +462,7 @@
     const png_color *c = &header->palette[0];
     
     // only supports zooming out...
-    if (ti->zoom >= 0)
+    if (ti->zoom < 0)
         RETURN_ERROR(PT_ERR_TILE_ZOOM);
 
     if ((row_buf = malloc(row_bytes)) == NULL)
@@ -484,7 +484,7 @@
         memset(row_buf, 0, row_bytes);
 
         // ...includes pixels starting from this row.
-        size_t in_row_offset = ti->y + scale_by_zoom_factor(out_row, -ti->zoom);
+        size_t in_row_offset = ti->y + scale_by_zoom_factor(out_row, ti->zoom);
         
         // ...each out row includes pixel_size in rows
         for (size_t in_row = in_row_offset; in_row < in_row_offset + pixel_size && in_row < header->height; in_row++) {
@@ -492,7 +492,7 @@
             for (size_t in_col = ti->x; in_col < ti->x + data_width && in_col < header->width; in_col++) {
 
                 // ...for this output pixel
-                size_t out_col = scale_by_zoom_factor(in_col - ti->x, ti->zoom);
+                size_t out_col = scale_by_zoom_factor(in_col - ti->x, -ti->zoom);
                 
                 // get pixel RGB data
                 png_pixel_data(&c, header, data, in_row, in_col);
--- a/static/tiles2.js	Wed Jan 27 02:18:58 2010 +0200
+++ b/static/tiles2.js	Sat Apr 10 22:18:41 2010 +0300
@@ -9,8 +9,8 @@
  *  x, y        - the pixel coordinates of the top-left corner
  *                XXX: these are scaled from the image coordinates by the zoom level
  *
- *  zl          - the zoom level used, out < zl < in.
- *                The image pixels are scaled by powers-of-two, so a 256x256 tile at zl=-1 shows a 512x512 area of the
+ *  zl          - the zoom level used, in < zl < out.
+ *                The image pixels are scaled by powers-of-two, so a 256x256 tile at zl=1 shows a 512x512 area of the
  *                1:1 image.
  */
 var Source = Class.create({
@@ -234,7 +234,7 @@
         
         // delta > 0 : scroll up, zoom in
         // delta < 0 : scroll down, zoom out
-        delta = delta < 0 ? -1 : 1;
+        delta = delta < 0 ? 1 : -1;
 
         // Firefox's DOMMouseEvent's pageX/Y attributes are broken. layerN is for mozilla, offsetN for IE, seems to work
 
@@ -315,8 +315,8 @@
     /** Zoom à la delta such that the given (current) absolute (x, y) co-ordinates will be at the top left */
     zoom_to: function (x, y, delta) {
         return this.zoom_scaled(
-            scaleByZoomDelta(x, delta),
-            scaleByZoomDelta(y, delta),
+            scaleByZoomDelta(x, -delta),
+            scaleByZoomDelta(y, -delta),
             delta
         );
     },
@@ -324,8 +324,8 @@
     /** Zoom à la delta such that the given (current) absolute (x, y) co-ordinates will be at the center */
     zoom_center_to: function (x, y, delta) {
         return this.zoom_scaled(
-            scaleByZoomDelta(x, delta) - this.center_offset_x,
-            scaleByZoomDelta(y, delta) - this.center_offset_y,
+            scaleByZoomDelta(x, -delta) - this.center_offset_x,
+            scaleByZoomDelta(y, -delta) - this.center_offset_y,
             delta
         );
     },
@@ -341,18 +341,18 @@
 
     /** Zoom in one level, keeping the view centered */
     zoom_in: function () {
-        return this.zoom_centered(+1);
+        return this.zoom_centered(-1);
     },
     
     /** Zoom out one level, keeping the view centered */
     zoom_out: function () {
-        return this.zoom_centered(-1);
+        return this.zoom_centered(+1);
     },
 
     /** Center the view on the given coords, and zoom in, if possible */
     center_and_zoom_in: function (cx, cy) {
         // try and zoom in
-        if (this.update_zoom(1)) {
+        if (this.update_zoom(-1)) {
             // scaled coords
             cx = scaleByZoomDelta(cx, 1);
             cy = scaleByZoomDelta(cy, 1);
@@ -548,11 +548,11 @@
     update_zoom_ui: function () {
         // deactivate zoom-in button if zoomed in
         if (this.btn_zoom_in)
-            (this.zoom_layer.level >= this.source.zoom_max) ? this.btn_zoom_in.disable() : this.btn_zoom_in.enable();
+            (this.zoom_layer.level <= this.source.zoom_min) ? this.btn_zoom_in.disable() : this.btn_zoom_in.enable();
         
         // deactivate zoom-out button if zoomed out
         if (this.btn_zoom_out)
-            (this.zoom_layer.level <= this.source.zoom_min) ? this.btn_zoom_out.disable() : this.btn_zoom_out.enable();
+            (this.zoom_layer.level >= this.source.zoom_max) ? this.btn_zoom_out.disable() : this.btn_zoom_out.enable();
         
         // link-to-image
         this.update_image_link();
@@ -632,7 +632,7 @@
      * For zoom levels different than this layer's level, this will resize the tiles!
      */
     update_tiles: function (zoom_level) {
-        var zd = zoom_level - this.level;
+        var zd = this.level - zoom_level;
         
         // desired tile size
         var tw = scaleByZoomDelta(this.source.tile_width, zd);
@@ -656,8 +656,8 @@
     }
 });
 
-// scale the given co-ordinate by a zoom delta. If we zoom in (dz > 0), n will become larger, and if we zoom 
-// out (dz < 0), n will become smaller.
+// scale the given co-ordinate by a zoom delta. If we zoom out (dz > 0), n will become larger, and if we zoom 
+// in (dz < 0), n will become smaller.
 function scaleByZoomDelta (n, dz) {
     if (dz > 0)
         return n << dz;