People who have worked on computer images know that each pixel in the image is usually an integer number that can be divided into 4 unsigned char types to represent its RGBA four components. An image can be viewed as a two-dimensional integer array. Here I will generate a float array with an array size of 1000000, just 1000*1000, the number of floating-point numbers within the array ranges from 0 to 1000.0, which are arranged in equal margin, and the difference between the two adjacent numbers is 0.001. Each of its floating-point numbers is then coerced into an integer or three unsigned char to determine the RGB three-channel component of the pixel, and see what the resulting image looks like.
A few days ago wrote an article is the use of XOR in the C language to exchange two arbitrary types of variables, draw someone's question, say what "pointer coercion type conversion has a certain limit, not how you want to turn on how to turn, the result may be wrong". To negate my code with this kind of bogus words. To mock his ignorance, I purposely wrote this algorithm of generating an image with a forced pointer type conversion.
First the C code:
#include <iostream>#include<cmath>#include<cstdlib>#defineDIM 1000voidPixel_write (int,int); FILE*FP;intMain () {FP= fopen ("image.ppm","WB"); if(!FP) { return-1; } fprintf (FP,"p6\n%d%d\n255\n", Dim, dim); for(intj=0; j<dim;j++) { for(intI=0; i<dim;i++) {pixel_write (i,j); }} fclose (FP); return 0;}voidPixel_write (intIintj) { StaticUnsignedCharcolor[3]; floatT = j + i*0.001f; memcpy (color,&t,3); Fwrite (color,1,3, FP);
In fact, the more simple way to explode is
Fwrite (&t, 1, 3, FP);
}
When the code runs, it generates an image in PPM format, as shown in:
The image is somewhat fractal feeling. The PPM format image is less common and it is a very simple image format. I wrote the software why the mathematical image generation tool can be viewed, of course, I will also write the image generation algorithm written in this software, the relevant code is as follows:
#ifndef _pixelfloatconvertint_h_#define_pixelfloatconvertint_h_// --------------------------------------------------------------------------------------#include"IPixelEquation.h"// --------------------------------------------------------------------------------------classCpixelfloatconvertint: Publicipixelequation{ Public: Cpixelfloatconvertint () {m_width= +; M_height= +; } Const Char* GetName ()Const { return "Float Convert Int"; } unsignedintCalculatepixel (unsignedintX, unsignedinty) {floatt = y + x*0.001f; unsignedintRST = * (unsignedint*) &T; RST|=0xff000000; returnrst; }};// --------------------------------------------------------------------------------------#endif
Use the Why Math image generation tool to see the three components of the image's red and green blue:
R-Channel diagram:
G-Channel diagram:
B-Channel diagram:
The code changes slightly, and the fractal effect is more obvious:
int int int y) { float t = y/4+ x*0.001f; int int*) &t; 0xff000000 ; return rst; }
C language generates an image from 0 to 1000 floating-point numbers in a forced type conversion manner