Changeset 2393


Ignore:
Timestamp:
Jun 14, 2008, 3:02:23 PM (15 years ago)
Author:
Sam Hocevar
Message:
  • BMP write support for Win32.
  • Include a pre-compiled .exe file.
Location:
www/img2oric
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • www/img2oric/img2oric.c

    r2392 r2393  
    406406{
    407407#if defined _WIN32
     408    PBITMAPINFO pbinfo;
     409    BITMAPINFO binfo;
     410    BITMAPFILEHEADER bfheader;
     411    ULONG bisize;
     412    HANDLE hfile;
    408413    HBITMAP tmp;
    409414    HDC hdc;
     415    DWORD ret;
    410416#else
    411417    SDL_Surface *tmp, *surface;
     418#endif
     419    FILE *f;
    412420    uint8_t *pixels;
    413 #endif
    414     FILE *f;
    415421    int *src, *dst, *srcl, *dstl;
    416422    int stride, x, y, depth, c;
     
    479485        uint8_t bg = 0, fg = 7;
    480486
    481         fprintf(stderr, "\rProcessing... %i%%", (y * 100 + 50) / HEIGHT);
     487        fprintf(stderr, "\rProcessing... %i%%", (y * 100 + 99) / HEIGHT);
    482488
    483489        for(x = 0; x < WIDTH; x += 6)
     
    517523    }
    518524
     525    fclose(f);
     526
    519527    fprintf(stderr, " done.\n");
    520528
     
    529537            SetPixel(hdc, x, y, RGB(r, g, b));
    530538        }
    531     /* FIXME: how do I save the image now? */
     539
     540    binfo.bmiHeader.biSize = sizeof(binfo.bmiHeader);
     541    binfo.bmiHeader.biBitCount = 0;
     542    GetDIBits(hdc, tmp, 0, 0, NULL, &binfo, DIB_RGB_COLORS);
     543
     544    switch(binfo.bmiHeader.biBitCount)
     545    {
     546    case 24:
     547        bisize = sizeof(BITMAPINFOHEADER);
     548        break;
     549    case 16:
     550    case 32:
     551        bisize = sizeof(BITMAPINFOHEADER) + sizeof(DWORD) * 3;
     552        break;
     553    default:
     554        bisize = sizeof(BITMAPINFOHEADER)
     555                    + sizeof(RGBQUAD) * (1 << binfo.bmiHeader.biBitCount);
     556        break;
     557    }
     558
     559    pbinfo = (PBITMAPINFO)GlobalAlloc(GMEM_FIXED | GMEM_ZEROINIT, bisize);
     560    memcpy(pbinfo, &binfo, sizeof(BITMAPINFOHEADER));
     561
     562    bfheader.bfType = 0x4D42; /* "BM" */
     563    bfheader.bfSize = sizeof(BITMAPFILEHEADER)
     564                         + bisize + pbinfo->bmiHeader.biSizeImage;
     565    bfheader.bfReserved1 = 0;
     566    bfheader.bfReserved2 = 0;
     567    bfheader.bfOffBits = sizeof(BITMAPFILEHEADER) + bisize;
     568
     569    pixels = GlobalAlloc(GMEM_FIXED | GMEM_ZEROINIT,
     570                         binfo.bmiHeader.biSizeImage);
     571    GetDIBits(hdc, tmp, 0, pbinfo->bmiHeader.biHeight,
     572              pixels, pbinfo, DIB_RGB_COLORS);
     573
     574    hfile = CreateFile(BMPFILE, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
     575                       FILE_ATTRIBUTE_ARCHIVE, NULL);
     576    WriteFile(hfile, &bfheader, sizeof(BITMAPFILEHEADER), &ret, NULL);
     577    WriteFile(hfile, pbinfo, bisize, &ret, NULL);
     578    WriteFile(hfile, pixels, pbinfo->bmiHeader.biSizeImage, &ret, NULL);
     579    CloseHandle(hfile);
     580
     581    GlobalFree(pbinfo);
     582    GlobalFree(pixels);
    532583#else
    533584    for(y = 0; y < HEIGHT; y++)
     
    539590#endif
    540591
    541     fclose(f);
    542 
    543592    return 0;
    544593}
Note: See TracChangeset for help on using the changeset viewer.