void writeIconDirEntry(ByteStream& stream, int width, int height, int offset) { stream.put(width == 256 ? 0 : width); //Specifies image width in pixels. Can be any number between 0 and 255. Value 0 means image width is 256 pixels. stream.put(height == 256 ? 0 : height); //Specifies image height in pixels. Can be any number between 0 and 255. Value 0 means image height is 256 pixels. stream.put(0); //Specifies number of colors in the color palette. Should be 0 if the image does not use a color palette. stream.put(0); //Reserved. Should be 0.[Notes 2] stream.write(convertShortToByteArrayLE((short)1)); //Specifies color planes. Should be 0 or 1.[Notes 3] stream.write(convertShortToByteArrayLE((short)32)); //Specifies bits per pixel. [Notes 4] stream.write(convertIntToByteArrayLE(width * height * 4 + 40)); //Specifies the size of the image's data in bytes stream.write(convertIntToByteArrayLE(offset)); //Specifies the offset of BMP or PNG data from the beginning of the ICO/CUR file }
int stream_writefn(struct iw_context *ctx, struct iw_iodescr *iodescr, const void *buf, size_t nbytes) { //fwrite(buf, 1, nbytes, (FILE*)iodescr->fp); ByteStream* stream = (ByteStream*)iodescr->fp; for (size_t i = 0; i < nbytes; ++i) { stream->put(((byte*)buf)[i]); } return 1; }
void writeBMP(ByteStream& stream, iw_image* image) { writeBMPHeader(stream, image->width, image->height); for (int y = image->height - 1; y >= 0; --y) { for (int x = 0; x < image->width; ++x) { //stream.put(image->b(x, y)); //stream.put(image->g(x, y)); //stream.put(image->r(x, y)); //stream.put(image->a(x, y)); //stream.write(convertIntToByteArrayLE(image->argb(x, y))); stream.put(image->pixels[y * image->bpr + x * 4 + 2]); stream.put(image->pixels[y * image->bpr + x * 4 + 1]); stream.put(image->pixels[y * image->bpr + x * 4 + 0]); stream.put(image->pixels[y * image->bpr + x * 4 + 3]); } } }
void macIcon(iw_context* context, const char* filename) { //16x16 //32x32 //128x128 //256x256 //512x512 //1024x1024 ByteStream stream; iw_iodescr writedescr; memset(&writedescr, 0, sizeof(struct iw_iodescr)); writedescr.write_fn = stream_writefn; writedescr.seek_fn = stream_seekfn; writedescr.fp = &stream; stream.put('i'); stream.put('c'); stream.put('n'); stream.put('s'); stream.put('-'); stream.put('-'); stream.put('-'); stream.put('-'); stream.put('i'); stream.put('c'); stream.put('0'); stream.put('8'); stream.put('-'); stream.put('-'); stream.put('-'); stream.put('-'); scale(context, 256, 256); iw_write_file_by_fmt(context, &writedescr, IW_FORMAT_PNG); int icon08size = static_cast<int>(stream.size() - 8); stream.put('i'); stream.put('c'); stream.put('0'); stream.put('9'); stream.put('-'); stream.put('-'); stream.put('-'); stream.put('-'); scale(context, 512, 512); iw_write_file_by_fmt(context, &writedescr, IW_FORMAT_PNG); int icon09size = static_cast<int>(stream.size() - icon08size - 8); stream.put('i'); stream.put('c'); stream.put('1'); stream.put('0'); stream.put('-'); stream.put('-'); stream.put('-'); stream.put('-'); scale(context, 1024, 1024); iw_write_file_by_fmt(context, &writedescr, IW_FORMAT_PNG); int icon10size = static_cast<int>(stream.size() - icon09size - icon08size - 8); std::vector<byte> size = convertIntToByteArray(static_cast<int>(stream.size())); for (int i = 0; i < 4; ++i) stream.set( 4 + i, size[i]); size = convertIntToByteArray(icon08size); for (int i = 0; i < 4; ++i) stream.set(12 + i, size[i]); size = convertIntToByteArray(icon09size); for (int i = 0; i < 4; ++i) stream.set(icon08size + 8 + 4 + i, size[i]); size = convertIntToByteArray(icon10size); for (int i = 0; i < 4; ++i) stream.set(icon08size + icon09size + 8 + 4 + i, size[i]); stream.save(filename); }
void writeIcoHeader(ByteStream& stream) { stream.put(0); stream.put(0); //Reserved. Must always be 0. stream.write(convertShortToByteArrayLE((short)1)); //Specifies image type: 1 for icon (.ICO) image, 2 for cursor (.CUR) image. Other values are invalid. stream.write(convertShortToByteArrayLE((short)4)); //Specifies number of images in the file. }
void put(ByteStream & target, const int64 & source) { target.put(&source, 8); }
void put(ByteStream & target, const std::string & source) { target.put(source.c_str(), source.size()+1); }
void put(ByteStream& target, const std::string& source) { const unsigned int size = source.size(); put(target, size); target.put(source.data(), size * sizeof (char)); }
void Ball::exportToMacIcon(Path filename, Path directory) { //16x16 //32x32 //128x128 //256x256 //512x512 //1024x1024 ByteStream stream; stream.put('i'); stream.put('c'); stream.put('n'); stream.put('s'); stream.put('-'); stream.put('-'); stream.put('-'); stream.put('-'); stream.put('i'); stream.put('c'); stream.put('0'); stream.put('8'); stream.put('-'); stream.put('-'); stream.put('-'); stream.put('-'); scale(256, 256, transparent, directory)->save(stream); int icon08size = static_cast<int>(stream.size() - 8); stream.put('i'); stream.put('c'); stream.put('0'); stream.put('9'); stream.put('-'); stream.put('-'); stream.put('-'); stream.put('-'); scale(512, 512, transparent, directory)->save(stream); int icon09size = static_cast<int>(stream.size() - icon08size - 8); stream.put('i'); stream.put('c'); stream.put('1'); stream.put('0'); stream.put('-'); stream.put('-'); stream.put('-'); stream.put('-'); scale(1024, 1024, transparent, directory)->save(stream); int icon10size = static_cast<int>(stream.size() - icon09size - icon08size - 8); std::vector<byte> size = convertIntToByteArray(static_cast<int>(stream.size())); for (int i = 0; i < 4; ++i) stream.set( 4 + i, size[i]); size = convertIntToByteArray(icon08size); for (int i = 0; i < 4; ++i) stream.set(12 + i, size[i]); size = convertIntToByteArray(icon09size); for (int i = 0; i < 4; ++i) stream.set(icon08size + 8 + 4 + i, size[i]); size = convertIntToByteArray(icon10size); for (int i = 0; i < 4; ++i) stream.set(icon08size + icon09size + 8 + 4 + i, size[i]); stream.save(filename); }