Bitmap Resizer with anti-aliasing

Bitmap Resizer Version: 1.0
By: Krän

Introduction

This script allows to resize bitmaps with anti-aliasing.
It can only reduce pictures, and is super-duper slow.

Krän made this script for me long time ago. I'm looking for some help (or another script which can do the same thing) to improve this script.




Screenshots

Image

<- Original picture
-> resized with RM
---> resized with the script



Script



  1.  
  2.  
  3.  
  4. class Bitmap
  5.  
  6.  
  7.   def resize(zoom)
  8.     big_bmp = Bitmap.new(self.width*zoom, self.height*zoom)
  9.     big_bmp.stretch_blt(big_bmp.rect, self, self.rect)
  10.     return big_bmp
  11.   end
  12.  
  13.  
  14.   def resize_aa(zoom)
  15.     bmp = self.zoom_aa_x(zoom)
  16.     bmp = bmp.zoom_aa_y(zoom)
  17.     return bmp
  18.   end
  19.  
  20.  
  21.  
  22.   #--------------------------------------------------------------------------------------------------------------------------------
  23.   # - Nom : Script de redimensionnement avec Anti-Aliasing
  24.   # - Auteur : Krän
  25.   # - But : Réduire une image en adoussissant les contours.(Moins de crénelage)
  26.   # - Utilisation : Utilisez la fonction zoom_AA_x(zoom) ou zoom_AA_y(zoom)
  27.   #   sur un bitmap pour que cette fonction renvoie un autre bitmap dont la hauteur ou la largeur
  28.   #   ont changé. L'argument zoom correspond au rapport entre la nouvelle dimension sur l'acienne.
  29.   # - Exemple : bitmap.zoom_AA_x(0.5) renverra la même image que bitmap dont la largeur la
  30.   #   moitié de bitmap.
  31.   #--------------------------------------------------------------------------------------------------------------------------------
  32.   #
  33.   # Attention : pour le moment ce script ne permet que de réduire des images. L'argument zoom
  34.   # doit être inférieur à 1
  35.   #
  36.  
  37.  
  38.   def zoom_aa_x(zoom)
  39.     nouvelle_taille = zoom * self.width
  40.     nouveau_bitmap = Bitmap.new(nouvelle_taille,self.height)
  41.  
  42.     taille_section = self.width.to_f / nouvelle_taille.to_f
  43.  
  44.     #On analyse le nouveau bitmap dans sa largeur :
  45.     for x in 0..nouvelle_taille
  46.       #On établit où se trouve le curseur sur l'image d'origine :
  47.       curseur = x.to_f * taille_section
  48.  
  49.       #On détermine la section de l'image d'origine correspondant à un pixel de la nouvelle :
  50.       debut = curseur.to_int
  51.       fin = (curseur + taille_section).to_int
  52.       #On détermine les rapports de debut et fin de section (On détermine l'importance des pixels extrèmes) :
  53.       rapport1 = (curseur.to_int + 1 - curseur)
  54.       rapport2= (curseur + taille_section) - (curseur + taille_section).to_int
  55.  
  56.       for y in 0..self.height
  57.         #On crée 4 variables correspondant aux nouvelle couleurs du pixel :
  58.         red = 0.0
  59.         green = 0.0
  60.         blue = 0.0
  61.         alpha = 0.0
  62.  
  63.         for i in debut..fin
  64.           if i == debut
  65.             red += self.get_pixel(i,y).red * rapport1
  66.             green += self.get_pixel(i,y).green * rapport1
  67.             blue += self.get_pixel(i,y).blue * rapport1
  68.             alpha += self.get_pixel(i,y).alpha * rapport1
  69.           elsif  i == fin
  70.             red += self.get_pixel(i,y).red * rapport2
  71.             green += self.get_pixel(i,y).green * rapport2
  72.             blue += self.get_pixel(i,y).blue * rapport2
  73.             alpha += self.get_pixel(i,y).alpha * rapport2
  74.           else
  75.             red += self.get_pixel(i,y).red
  76.             green += self.get_pixel(i,y).green
  77.             blue += self.get_pixel(i,y).blue
  78.             alpha += self.get_pixel(i,y).alpha
  79.           end
  80.         end
  81.         #On fait les moyennes
  82.         red /= taille_section
  83.         green /= taille_section
  84.         blue /= taille_section
  85.         alpha /= taille_section#taille_section
  86.  
  87.         #On donne une couleur au nouveau pixel :
  88.         nouveau_bitmap.set_pixel(x, y, Color.new(red,green,blue,alpha))
  89.       end
  90.     end
  91.     return nouveau_bitmap
  92.   end
  93.  
  94.  
  95.  
  96.   def zoom_aa_y(zoom)
  97.     nouvelle_taille = zoom * self.height
  98.     nouveau_bitmap = Bitmap.new(self.width,nouvelle_taille)
  99.  
  100.     taille_section = self.height.to_f / nouvelle_taille.to_f
  101.  
  102.     #On analyse le nouveau bitmap dans sa largeur :
  103.     for y in 0..nouvelle_taille
  104.       #On établit où se trouve le curseur sur l'image d'origine :
  105.       curseur = y.to_f * taille_section
  106.  
  107.       #On détermine la section de l'image d'origine correspondant à un pixel de la nouvelle :
  108.       debut = curseur.to_int
  109.       fin = (curseur + taille_section).to_int
  110.       #On détermine les rapports de debut et fin de section (On détermine l'importance des pixels extrèmes) :
  111.       rapport1 = (curseur.to_int + 1 - curseur)
  112.       rapport2= (curseur + taille_section) - (curseur + taille_section).to_int
  113.  
  114.       for x in 0..self.width
  115.         #On crée 4 variables correspondant aux nouvelle couleurs du pixel :
  116.         red = 0.0
  117.         green = 0.0
  118.         blue = 0.0
  119.         alpha = 0.0
  120.  
  121.         for i in debut..fin
  122.           if i == debut
  123.             red += self.get_pixel(x,i).red * rapport1
  124.             green += self.get_pixel(x,i).green * rapport1
  125.             blue += self.get_pixel(x,i).blue * rapport1
  126.             alpha += self.get_pixel(x,i).alpha * rapport1
  127.           elsif  i == fin
  128.             red += self.get_pixel(x,i).red * rapport2
  129.             green += self.get_pixel(x,i).green * rapport2
  130.             blue += self.get_pixel(x,i).blue * rapport2
  131.             alpha += self.get_pixel(x,i).alpha * rapport2
  132.           else
  133.             red += self.get_pixel(x,i).red
  134.             green += self.get_pixel(x,i).green
  135.             blue += self.get_pixel(x,i).blue
  136.             alpha += self.get_pixel(x,i).alpha
  137.           end
  138.         end
  139.         #On fait les moyennes
  140.         red /= taille_section
  141.         green /= taille_section
  142.         blue /= taille_section
  143.         alpha /= taille_section#taille_section
  144.  
  145.         #On donne une couleur au nouveau pixel :
  146.         nouveau_bitmap.set_pixel(x, y, Color.new(red,green,blue,alpha))
  147.       end
  148.     end
  149.     return nouveau_bitmap
  150.   end
  151.  
  152.  
  153. end
