Resize an image and make a thumb !

Hey !
After two months without any article, I’m ready to start again activity on this blog ! For sure, it’s the start of the new school year !

So today, a little script really useful to make a thumb in ActionScript 3 !
EDIT : deleted my previous script, this one coming from Cult Creative is awesome :

/**
* fitImage
* @ARG_object   the display object to work with
* @ARG_width    width of the box to fit the image into
* @ARG_height   height of the box to fit the image into
* @ARG_center   should it offset to center the result in the box
* @ARG_fillBox  should it fill the box, may crop the image (true), or fit the whole image within the bounds (false)
**/
 
private function fitImageProportionally( ARG_object:DisplayObject, ARG_width:Number, ARG_height:Number, ARG_center:Boolean = true, ARG_fillBox:Boolean = true ):Bitmap {
 
    var tempW:Number = ARG_object.width;
    var tempH:Number = ARG_object.height;
 
    ARG_object.width = ARG_width;
    ARG_object.height = ARG_height;
 
    var scale:Number = (ARG_fillBox) ? Math.max(ARG_object.scaleX, ARG_object.scaleY) : Math.min(ARG_object.scaleX, ARG_object.scaleY);
 
    ARG_object.width = tempW;
    ARG_object.height = tempH;
 
    var scaleBmpd:BitmapData = new BitmapData(ARG_object.width * scale, ARG_object.height * scale);
    var scaledBitmap:Bitmap = new Bitmap(scaleBmpd, PixelSnapping.ALWAYS, true);
    var scaleMatrix:Matrix = new Matrix();
    scaleMatrix.scale(scale, scale);
    scaleBmpd.draw( ARG_object, scaleMatrix );
 
    if (scaledBitmap.width > ARG_width || scaledBitmap.height > ARG_height) {
 
        var cropMatrix:Matrix = new Matrix();
        var cropArea:Rectangle = new Rectangle(0, 0, ARG_width, ARG_height);
 
        var croppedBmpd:BitmapData = new BitmapData(ARG_width, ARG_height);
        var croppedBitmap:Bitmap = new Bitmap(croppedBmpd, PixelSnapping.ALWAYS, true);
 
        if (ARG_center) {
            var offsetX:Number = Math.abs((ARG_width -scaleBmpd.width) / 2);
            var offsetY:Number = Math.abs((ARG_height - scaleBmpd.height) / 2);
 
            cropMatrix.translate(-offsetX, -offsetY);
        }
 
        croppedBmpd.draw( scaledBitmap, cropMatrix, null, null, cropArea, true );
        return croppedBitmap;
 
    } else {
        return scaledBitmap;
    }
 
}

An other method coming from Thibault Imbert !

package {
 
	import flash.display.BitmapData;
	import flash.geom.ColorTransform;
	import flash.geom.Matrix;
 
	/**
	 * @author Maxime Cousinou
	 */
	public class BitmapManager {
 
		public static function reduceBitmapData(bmp:BitmapData, ratio:Number, transparent:Boolean = true):BitmapData {
 
			var bmpData:BitmapData = new BitmapData(Math.round(bmp.width * ratio), Math.round(bmp.height * ratio), transparent, 0x00FFFFFF);
			var scaleMatrix:Matrix = new Matrix(bmpData.width / bmp.width, 0, 0, bmpData.height / bmp.height, 0, 0);
			bmpData.draw(bmp, scaleMatrix);
 
			return bmpData;
		}
 
		public static function resizeBitmapData(bmp:BitmapData, ratio:Number, transparent:Boolean = true):BitmapData {
 
			var bmpData:BitmapData = new BitmapData(Math.round(bmp.width * ratio), Math.round(bmp.height * ratio), transparent, 0x00FFFFFF);
			var scaleMatrix:Matrix = new Matrix(bmpData.width / bmp.width, 0, 0, bmpData.height / bmp.height, 0, 0);
			var colorTransform:ColorTransform = new ColorTransform();
			bmpData.draw(bmp, scaleMatrix, colorTransform, null, null, true);
 
			return bmpData;
		}
 
		public static function resampleBitmapData(bmp:BitmapData, ratio:Number, transparent:Boolean = true):BitmapData {
 
			if (ratio >= 1) {
				return BitmapManager.resizeBitmapData(bmp, ratio, transparent);
			} else {
				var bmpData:BitmapData = bmp.clone();
				var appliedRatio:Number = 1;
 
				do {
					if (ratio < 0.5 * appliedRatio) {
						bmpData = BitmapManager.resizeBitmapData(bmpData, 0.5, transparent);
						appliedRatio = 0.5 * appliedRatio;
					} else {
						bmpData = BitmapManager.resizeBitmapData(bmpData, ratio / appliedRatio, transparent);
						appliedRatio = ratio;
					}
				} while (appliedRatio != ratio);
 
				return bmpData;
			}
		}
	}
}

Tribute to the Metal’s kings

Yesterday my brother was come back from Romania. And for sure, we speak a lot about music ! Metal music, with many great leader which died recently.

