コード例 #1
0
ファイル: bitmap.c プロジェクト: OS2World/DEV-UTIL-MGL
/****************************************************************************
DESCRIPTION:
Copy a portion of a device context as a lightweight memory bitmap.

HEADER:
mgraph.h

PARAMETERS:
dc          - Device context to save from
left        - Left coordinate of bitmap to save
top         - Top coordinate of bitmap to save
right       - Right coordinate of bitmap to save
bottom      - Bottom coordinate of bitmap to save
savePalette - Save palette with bitmap.

RETURNS:
Pointer to allocated bitmap, NULL on error.

REMARKS:
This function copies a portion of a device context as a lightweight memory bitmap.
If this function fails (for instance if out of memory), it will return NULL and you
can get the error code from the MGL_result function. Otherwise this function will
return a pointer to a new lightweight bitmap containing the bitmap data.

Note that the source rectangle for the bitmap to be saved is not clipped to the
current clipping rectangle for the device context, but it is mapped to the current
viewport. If you specify dimensions that are larger than the current device context,
you will get garbage in the bitmap file as a result.

SEE ALSO:
MGL_loadBitmap, MGL_saveBitmapFromDC
****************************************************************************/
bitmap_t * MGLAPI MGL_getBitmapFromDC(
    MGLDC *dc,
    int left,
    int top,
    int right,
    int bottom,
    ibool savePalette)
{
    bitmap_t    *bitmap,bmh;
    long        size;
    int         palSize;

    /* Build the bitmap header and allocate memory for it */
    createBitmapHeader(dc,&left,&top,&right,&bottom,&bmh,&palSize);
    if (!savePalette && (dc->mi.modeFlags & MGL_IS_COLOR_INDEX))
        palSize = 0;
    size = sizeof(bitmap_t) + (long)bmh.bytesPerLine * bmh.height + palSize;
    if (dc->mi.bitsPerPixel >= 15)
        size += sizeof(pixel_format_t);
    if ((bitmap = PM_malloc(size)) == NULL) {
        __MGL_result = grNoMem;
        return NULL;
        }
    *bitmap = bmh;
    size = sizeof(bitmap_t);
    if (dc->mi.modeFlags & MGL_IS_COLOR_INDEX) {
        bitmap->pf = NULL;
        if (dc->mi.bitsPerPixel == 16) {
            bitmap->pf = (pixel_format_t*)((uchar*)bitmap + size);
            size += sizeof(pixel_format_t);
            *bitmap->pf = dc->pf;
            }
        if (savePalette) {
            bitmap->pal = (palette_t*)((uchar*)bitmap + size);
            size += palSize;
            memcpy(bitmap->pal,dc->colorTab,palSize);
            }
        else
            bitmap->pal = NULL;
        }
    else {
        bitmap->pf = (pixel_format_t*)((uchar*)bitmap + size);
        size += sizeof(pixel_format_t);
        *bitmap->pf = dc->pf;
        bitmap->pal = NULL;
        }
    bitmap->surface = (uchar*)bitmap + size;

    /* Now copy the bits from the device context into the bitmap */
    MAKE_HARDWARE_CURRENT(dc,true);
    dc->r.GetBitmapSys(bitmap->surface,bitmap->bytesPerLine,
        left,top,right-left,bottom-top,0,0,GA_REPLACE_MIX);
    RESTORE_HARDWARE(dc,true);
    return bitmap;
}
コード例 #2
0
int main()
{

  
	char const outputFileName[] = "output.bmp";

	unsigned int width = 10000;
	unsigned int height = 10000;

	FILE *outputFile = fopen(outputFileName,"w");

	createBitmapHeader(outputFile, width, height);

	double xmin = 2.1, xmax = -0.6, ymin = -1.35, ymax = 1.35;
	double dx = (xmax - xmin) / width ;
	double dy = (ymax - ymin) / height; 
	
	#pragma omp parallel for
	for (unsigned int i = 0; i < height; i++)
	{
		double jy = ymin + i * dy;
		#pragma omp parallel for
		for (unsigned int j = 0; j < width; j++)
		{
			double jx = xmin + j * dx;

			color_t color = calculate(jx, jy);

			//pragma omp ordered
			fwrite(&color.b, 1, 1, outputFile);
			fwrite(&color.g, 1, 1, outputFile);
			fwrite(&color.r, 1, 1, outputFile);
		}		
	}

	fclose(outputFile);

	//return 0;
}
コード例 #3
0
ファイル: bitmap.c プロジェクト: OS2World/DEV-UTIL-MGL
/****************************************************************************
DESCRIPTION:
Save a portion of a device context to bitmap file on disk.

HEADER:
mgraph.h

PARAMETERS:
dc          - Device context to save
bitmapName  - Name of bitmap file to save
left        - Left coordinate of bitmap to save
top         - Top coordinate of bitmap to save
right       - Right coordinate of bitmap to save
bottom      - Bottom coordinate of bitmap to save

RETURNS:
True on success, false on error.

REMARKS:
This function saves a portion of a device context as a bitmap file to disk. If this
function fails for some reason, it will return false and you can get the error code
from the MGL_result function.

Note that the source rectangle for the bitmap to be saved is not clipped to the
current clipping rectangle for the device context, but it is mapped to the current
viewport. If you specify dimensions that are larger than the current device context,
you will get garbage in the bitmap file as a result.

SEE ALSO:
MGL_loadBitmap,MGL_loadBitmapIntoDC
****************************************************************************/
ibool MGLAPI MGL_saveBitmapFromDC(
    MGLDC *dc,
    const char *bitmapName,
    int left,
    int top,
    int right,
    int bottom)
{
    FILE                *f;
    bitmap_t            bmh;
    winBITMAPFILEHEADER hdr;
    winBITMAPINFOHEADER bmInfo;
    long                size;
    int                 i,palSize;
    uchar               *p;
    M_int32             masks[3];

    /* Attempt to open the file for writing */
    __MGL_result = grOK;
    if ((f = __MGL_fopen(bitmapName,"wb")) == NULL) {
        __MGL_result = grBitmapNotFound;
        return false;
        }

    /* Build the bitmap file header and write to disk */
    createBitmapHeader(dc,&left,&top,&right,&bottom,&bmh,&palSize);
    memset(&hdr,0,sizeof(hdr));
    hdr.bfType[0] = 'B';
    hdr.bfType[1] = 'M';

    /* Build bitmap info header and write to disk */
    memset(&bmInfo,0,sizeof(bmInfo));
    putLELong(bmInfo.biSize,sizeof(winBITMAPINFOHEADER));
    putLELong(bmInfo.biWidth,bmh.width);
    putLELong(bmInfo.biHeight,bmh.height);
    putLELong(bmInfo.biPlanes,1);
    switch (bmh.bitsPerPixel) {
        case 1:
            putLELong(bmInfo.biBitCount,1);
            putLELong(bmInfo.biCompression,winBI_RGB);
            putLELong(bmInfo.biClrUsed,2);
            break;
        case 4:
            putLELong(bmInfo.biBitCount,4);
            putLELong(bmInfo.biCompression,winBI_RGB);
            putLELong(bmInfo.biClrUsed,16);
            break;
        case 8:
            putLELong(bmInfo.biBitCount,8);
            putLELong(bmInfo.biCompression,winBI_RGB);
            putLELong(bmInfo.biClrUsed,256);
            break;
        case 15:
            putLELong(bmInfo.biBitCount,16);
            putLELong(bmInfo.biCompression,winBI_RGB);
            putLELong(bmInfo.biClrUsed,0);
            break;
        case 16:
            putLELong(bmInfo.biBitCount,16);
            putLELong(bmInfo.biClrUsed,0);
            putLELong(bmInfo.biCompression,winBI_BITFIELDS);
            masks[0] = 0x0000F800UL;
            masks[1] = 0x000007E0UL;
            masks[2] = 0x0000001FUL;
            break;
        case 24:
            putLELong(bmInfo.biBitCount,24);
            putLELong(bmInfo.biClrUsed,0);
            if (dc->pf.redPos == 0) {
                /* Bitmap is in the extended 24 bit BGR format */
                putLELong(bmInfo.biCompression,winBI_BITFIELDS);
                masks[0] = 0x0000FFUL;
                masks[1] = 0x00FF00UL;
                masks[2] = 0xFF0000UL;
                }
            else putLELong(bmInfo.biCompression,winBI_RGB);
            break;
        case 32:
            putLELong(bmInfo.biBitCount,32);
            putLELong(bmInfo.biClrUsed,0);
            if (dc->pf.alphaPos != 0) {
                if (dc->pf.redPos != 0)
                    putLELong(bmInfo.biCompression,winBI_RGB);
                else {
                    /* Bitmap is in the extended 32 bit ABGR format */
                    putLELong(bmInfo.biCompression,winBI_BITFIELDS);
                    masks[0] = 0x000000FFUL;
                    masks[1] = 0x0000FF00UL;
                    masks[2] = 0x00FF0000UL;
                    }
                }
            else {
                if (dc->pf.redPos != 8) {
                    putLELong(bmInfo.biCompression,winBI_BITFIELDS);
                    masks[0] = 0xFF000000UL;
                    masks[1] = 0x00FF0000UL;
                    masks[2] = 0x0000FF00UL;
                    }
                else {
                    putLELong(bmInfo.biCompression,winBI_BITFIELDS);
                    masks[0] = 0x0000FF00UL;
                    masks[1] = 0x00FF0000UL;
                    masks[2] = 0xFF000000UL;
                    }
                }
            break;
        }
    size = (long)bmh.bytesPerLine * bmh.height;
    putLELong(bmInfo.biSizeImage,size);
    putLELong(bmInfo.biClrImportant,0);

    /* Write header and palette data to disk */
    if (bmh.bitsPerPixel <= 8)
        size += sizeof(hdr) + sizeof(bmInfo) + palSize;
    else if (getLELong(bmInfo.biCompression) == winBI_BITFIELDS)
        size += sizeof(hdr) + sizeof(bmInfo) + sizeof(masks);
    else
        size += sizeof(hdr) + sizeof(bmInfo);
    putLELong(hdr.bfSize,size);
    putLELong(hdr.bfOffBits,size - getLELong(bmInfo.biSizeImage));
    __MGL_fwrite(&hdr,1,sizeof(hdr),f);
    __MGL_fwrite(&bmInfo,1,sizeof(bmInfo),f);
    if (bmh.bitsPerPixel <= 8)
        __MGL_fwrite(dc->colorTab,1,palSize,f);
    else if (getLELong(bmInfo.biCompression) == winBI_BITFIELDS)
        __MGL_fwrite(masks,1,sizeof(masks),f);

    /* Now write the bits from the device context to disk. Note that we
     * write the data in bottom up DIB format, which is the standard DIB
     * format for files saved to disk
     */
    p = _MGL_buf;
    MAKE_HARDWARE_CURRENT(dc,true);
    for (i = bottom-1; i >= top; i--) {
        dc->r.GetBitmapSys(p,bmh.bytesPerLine,left,i,right-left,1,0,0,GA_REPLACE_MIX);
        __MGL_fwrite(p,1,bmh.bytesPerLine,f);
        }
    RESTORE_HARDWARE(dc,true);
    __MGL_fclose(f);
    return true;
}