Programming Images

William Hemsworth 0 Tallied Votes 1K Views Share

Heres a snippet that allows you to make your own images by using a mathematical formula. It gives you fast access to each pixel in the bitmap, and then saves the image as a .bmp once its made. You can be creative like this and make images which would be impossible to make using applications such as photoshop, here are some examples of some I have made using this technique.

One
Two
Three

The trick to making these, is to just put in the craziest formulas you can think of, you don't even need to know what the output should look like, just experiment, and speed isn't a problem, so it can be as long as you want it :icon_cheesygrin: !

Heres a one more :)

r = static_cast<ubyte>( (cos( sin (px) / py ) * (px + py)) * 127 + 127 );
g = static_cast<ubyte>( cos( (double)r ) * 127 + 127 );
b = static_cast<ubyte>( ~((r + g) / 2) );
// Will create a file named "image.bmp" in the project folder

#include <windows.h>
#include <iostream>
#include <cmath>

typedef unsigned long ulong;
typedef unsigned short ushort;
typedef unsigned char ubyte;

struct Pixel {
   union {
      ulong rgb;
      struct {
         // r, g, b share same memory as rgb
         unsigned char b, g, r;
      };
   };

   Pixel() {
      // Automatically set pixel black
      rgb = 0;
   }

   // Init pixel
   Pixel(byte _r, byte _g, byte _b) {
      r = _r;
      g = _g;
      b = _b;
   }

   inline operator ulong() {
      return rgb;
   }

   // Set pixel
   inline Pixel &operator =(ulong _RGB) {
      rgb = _RGB;
      return *this;
   }
};

template<short _Width, short _Height>
struct Bitmap {
   // Pixels
   Pixel *pixels;

   // Dimensions
   short width;
   short height;

   // Init
   Bitmap() {
      width = _Width;
      height = _Height;

      // Allocate Pixels
      pixels = new Pixel[width * height];
   }

   // Destroy
   ~Bitmap() {
      // Free all pixels
      delete[] pixels;
   }

   inline Pixel *operator [](short y) {
      // Returns pointer to row of pixels
      return &pixels[(height - ++y) * width];
   }

   inline Pixel &operator()(short x, short y) {
      // Returns reference to pixel
      return pixels[(height - ++y) * width + x];
   }

   void save(const char *_FileName) {
      // Function to save as bitmap (no compression)
      BITMAPINFOHEADER bmpInfoHeader = {0};

      // Properties
      bmpInfoHeader.biSize = sizeof(BITMAPINFOHEADER);
      bmpInfoHeader.biBitCount = sizeof(Pixel) * 8;
      bmpInfoHeader.biClrImportant = 0;
      bmpInfoHeader.biClrUsed = 0;
      bmpInfoHeader.biCompression = BI_RGB;
      bmpInfoHeader.biHeight = height;
      bmpInfoHeader.biWidth = width;
      bmpInfoHeader.biPlanes = 1;
      bmpInfoHeader.biSizeImage = width * height * sizeof(Pixel);

      BITMAPFILEHEADER bfh = {0};
      bfh.bfType = 0x4D42;
      bfh.bfOffBits = sizeof(BITMAPINFOHEADER) + sizeof(BITMAPFILEHEADER);
      bfh.bfSize = bfh.bfOffBits + bmpInfoHeader.biSizeImage;

      // Create File
      HANDLE hFile = CreateFile( _FileName, GENERIC_WRITE, 0, NULL,
                        CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

      // Failed
      if (!hFile) {
         return;
      }

      DWORD dwWritten = 0;
      WriteFile( hFile, &bfh, sizeof(bfh), &dwWritten , NULL );
      WriteFile( hFile, &bmpInfoHeader, sizeof(bmpInfoHeader), &dwWritten, NULL );
      WriteFile( hFile, &pixels[0], bmpInfoHeader.biSizeImage, &dwWritten, NULL );

      CloseHandle( hFile );
   }

};

int main() {
   std::cout << "Generating Bitmap...\n";

   // Make Bitmap Object
   Bitmap<500, 500> bmp;

   // Temp variables for pixel colour
   union {
      ulong rgb;

      // r, g, b and rgb all point to same memory
      struct {
         ubyte b, g, r;
      };
   };

   double px, // (0.0 when x = 0, 1.0 when x = bmp.width)
          py; // (0.0 when y = 0, 1.0 when y = bmp.height)

   // By using type double for x,y, compiler will give no warnings
   double dx, // Double value for x-loc
          dy; // Double value for y-loc

   // Same for bitmap sizes
   double dw = static_cast<double>( bmp.width ),  // Double value for width
          dh = static_cast<double>( bmp.height ); // Double value for height

   // Draw to bitmap
   for (ushort x = 0; x < bmp.width; ++x) {
      for (ushort y = 0; y < bmp.height; ++y) {
         dx = static_cast<double>( x );
         dy = static_cast<double>( y );
         px = dx / dw;
         py = dy / dh;

         r = static_cast<ubyte>( cos( px * 50.0 ) * 127 + 127 );
         g = static_cast<ubyte>( cos( py * 50.0 ) * 127 + 127 );
         b = static_cast<ubyte>( ~((r + g) / 2) );

         bmp[y][x] = rgb;
      }
   }

   // Save to file
   bmp.save("image.bmp");

   std::cout << "\nSaved BMP \"image.bmp\".";

   // Wait for a second
   Sleep(1000);
   return 0;
}
William Hemsworth 1,339 Posting Virtuoso

Another nice one :)
http://img186.imageshack.us/img186/1720/imagelu1.png

Heres the formula:

r = static_cast<ubyte>( ((cos(px+0.3) / sin(py+0.3)) * 10) * 127 + 127 );
g = static_cast<ubyte>( ~r );
b = static_cast<ubyte>( sin((r + g) / 2.0) * 127 + 127 );
Alex Edwards 321 Posting Shark

Whoo! Gnarly colors @_@

Very Cool! =)

Prabakar 77 Posting Whiz

Very nice:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.