Author Topic: Soft zoom algorithm  (Read 2950 times)

0 Members and 1 Guest are viewing this topic.

Offline Elder0010

  • ZX 81
  • *
  • Posts: 5
  • Karma: 0
    • View Profile
Soft zoom algorithm
« on: January 14, 2012 »
Hey there, i'm trying to code a realtime text zoomscroller but i'm having some issues to find a proper algorithm.

Here is what i have in mind:
assuming that each char is an 8X8 pixel square for example:
00000000
00000000
01111100
01100100
01111100
01100000
01100000
01000000
(this can be the letter P - 1 byte per line)

assuming that i want to zoom from 1X1 to 2X2, for each step of the algorithm i have to double only certain couple of pixels, for x and y like

STEP1 (one step per frame - i double the the border pixels)
//X zoom
foreach row of the char (1 byte)
  double pixel (row+0)  //first  pixel of each row
  double pixel (row+7)  //Last pixel of each row
endforeach

//Y zoom
foreach col of the char (1 byte, formed by "vertical" pixels)
  double pixel (colum+0) //First pixel of each column
  double pixel (column+56) //last pixel of each column
endforeach
-----------------------------------------------------------------------
STEP2 (i double the leftborder+1 pixel and rightborder-1 pixels)
//X zoom
foreach row of the char (1 byte)
  double pixel (row+1)  //second  pixel of each row
  double pixel (row+6)  //last-1 pixel of each row
endforeach

//Y zoom
foreach col of the char (1 byte, formed by "vertical" pixels)
  double pixel (colum+1) //Second pixel of each column
  double pixel (column+56) //last-1 pixel of each column
endforeach

(repeat for 7 frames to obtain a 1X1 to 2X2 zoom)

What do you think? This effect is quite common in all oldschool demos, so i know that it's doable with my C64. My approach is surely NOT optimized :(

« Last Edit: January 14, 2012 by Elder0010 »

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Soft zoom algorithm
« Reply #1 on: January 16, 2012 »
Normally you wouldn't do it like that unless speed was utterly essential.  It's a lot of code and it'd change any time a graphic changed size.

How you'd do it more generally is to do an x/y loop over the destination area and step across the source graphic at a rate that gives you the right source pixels.
Let's do that using floats so it's easy to understand:
Code: [Select]
int dstwidth=...
int dstheight=...
int x,y
int srcwidth=...
int srcheight=...

float dx = (float)srcwidth/dstwidth
float dy = (float)srcheight/dstheight

float xx,yy

yy=0
for y=0 to dstheight-1
  xx=0
  for x=0 to dstwidth-1
    setpixel(x,y,getpixel((int)xx,(int)yy))
    xx+=dx
  next
  yy+=dy
next

Obviously setpixel,getpixel can be massively optimised, but I hope that gives you the idea.

Jim
« Last Edit: January 16, 2012 by Jim »
Challenge Trophies Won:

Offline Elder0010

  • ZX 81
  • *
  • Posts: 5
  • Karma: 0
    • View Profile
Re: Soft zoom algorithm
« Reply #2 on: January 17, 2012 »
In my case speed requirement is critical, but your reply makes me think!
I need a more general approach that always starts from the original, instead of considering the last zoomed stuff.. this solves also the problem of zooming out back :D thanks!