So today it’s my first post on music for a tribute to the dead Metal’s kings !

Ronnie James Dio (ex-Black Sabbath, ex-Rainbow, Dio, Heaven & Hell) :

Peter Steele (ex-Carnivore, Type O Negative) :

Quorthon (Bathory) :

Valfar (Windir) :

And many others… R.I.P.

The Settlers of UTBM

This is my last project at the UTBM. It is based on the Settlers of Catan‘s game. I made this project with two schoolmates, we did it in Java with Swing’s library.

I retain of this project a hard utilization of the Swing’s library : it is really another way of conception than ActionScript 3, you manage all as you want, but that’s not easy to see out of the box.

This is the link of the game, you can find all packages and a .jar to run the game ! We tried to respect the MVC model ! I hope you enjoy !

I will add some news soon 😉

A new Hope

On this Geek Pride Day, I received the result of my examination for the famous Gobelins french school. I’m accepted 🙂
It’s really a good news for me (see references with the title in Star Wars 😉 ) !! I will stop my reading courses at the UTBM in june. The UTBM is an engineering school, but I am really upset about it !
So what is coming ? First of all, I will live in Annecy a beautiful city. After so many years in Belfort, it is welcome ! Secondly I go back in the Multimedia, C/C++ and Java is henceforth finish, I will be specialized in the Internet’s technologies. Thirdly I will be in an apprenticeship, one week at school, one week in an entreprise. That is great !
The training is for two years, so I will not be an engineer one day, but it does not matter.

A new hope, a new beginning, and a famous school !
Now I’m looking for a web agency, and an accomodation !

See ya

The final Countdown

Hey there ! This is a little script to made a simple countdown on 12/12/2012 at 12:12:12. You know, the end of the world 😉
The result.

And the code :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package {
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
 
	/**
	 * @author Aymeric
	 */
	public class Countdown extends Sprite {
 
		private var target:Date;
		private var now:Date;
		private var currentTime:Number;
		private var targetTime:Number;
		private var time:TextField;
		private var format:TextFormat; 
 
		public function Countdown() {
 
			target = new Date(2012, 11, 12, 12, 12, 12); // january = 0 -> december = 11
 
			time = new TextField();
			format = new TextFormat("Arial", 30);
			time.textColor = 0xFF0000;
			time.x = stage.stageWidth/2 - 100;
			time.y = stage.stageHeight/2 - 50;
			time.embedFonts = true;
 
			addChild(time);
 
			addEventListener(Event.ENTER_FRAME, refresh);
		}
 
		private function refresh(e:Event):void {
 
			now = new Date();
			currentTime = now.getTime();
			targetTime = target.getTime();
 
			var nbrSeconds:Number = (targetTime - currentTime)/1000;
			var seconds:Number = Math.floor(nbrSeconds%60);
			var minutes:Number = Math.floor(nbrSeconds/60%60);
			var hours:Number = Math.floor(nbrSeconds/3600%24);
			var days:Number = Math.floor(nbrSeconds/3600/24);
 
			var secondText:String;
			var minuteText:String;
			var hourText:String;
 
			if (seconds < 10) {
				secondText = "0" + seconds;
			} else {
				secondText = seconds.toString();
			}
 
			if (minutes < 10) {
				minuteText = "0" + minutes;
			} else {
				minuteText = minutes.toString();
			}
 
			if (hours < 10) {
				hourText = "0" + hours;
			} else {
				hourText = hours.toString();
			}
 
			time.text = days.toString() + " : " + hourText + " : " + minuteText + " : " + secondText;
			time.autoSize = TextFieldAutoSize.LEFT;
			time.setTextFormat(format);
		}
	}
}

I used an EnterFrame to refresh the countdown, but I have hesitated with a Timer which is called each seconds. I think the EnterFrame is better because the Timer depends of user’s computer. If you can highlight me, do not hesitate !

Why DRM sucks ? My thought on piracy

As you may know, there is a war against piracy on digital contents. Piracy is of course prohibited, but moreover softwares’ companies try to block this practical thanks to new technologies. DRM for Digital Rights Management is used to describe any technology that inhibits uses of digital content not desired or intended by the content provider. So in fact, companies just wish to protect more their works this is natural. However there is a real unease, what is wrong with DRM ?

To explain my point of view, I start with the new controversial Ubisoft’s DRM. Ubisoft is a great company which made marvellous games such as Assassin’s Creed, Far Cry, Prince of Persia, ect… and of course as many others it uses DRM. Before using is own DRM, Ubisoft used SecuROM’s software. SecuROM aims to resist home media duplication devices, professional duplicators, and attempts at reverse engineering software. In fact SecuROM limits the number of activation on your product. For instance, if I just bought Far Cry 2, I must activate my game on the Internet through SecuROM to be able to play. But, there are 2 problems : the number of activations is controlled, no more than 5 in general and I should revoke SecuROM before uninstall my games for taking back my activation. This is a strong problem if your disk crash ! The second is if SecuROM’s server is down in a few years, I will not be able to play my game ! And this is completely unacceptable.

