First look at the effect of loading an image, according to four vertices arbitrary transformation. Knowledge Points: 1. Bitmapcontext 2. Matrix transformationI. What is BitmapcontextOfficial explanation: The number of the components for each pixel in a bitmap graphics context are specified by a color space, defined by a Cgco Lorspaceref. The bitmap graphics context specifies whether the bitmap should contain an alpha channel, and how the bitmap is generated. In layman's terms: first, we create a bitmapcontext based on the image and draw a picture onto the bitmapcontext. At this point you can think of the picture as being made up of a lot of colored dots. A table is formed when the image is zoomed in locally. Each cell represents a single pixel that contains 4 elements: alpha (transparency) red, green, and blue each pixel contains information 1. Create
Cgcontextref contexref = cgbitmapcontextcreate (void *data, size_t width, size_t height, size_t bitspercomponent, size_t by Tesperrow, Cgcolorspaceref space, uint32_t bitmapinfo);
Data: The memory space required to create the Bitmapcontext, width is created by malloc: height of the picture: The maximum number of bytes per data in the picture Bitspercomponent:data Bytesperrow: Number of bits per row = number of picture columns (because there are 4 channels per point) space: Color range Bitmapinfo:bitmap type, General selection Premultipliedfirst (ARGB) 2. Reading all pixel data in a picture
unsigned char* needdata = malloc (How large it takes); needdata = Cgbitmapcontextgetdata (contexref);
3. Read the contents of a point
int alpha = needdata[4*n]; int red = needdata[4*n+1]; int green = needdata[4*n+2]; int blue = needdata[4*n+3];
A point is represented by 4 messages 4. Modify the contents of a point
Newdata[4*n ] = Alpha; Newdata[4*n + 1] = red; Newdata[4*n + 2] = green;
5. UIImage data from the new program
Cgimageref cgimage = Cgbitmapcontextcreateimage (newcontext); _imageview.image = [UIImage imagewithcgimage:cgimage];
With a few functions, you'll need to specify a set of rules, get all the pixels, and then generate a new picture from the group element. And this set of rules is the corresponding matrix transformationTwo. Matrix Transformation Note: This part of the principle of the majority, if you can not directly skip the copy code, does not affect the implementation of your function1. What is a matrix if a line vector [X1 X2 .... Xn] represents the coordinates of a point in an n-dimensional space, then the M-point coordinates in an n-dimensional space can be represented as a set of vectors, which is a matrix.
That is, a set of all the dots (x, y) coordinates of our two-dimensional picture is a matrix. Picture and some of the dots 2. Graphical transformations The common operation of our images is to review, rotate, and scale the matrix transformations of the graphs, essentially changing the coordinates of each point.
All graphical points are changed from the old x y coordinate to the new X ' y ' coordinate.
3. Common graphical transformations of the basic two-dimensional transformation can include rotation, scaling, twisting, and moving four, and these geometric operations can be converted to some basic matrix operations: These transformations are linear, but the translation operation is not linear, can not be completed by 2*2 matrix operation. To shift the point (2, 1) in the x direction by 3 units, pan it in the y direction by 4 units. You can do this by first using matrix multiplication and then using matrix addition. Combining these basic operations, mathematicians unify them into a 3*3 matrix, which is stored in the following form: Because the third column of the matrix that represents the affine transformation is always (0,0,1), most of the time the matrix is stored as a 2*3 array.three. Find the law of the corresponding Graph transformation from a picture composed of 4 new verticesMethod 1. Calculate the original coordinates and the four vertex coordinates after the transformation by matrix multiplication We are all known, we can establish a 4-yuan equation by the known data, draw a B C D four position number and then calculate the new graph coordinates. This method is suitable for doing mathematical application problems. Method 2. Find out the law by the linear transformation of x y the pictures we got before are all very well-behaved, a rectangle. Add x relative to your own plot space coordinates, this x coordinates is (7/11 * picture width, 4/8 * picture height) If a graph changes, then the J and I vectors are not previously perpendicular to each other 90 degrees, and then all the points in the coordinates may be shifted. Like I'm going to move the x point to point B. We can think of the above Cartesian coordinate system as a bus, all the points are sitting in the bus, we do not move in the car, and then the bus arrived at the destination, we have been very far away from the city. Like this, the X point is not moving to point B.Focus: So the idea of calculation is that through the old Cartesian coordinate system to obtain the position of each point, and then find the new coordinate system, the point moved up, completed the first picture to achieve the effect ofLet's start looking at the rules, if the new picture turns out this way
Since 4 vertices are known, how long are these 4 edges, and the specific coordinates of the points above each edge we can confirm.
By confirming the two vertices from the top and bottom two edges, we can confirm the linear variation of y on the X coordinate. For example, the linear transformation of the Y-direction linear transformation of the coordinate point in the X-aspect 1/4 position of the previous picture becomes the left figure y '. When the X coordinate is fixed, we only need to pass in the Y value in the previous coordinates, we can get all the coordinate points in the new graph relative to our own width 1/4. In the same way, before 2/4 3/4 and so on all of the vertex direction of the Y-linear transformation law we can get. Then all we have to do is loop through all the downward vertices and get all the Y-direction points at the nth position, so that all the coordinates are drawn. Then this set of coordinates is composed of a new picture. Finally attach the key code//array contains the first left upper right bottom right lower left bottom 4 points in different positions, the picture shows the different
1-(void) Changeimagebypoints: (Nsarray *) pointarray{2UIImage * image = _image;//picture that needs to be transformed globally3 floatwidth =cgimagegetwidth (image. Cgimage);4 floatHeight =cgimagegetheight (image. Cgimage);5 6Cgpoint P0 = [pointarray[0]cgpointvalue];7Cgpoint P1 = [pointarray[1]cgpointvalue];8Cgpoint P2 = [pointarray[2]cgpointvalue];9Cgpoint P3 = [pointarray[3]cgpointvalue];Ten One //Pain calculates a new width and height relative to the absolute 4 vertices of the parent view A floatMinleft =Min ( min (p0.x, p1.x), Min (p2.x, p3.x)); - floatMintop =Min ( min (p0.y, p1.y), Min (p2.y, p3.y)); - floatSHAPW = Kint (max (max (p0.x, p1.x), Max (p2.x, p3.x))-minleft)); the floatShaph = Kint (max (max (P0.Y, P1.y), Max (P2.Y, p3.y))-mintop)); - - //Change point relative to image not Superview -p0.x = p0.x-Minleft; +p1.x = p1.x-Minleft; -p2.x = p2.x-Minleft; +p3.x = p3.x-Minleft; AP0.Y = P0.y-Mintop; atP1.Y = P1.y-Mintop; -P2.Y = P2.y-Mintop; -P3.Y = P3.y-Mintop; - - //Create a Bitmapcontext - if(!_first) { inNeeddata = malloc (kint (width) * Kint (height) *4); -Cgcontextref Imagecontext = Cgbitmapcontextcreate (needdata, width, height,8, Width *4, Cgimagegetcolorspace (image. Cgimage), Cgimagegetalphainfo (image. Cgimage)); toCgcontextdrawimage (Imagecontext, CGRectMake (0,0, width, height), image. Cgimage); +data = malloc (Kint (width) * Kint (height) *4); -data =Cgbitmapcontextgetdata (imagecontext); the_first =YES; * } $ Panax Notoginseng //initializes the new image with the required data -UnsignedChar* Shapedata = malloc (SHAPW * shaph *4); the for(inti =0; I < shaph-1; i + +) { + for(intj =0; J < SHAPW-1; J + +) { A intoffset = (I * SHAPW + j) *4; theShapedata[offset] =255; +Shapedata[offset +1] =255; -Shapedata[offset +2] =255; $Shapedata[offset +3] =255; $ } - } - the //add a corresponding pixel value to data - for(inti =0; I < height-1; i++) {Wuyi for(intj =0; J < Width-1; J + +) { theCgpoint Originpoint =Cgpointmake (J, i); - intOriginoffset = (I * width + j) *4; Wu //calculate the position of each point in the original image in the new diagram - floatXfunc = (float) Originpoint.x/(float) width; About floatYfunc = (float) Originpoint.y/(float) height; $ - floatDelx = (p1.x-p0.x) *Xfunc; - floatdely = (P1.Y-P0.Y) *Xfunc; -Cgpoint Toppoint = cgpointmake (p0.x + delx, P0.y +dely); A +Delx = (p2.x-p3.x) *Xfunc; thedely = (P2.Y-P3.Y) *Xfunc; -Cgpoint bottompoint = cgpointmake (p3.x + delx, P3.y +dely); $ theDelx = (bottompoint.x-toppoint.x) *Yfunc; thedely = (BOTTOMPOINT.Y-TOPPOINT.Y) *Yfunc; the theCgpoint Newpoint = cgpointmake (Toppoint.x + delx, Toppoint.y +dely); - in intNewoffset = ((Kint (NEWPOINT.Y) * SHAPW + kint (newpoint.x))) *4; the the //give Shapeview new value AboutShapedata[newoffset] =Data[originoffset]; theShapedata[newoffset +1] = Data[originoffset +1]; theShapedata[newoffset +2] = Data[originoffset +2]; theShapedata[newoffset +3] = Data[originoffset +3]; + - } the }Bayi //Create a new picture theCgcontextref Newcontext = cgbitmapcontextcreate (Shapedata, SHAPW, Shaph,8, SHAPW *4, Cgimagegetcolorspace (image. Cgimage), Cgimagegetalphainfo (image. Cgimage)); the -Cgimageref Cgimage =cgbitmapcontextcreateimage (newcontext); -_imageview.image = [UIImage imagewithcgimage:cgimage];//This _imageview is affixed to the viewcontroller above the Uiimageview the_imageview.frame =CGRectMake (Minleft, Mintop, SHAPW, shaph); the cgcontextrelease (newcontext); the cgimagerelease (cgimage); the Free (shapedata); -}
IOS Implements picture Customization transformations