Research on flash bitmap Technology (7): pixel particle 2

Source: Internet
Author: User


 
Recently, I will slow down to update my blog. The key is that many things have not been completed. Sometimes the program has come out, but I still don't know much about it. It's a bit dumb at first. This application is available in
Wonderfl has read it, but it has been hard to understand this program. Today I learned it step by step. Obtain the pixels of the image and combine these pixels into the above pattern through changes.

Process:

1. Read external Images

We use loade to read external images. In Flex, we can use embed embedding to read external images. Here we only talk about flash.

Private function Init (): void
{
// Load a bitmap externally
Loader = new loader ();
Loader. Load (New URLRequest (URL ));
Loader. contentloaderinfo. addeventlistener (event. Complete, onloadcomplete );
}


Ii. Processing pixels

We need to get some pixel values and then change these pixel values. Generally, we use the getpixel () method in the bitmap class to process them.

As follows:

VaR color: uint = bitmap. bitmapdata. getpixel (J, I );

After we obtain the data, we need to save it. In advance, we can build a simplest data model, which contains the coordinates of a space point and XYZ, we save and set the pixels.

Package
{
// Basic spatial point Model
Public class point3d
{
Public var X: number;
Public var y: number;
Public var Z: number;

Public Function point3d (X: Number, Y: Number, Z: Number)
{
This. x = X;
This. Y = y;
This. z = z;
}
}
}



And then save it as an array.

For (var I: Int = 0; I <BMP. height; I ++)
{
For (var j: Int = 0; j <BMP. width; j ++)
{
VaR color: uint = bitmap. bitmapdata. getpixel (J, I );
VaR RED: Int = (color> 16) & 0xff;
VaR Px: Number = (J * 5)-(bitmap. width/2*5 );
VaR py: Number = (I * 5)-(bitmap. Height/2*5 );
If (Red> 1)
{
VaR point3d: point3d = new point3d (PX, Py, INT (Red/2 ));
Point3darray. Push (point3d );
}
}
}



After saving, we can get an array of points. These point operations allow us to re-set the pixels or rotate the point.

3. Rendering Images

With these pixel values and points, we can locate the points again, while the position is based on setpixel32 with transparency.

Private function render (): void
{
// Cyclic Detection
Temp. Lock ();
Temp. fillrect (temp. rect, 0x00 );
For each (var p: point3d in point3darray)
{
VaR scale: Number = fL/(FL + P. z );
VaR viewx: Int = centerx + p. x * scale;
VaR viewy: Int = centery + P. y * scale;
Color = 0xff000000 | P. z <16 | P. z <8 | P. Z;
Temp. setpixel32 (viewx, viewy, color );
}
Temp. Unlock ();

}



4. coordinate rotation

 
Point rotation is a very common operation. Here we only need to use formulas to achieve this, in order to increase the interesting angle of view.


// Rotate y
Private function rotationy (P: point3d, angle: Number): void
{
P. x = math. Cos (angle * Math. PI/180) * P. x-Math.sin (angle * Math. PI/180) * P. Z;
P. z = math. Cos (angle * Math. PI/180) * P. Z + math. Sin (angle * Math. PI/180) * P. X;
}



Total code:

Package
{
Import flash. display. Sprite;
Import flash. Events .*;
Import flash. display. loader;
Import flash. display. Bitmap;
Import flash. display. bitmapdata;
Import flash.net .*;
Import flash. Geom .*;
Import flash. utils. bytearray;
Import point3d;

Public class main extends Sprite
{
Private var Loader: loader;
Private var URL: String = "1.jpg ";
Private var BMP: bitmapdata;
Private var temp: bitmapdata;

Private var point3darray: array = []; // array of record points
Private var Vy: Number = 0;

Private var FL: Int = 500; // perspective
Private var centerx: Int = int (stage. stagewidth/2 );
Private var centery: Int = int (stage. stageheight/2 );
Private var C1: int;
Private var color: uint;
Public Function main ()
{
Init ();
Trace (centery );
}
Private function Init (): void
{
// Load a bitmap externally
Loader = new loader ();
Loader. Load (New URLRequest (URL ));
Loader. contentloaderinfo. addeventlistener (event. Complete, onloadcomplete );
}
Private function onloadcomplete (Event: Event): void
{
VaR bitmap: bitmap = loader. content as bitmap;
BMP = new bitmapdata (bitmap. Width, bitmap. Height, false, 0 xffffff );
Addchild (Bitmap );
// Obtain the value of each pixel
For (var I: Int = 0; I <BMP. height; I ++)
{
For (var j: Int = 0; j <BMP. width; j ++)
{
VaR color: uint = bitmap. bitmapdata. getpixel (J, I );
VaR RED: Int = (color> 16) & 0xff;
VaR Px: Number = (J * 5)-(bitmap. width/2*5 );
VaR py: Number = (I * 5)-(bitmap. Height/2*5 );
If (Red> 1)
{
VaR point3d: point3d = new point3d (PX, Py, INT (Red/2 ));
Point3darray. Push (point3d );
}
}
}

Temp = new bitmapdata (stage. stagewidth, stage. stageheight, true, 0x00 );
Addchild (New Bitmap (temp ));
Render ();
Addeventlistener (event. enter_frame, run );
}
Private function run (Event: Event): void
{

Vy + = 1;
If (Vy> 5)
Vy = 0;
For each (var p: point3d in point3darray)
{
Rotationy (p, Vy );
}
Render ();

}
// Render an image
Private function render (): void
{
// Cyclic Detection
Temp. Lock ();
Temp. fillrect (temp. rect, 0x00 );
For each (var p: point3d in point3darray)
{
VaR scale: Number = fL/(FL + P. z );
VaR viewx: Int = centerx + p. x * scale;
VaR viewy: Int = centery + P. y * scale;
// C1 = 255 + (P. Z + 50)/100*255 );
// C1 = C1 <0? 0: P. Z;
// C1 = C1> 255? 255: P. Z;
Color = 0xff000000 | P. z <16 | P. z <8 | P. Z;
Temp. setpixel32 (viewx, viewy, color );
}
Temp. Unlock ();

}

// Rotate y
Private function rotationy (P: point3d, angle: Number): void
{
P. x = math. Cos (angle * Math. PI/180) * P. x-Math.sin (angle * Math. PI/180) * P. Z;
P. z = math. Cos (angle * Math. PI/180) * P. Z + math. Sin (angle * Math. PI/180) * P. X;
}

}
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.