[C/C ++] The rgba array generates the ICO file in Windows

Source: Internet
Author: User

In widows, only the. ICO format is the program icon, which is a source image format from BMP. The color in ICO can be expressed by color table or by using the rgba array.

For the ICO file format, see the official documentation:

Http://msdn.microsoft.com/en-us/library/ms997538.aspx

Http://msdn.microsoft.com/en-us/library/windows/desktop/dd183376%28v=vs.85%29.aspx

For other documents about ICO files, see:

Detailed principle of Introduction: http://blogs.msdn.com/ B /oldnewthing/archive/2010/10/18/10077133.aspx

ICO Description: http://www.daubnet.com/en/file-format-ico

Programming Tips & tricks: http://tipsandtricks.runicsoft.com/Cpp/BitmapTutorial.html

BMP and ICO format description: http://www.digicamsoft.com/bmp/bmp.html

ICO file implementation:

Iconimageplugin in Python Pil: http://code.google.com/p/casadebender/source/browse/python/PIL/Win32IconImagePlugin.py

Construct ICO from rgba array-a python implementation: http://fayaa.com/code/view/6094/

Png2ico: http://www.winterdrache.de/freeware/png2ico/

The following is a C language that generates an ico file from rgba and generates a red icon file-test. ICO.

#define _CRT_SECURE_NO_WARNINGS#include <cstdio>#include <cstdlib>#include <cstring>#include <cassert>void Fwrite(FILE * f, char * data, int byte){if (1 != fwrite(data, byte, 1, f)) {perror("fwrite error");exit(1);}}void WriteByte(FILE* f, unsigned int val, int byte){    char data[4];    assert(byte <= 4);    memcpy((void *)data, (void *)&val, byte);    Fwrite(f, data, byte);};void generate_ico_file(const char * filename, char * body,         int width, int height, int has_alpha){    int x, y;    int index = 0;    int Size = 0;    int offset= 6 + 1 * 16;    int bpp = 32;    FILE* outfile=fopen(filename,"wb");    if (outfile==NULL) {        perror("fopen error");         exit(1);    };    WriteByte(outfile,0, 2);//idReserved    WriteByte(outfile,1, 2);//idType    WriteByte(outfile,1, 2);//idCount     WriteByte(outfile,width, 1);//bWidth    WriteByte(outfile,height, 1);//bHeight    WriteByte(outfile,0, 1);//bColorCount    WriteByte(outfile,0, 1);//bReserved    WriteByte(outfile,1, 2);//wPlanes    WriteByte(outfile,bpp, 2);//wBitCount    Size = 40 + height * ((width + 31) / 32 * 32 / 8 + width * 3);//Note 4 bytes alignment    if (bpp == 32)        Size += height * width;    WriteByte(outfile,Size, 4);//dwBytesInRes    WriteByte(outfile,offset, 4);//dwImageOffset    WriteByte(outfile,40, 4);//biSize    WriteByte(outfile,width, 4);//biWidth    WriteByte(outfile,2 * height, 4);//biHeight     WriteByte(outfile,1, 2);//biPlanes    WriteByte(outfile,bpp, 2);//biBitCount    WriteByte(outfile,0, 4);//biCompression    WriteByte(outfile,0, 4);//biSizeImage    WriteByte(outfile,0, 4);//biXPelsPerMeter       WriteByte(outfile,0, 4);//biYPelsPerMeter    WriteByte(outfile,0, 4);//biClrUsed     WriteByte(outfile,0, 4);//biClrImportant    // XOR mask    for (y= height - 1; y >= 0; --y) {         for (x = 0; x < width; ++x) {            index = (y * width + x) * 4;            WriteByte(outfile, body[index], 1);        //Blue             WriteByte(outfile, body[index + 1], 1);    //Green            WriteByte(outfile, body[index + 2], 1);    //Red            WriteByte(outfile, has_alpha ? body[index + 3] : 255, 1); //Alpha        }    }    // AND mask    for (y = 0; y < (height * ((width + 31) / 32 * 32 / 8)); ++y) {         WriteByte(outfile, 0, 1);    }    fclose(outfile);}#define WIDTH   72#define HEIGHT  72int main(){    int image[WIDTH * HEIGHT];    int i, j;    for (i = 0; i < HEIGHT; ++i) {        for (j = 0; j < WIDTH; ++j) {           image[i * WIDTH + j]  = 0xFFFF0000;  // pure red icon, for test        }    }    generate_ico_file("test.ico", (char *)image, WIDTH, HEIGHT, 1);    return 0;}

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.