| 83 | } |
| 84 | |
| 85 | void SaveBitmapToFile( const char * fname, LVGrayDrawBuf * bmp ) |
| 86 | { |
| 87 | if (!bmp) |
| 88 | return; |
| 89 | LVStreamRef stream = LVOpenFileStream(fname, LVOM_WRITE); |
| 90 | if (!stream) |
| 91 | return; |
| 92 | int rowsize = ((bmp->GetWidth()+1)/2); |
| 93 | int img_size = rowsize * bmp->GetHeight(); |
| 94 | int padding = rowsize - rowsize; |
| 95 | BITMAPFILEHEADER fh; |
| 96 | struct { |
| 97 | BITMAPINFOHEADER hdr; |
| 98 | RGBQUAD colors[16]; |
| 99 | } bmi; |
| 100 | memset(&fh, 0, sizeof(fh)); |
| 101 | memset(&bmi, 0, sizeof(bmi)); |
| 102 | fh.bfType = 0x4D42; |
| 103 | fh.bfSize = sizeof(fh) + sizeof(bmi) + img_size; |
| 104 | fh.bfOffBits = sizeof(fh) + sizeof(bmi); |
| 105 | bmi.hdr.biSize = sizeof(bmi.hdr); |
| 106 | bmi.hdr.biWidth = bmp->GetWidth(); |
| 107 | bmi.hdr.biHeight = bmp->GetHeight(); |
| 108 | bmi.hdr.biPlanes = 1; |
| 109 | bmi.hdr.biBitCount = 4; |
| 110 | bmi.hdr.biCompression = 0; |
| 111 | bmi.hdr.biSizeImage = img_size; |
| 112 | bmi.hdr.biXPelsPerMeter = 0xEC4; |
| 113 | bmi.hdr.biYPelsPerMeter = 0xEC4; |
| 114 | bmi.hdr.biClrUsed = 16; |
| 115 | bmi.hdr.biClrImportant = 16; |
| 116 | static lUInt8 gray[8] = { 0xFF, 0x00, 0x00, 0x00, 0xFF, 0xAA, 0x55, 0x00 }; |
| 117 | lUInt8 * pal = bmp->GetBitsPerPixel()==1?gray+0:gray+4; |
| 118 | for (int i=0; i<4; i++) |
| 119 | { |
| 120 | bmi.colors[i].rgbBlue = pal[i]; |
| 121 | bmi.colors[i].rgbGreen = pal[i]; |
| 122 | bmi.colors[i].rgbRed = pal[i]; |
| 123 | } |
| 124 | stream->Write( &fh, sizeof(fh), NULL ); |
| 125 | stream->Write( &bmi, sizeof(bmi), NULL ); |
| 126 | static const lUInt8 dummy[3] = {0,0,0}; |
| 127 | for (int y=0; y<bmp->GetHeight(); y++) |
| 128 | { |
| 129 | LVArray<lUInt8> row( (bmp->GetWidth()+1)/2, 0 ); |
| 130 | for ( int x=0; x<bmp->GetWidth(); x++) |
| 131 | { |
| 132 | int cl = bmp->GetPixel(x, bmp->GetHeight()-1-y); |
| 133 | //int cl = (src[x/8] >> ((1-(x&3))*2)) & 3; |
| 134 | row[x/2] = row[x/2] | (cl << ((x&1)?0:4)); |
| 135 | } |
| 136 | row[0] = 0x11; |
| 137 | row[1] = 0x11; |
| 138 | row[2] = 0x22; |
| 139 | row[3] = 0x22; |
| 140 | row[4] = 0x33; |
| 141 | row[5] = 0x33; |
| 142 | *stream << row; |