Expand to see the code.


Instructions

Juste use the methods resize and resize_aa on an existing bitmap to obtain the resized one.


Compatibility

RMXP compatible.


Credits and Thanks

- Krän
- the guy who drawn this nice pikachu :)
Quite a nice script !
Why separate operations on the width and length? You lose a lot of time on this. You should find a way to redraw x and y pixels at once. And regardless of the width/height ratio.

Pourquoi séparer les opérations sur la largeur et sur la longueur ? On perd pas mal de temps avec ca. Il faudrait trouver le moyen de traiter les deux à la fois, quel que soit le ratio h/l

regards,

berka
Quote:
You should find

Mathematics functions are really not what I'm good at. It's why I asked Krän to develop this script, and it's why I can't improve it.

So, I just added a function to use both operations. I asked Krän if he can work again on this script. I'm waiting for his answer.

And I was hoping this function already exists... it would be curious if it not exists, because it's super usefull.
berka wrote:
Why separate operations on the width and length? You lose a lot of time on this.


Not true. Imagine a 3x3 (9 pixels) image that you are reducing to 1 pixel. To compare each pixel to all of it's adjacent pixels, you would have the following number of comparisons:

    -------
    |3|5|3|
    -------
    |5|8|5|
    -------
    |3|5|3|
    ------- 40 comparisons

Now, doing only one direction at a time, you have

    -------
    |1|1|1|
    -------
    |2|2|2|
    -------
    |1|1|1|
    ------- 12 comparisons

    then

    -------
    |1|2|1|
    ------- 4 comparisons

    for a total of 16 to achieve the same results.

As your image increases in size, the difference should approach 1/2.
So it's still theoretically twice as efficient.

Original thread: https://www.hbgames.org/forums/viewtopic.php?t=67318