In C language, the floating point numbers from 0 to 1000 are forced pointer type conversion to generate an image.

Source: Internet
Author: User

In C language, the floating point numbers from 0 to 1000 are forced pointer type conversion to generate an image.
People who have worked on computer images know that each pixel in an image is usually an integer. It can be divided into four unsigned char types to represent its RGBA components. An image can be seen as a two-dimensional integer array. Here I will generate a float array with an array size of 1000000, which is just 1000*1000. The floating point numbers in the array range from 0 to 1000.0, and are arranged in an equal-difference array, the difference between two adjacent numbers is 0.001. Then, each floating point number is forcibly converted into an integer or three unsigned char types to determine the RGB three-channel component of the pixel and see what the generated image looks like. I wrote an article a few days ago to exchange two arbitrary types of variables using the exclusive or operation in C language, attracting some people's question, saying, "The mandatory type conversion of pointers has certain limitations, it is not how you want to convert it, but the result may be wrong ". I can deny my code if I don't need it. To laugh at his ignorance, I specifically wrote this algorithm for generating images using forced pointer type conversion. First run the C code: copy the Code # include <iostream> # include <cmath> # include <cstdlib> # define DIM 1000 void pixel_write (int, int); FILE * fp; int main () {fp = fopen ("image. ppm "," wb "); if (! Fp) {return-1;} fprintf (fp, "P6 \ n % d \ n255 \ n", DIM, DIM); for (int j = 0; j <DIM; j ++) {for (int I = 0; I <DIM; I ++) {pixel_write (I, j) ;}} fclose (fp ); return 0;} void pixel_write (int I, int j) {static unsigned char color [3]; float t = j + I * 0.001f; memcpy (color, & t, 3); fwrite (color, 1, 3, fp); // In fact, the simpler method is: // fwrite (& t, 1, 3, fp );} after the code is copied and run, an image in the PPM format is generated, as shown in figure: the image is a bit of fragment. The image in PPM format is not very common. It is a very simple image format. I can view it in the Why mathematical image generation tool I wrote. Of course, I also write the image generation algorithm into this software. The relevant code is as follows: copy the Code # ifndef _ PixelFloatConvertInt_H _ # define _ PixelFloatConvertInt_H _ // alias # include "IPixelEquation. h "// define class CPixelFloatConvertInt: public IPixelEquation {public: CPixelFloatConvertInt () {m_width = 1000; m_height = 1000;} const char * GetName () const {return "Float Convert Int";} unsigned int CalculatePixel (unsigned int x, unsigned int y) {float t = y + x * 0.001f; unsigned int rst = * (unsigned int *) & t; rst | = 0xff000000; return rst ;}}; // signature # endif

Related Article

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.