Now get back on the own Ubisoft’s DRM. Now, if I buy Assassin’s Creed 2, I download and install it, I will suffer if I am not connected on the Internet I can’t play, because the game requests a permanent connection. What the fuck guys ??? This is only a solo game !! This is preposterous… and of course the first weekend there were many connections’ problems with the server. People which just bought the game were not able to play. And nowadays there are always problems. Skid Row’s pirates have just complete their crack to revoke Ubisoft’s DRM, and tell in a letter that Ubisoft should improve quality of their games before thinking the DRM. And they add that they simplify life of honest customers ! Pirate 1 – Ubisoft 0.

I buy games, many games, I really enjoyed the first Assassin’s Creed but I hate DRM. I will buy the new Assassin’s Creed ? I don’t think so, because I don’t want to support this kind of practice. Oh for sure, I will buy it if they revoke their crap, or if it is really really cheap. And there is other problem with DVD, I just want to see my film and I can’t skip ads, piracy’s warning… I have bought it, what is the problem ?? So to conclude this first part on DRM, everything which compel the honest customer should be banned.

I have a dream, it is a world of freedom with a total access on Art for everyone, but it is impossible in our world ruled by money. I am the first to say that all work deserves payment furthermore if you earn money thanks to the work of someone else, but I can not abide that piracy is the first problem of the industry. The first problem of movies is the price of movie theater. In Belfort, for a student !, it is 7.10 €. This is really too expensive, for sure if it costs 3€ I go more often to the cinema and certainly I will consume food or drink. But no, actually I wait to buy the dvd, or rent it. And you know, if the movie is good people will see it. That’s a fact, for example the last Cameron’s movie : Avatar.

Piracy is also a means to judge quality before buy it. There are so many movies, so many bad movies… that you don’t want to pay 7.10 €. Our consumer society product always more and more, but quality is not here. It seems to be that artists are more attracted by money than their work. I am a metal music fan, and when you speak with some underground bands after concerts, you feel their passion. Of course they would like to live thanks to their music, but in fact this is not the most important. No, the most important for them is the audience, the people which waits since several years to see them in his town ! The man chich pay a beer to the band and like to share his passion. This is what a real artist should love !! Not the number of discs sold, and if he can earn more money if he puts a fucking DRM on them. You know when I buy a CD, I am proud to give money to the band, to contibute, to show that I like their work. And thanks to that they can organize concert and make an other good CD later.

To conclude, I would like to remember that pirates are people who consume the most. And there is one thing that I am sure : if they have all the money they want they will pay for all the entertainments they do.

Handle the Right Click in AS3

It can not be denied that handling the Right Click in Flash is a problem.
At first, it was made to display a Contextual Menu with Flash Player’s options, this is great for a website. However Flash is also used to make browser’s games and it is reductive if we can’t use this. For example in a Point & Click game, like the mythical Diablo, the Right Click is very important and simply to use. If you want to develop that kind of game with Flash (one of my dream ;-)) it will be difficult to manage this. In this direction, old Apple’s mouses with their only click were preposterous !! I think that was a commercial argument like “our Operating Systems is so ergonomic that you don’t need an alternative button”. But if you used software like Photoshop, you will always push the ctrl button on the keyboard, but also shift for other action or alt, cmd…

So today I try to look over that. And what I notice is :
– the right click and the middle click are functional in AS3 (those MouseEvent exist) but work only for Air !!
– you can easily change the context menu options (good example) and add functions but About and Settings can not be removed from the menu !!
– use JavaScript to handle the Right Click thanks to this class. Here we go :

I created a .swf in which with a right click you create a circle, and with a left click on a circle delete it (the application). Note it seems it is no more updated. But what happens ? It doesn’t work with Safari and Opera… failed. And you should have notice that I created a circle by pressing the right button, logically it should be the left… ?? Because of the JavaScript, you can’t give an occurence through it to Flash, so how can I obtain the target ?
This is the code :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
var circle:Circle;
 
stage.addEventListener(MouseEvent.MOUSE_DOWN, deleteCircle);
 
var i:int = 0;
 
var methodName:String = "rightClick";
var method:Function = rightClick;
ExternalInterface.addCallback(methodName, method);
 
function rightClick():void {
 
	circle = new Circle();
	i++;
	circle.name = String(i);
	addChild(circle);
	circle.x = mouseX;
	circle.y = mouseY;
}
 
function deleteCircle(m:MouseEvent):void {
 
	if (m.target.name != null) {
		removeChild(getChildByName(m.target.name));
	}
}

So to conclude, at the moment you can’t use cleanly the right click in Flash like the left. You will always have the contextual menu with About and Settings’ buttons. But in fact who use them ? Aside on a website using Webcam or Microphone. So I think that developers should have the choice if they want a contextual menu (by default if we don’t need the right click) or not to have a more ergonomical application.