// if psFilename == NULL, takes a memory screenshot in DIB format (for copying to clipboard) // bool ScreenShot(LPCSTR psFilename, // else NULL = take memory snapshot (for clipboard) LPCSTR psCopyrightMessage, // /* = NULL */ int iWidth, // /* = <screenwidth> */ int iHeight // /* = <screenheight> */ ) { bool bReturn = false; int iOldPack; glGetIntegerv(GL_PACK_ALIGNMENT,&iOldPack); glPixelStorei(GL_PACK_ALIGNMENT,1); void *pvGLPixels = malloc (iWidth * iHeight * 3); // 3 = R,G,B if (pvGLPixels) { if (psCopyrightMessage) { bool bOldInhibit = gbTextInhibit; gbTextInhibit = false; Text_DisplayFlat(psCopyrightMessage, 0, (iHeight-TEXT_DEPTH)-1,255,255,255); // y-1 = aesthetic only gbTextInhibit = bOldInhibit; } glReadPixels( 0, // x 0, // y (from bottom left) iWidth, // width iHeight, // height GL_RGB, // format GL_UNSIGNED_BYTE, // type pvGLPixels // buffer ptr ); // save area is valid size... // if (BMP_Open(psFilename, iWidth, iHeight)) { for (int y=0; y<iHeight; y++) { LPGLRGBBYTES lpGLRGBBytes = (LPGLRGBBYTES) pvGLPixels; lpGLRGBBytes+= y * iWidth; for (int x=0; x<iWidth; x++, lpGLRGBBytes++) { BMP_WritePixel(lpGLRGBBytes->r,lpGLRGBBytes->g,lpGLRGBBytes->b); } BMP_WriteLinePadding(iWidth); // arg is # pixels per row } BMP_Close(psFilename,false); // false = bFlipFinal bReturn = true; } free(pvGLPixels); pvGLPixels = NULL; // yeah...yeah } glPixelStorei(GL_PACK_ALIGNMENT,iOldPack); return bReturn; }
//Read the image BMPImage* BMP_Read(char *file){ BMPImage * image = BMP_CreateBlank(); BMPHeader *file_header = image->file_header; BMPImageHeader *image_header = image->image_header; unsigned char *image_data = NULL; FILE *bmp_file = NULL; int file_open; bmp_file = BMP_InputOpen(file); if(!bmp_file){ printf("ERROR: Can't open file %s\n",file); return NULL; } int header_read; header_read = BMP_Read_FileHeader(bmp_file,file_header); if(header_read){ printf("ERROR: Can't read the image header\n"); if(file_header) free(file_header); if(image_header) free(image_header); return NULL; } int img_header_read; img_header_read = BMP_Read_ImageHeader(bmp_file,image_header); if(img_header_read){ printf("ERROR: Can't read the data header\n"); if(file_header) free(file_header); if(image_header) free(image_header); return NULL; } //Allocate memory for the image pixels image_data = (unsigned char *)malloc(image_header->Height * image_header->Width * 3); if(!image_data){ printf("ERROR: Can't allocate memory for the image\n"); if(file_header) free(file_header); if(image_header) free(image_header); return NULL; } int img_data_read; img_data_read = BMP_Read_Image(bmp_file,image_data,(image_header->Height*image_header->Width*3)); if(img_data_read){ printf("ERROR: Can't read the image data\n"); if(file_header) free(file_header); if(image_header) free(image_header); if(image_data) free(image_data); return NULL; } int row = (int)(image_header->Height); int col = (int)(image_header->Width); image->data = image_data; unsigned char *tmp = image_data; //Copy the image data into the storage arrays /*int i, j; unsigned char r_temp, b_temp, g_temp; for(i=0; i < row; i++){ for(j=0; j < col; j++){ b_temp = *tmp++; g_temp = *tmp++; r_temp = *tmp++; // printf("R %d G %d B %d\n",r_temp,b_temp,g_temp); image->R[(row-1-i)][j] = r_temp; image->G[(row-1-i)][j] = g_temp; image->B[(row-1-i)][j] = b_temp; } }*/ BMP_Close(bmp_file); return image; }
//Read the image BMPImage* BMP_Read(char *file) { BMPImage * image = BMP_CreateBlank(); BMPHeader * file_header = image->file_header; BMPImageHeader * image_header = image->image_header; unsigned char * image_data = NULL; int i,j; FILE *bmp_file = NULL; int file_open; bmp_file = BMP_InputOpen(file); if(!bmp_file) { printf("ERROR: Can't open file %s\n",file); return NULL; } int header_read; header_read = BMP_Read_FileHeader(bmp_file,file_header); if(header_read) { printf("ERROR: Can't read the image header\n"); if(file_header) free(file_header); if(image_header) free(image_header); return NULL; } int img_header_read; img_header_read = BMP_Read_ImageHeader(bmp_file,image_header); if(img_header_read) { printf("ERROR: Can't read the data header\n"); if(file_header) free(file_header); if(image_header) free(image_header); return NULL; } //Allocate memory for the image pixels image_data = (unsigned char *)malloc(image_header->Height * image_header->Width * 3); if(!image_data) { printf("ERROR: Can't allocate memory for the image\n"); if(file_header) free(file_header); if(image_header) free(image_header); return NULL; } int img_data_read; img_data_read = BMP_Read_Image(bmp_file,image_data,(image_header->Height*image_header->Width*3)); if(img_data_read) { printf("ERROR: Can't read the image data\n"); if(file_header) free(file_header); if(image_header) free(image_header); if(image_data) free(image_data); return NULL; } //Allocate for R,G,B channel float * R = (float *)calloc(image_header->Height * image_header->Width,8); float * G = (float *)calloc(image_header->Height * image_header->Width,8); float * B = (float *)calloc(image_header->Height * image_header->Width,8); if(!R||!G||!B) { printf("ERROR:Can't allocate for RGB channels\n"); if(file_header) free(file_header); if(image_header) free(image_header); if(image_data) free(image_data); if(R) free(R); if(G) free(G); if(B) free(B); return NULL; } //assert((void*)R != (void*)image_data); unsigned const char * ptr = image_data; int count = 0,Width_Stride = (4 - image_header->Width *3%4)%4; for(i = 0;i < image_header->Height;++i) { for(j = 0;j < image_header->Width;++j) { R[count] = ptr[0]; G[count] = ptr[1]; B[count++] = ptr[2]; ptr += 3; } ptr += Width_Stride; } image->Height = (int)(image_header->Height); image->Width = (int)(image_header->Width); image->data = image_data; image->R = R; image->G = G; image->B = B; // close bmp file BMP_Close(bmp_file); return image; }