/* * Copyright 1993-2015 NVIDIA Corporation. All rights reserved. * * Please refer to the NVIDIA end user license agreement (EULA) associated * with this source code for terms and conditions that govern your use of * this software. Any use, reproduction, disclosure, or distribution of * this software and related documentation outside the terms of the EULA * is strictly prohibited. * */ /** ************************************************************************** * \file BmpUtil.cpp * \brief Contains basic image operations implementation. * * This file contains implementation of basic bitmap loading, saving, * conversions to different representations and memory management routines. */ #include "Common.h" #include "BmpUtil.h" #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64) #pragma warning( disable : 4996 ) // disable deprecated warning #endif /** ************************************************************************** * The routine clamps the input value to integer byte range [0, 255] * * \param x [IN] - Input value * * \return Pointer to the created plane */ int clamp_0_255(int x) { return (x < 0) ? 0 : ((x > 255) ? 255 : x); } /** ************************************************************************** * Float round to nearest value * * \param num [IN] - Float value to round * * \return The closest to the input float integer value */ float round_f(float num) { float NumAbs = fabs(num); int NumAbsI = (int)(NumAbs + 0.5f); float sign = num > 0 ? 1.0f : -1.0f; return sign * NumAbsI; } /** ************************************************************************** * Memory allocator, returns aligned format frame with 8bpp pixels. * * \param width [IN] - Width of image buffer to be allocated * \param height [IN] - Height of image buffer to be allocated * \param pStepBytes [OUT] - Step between two sequential rows * * \return Pointer to the created plane */ byte *MallocPlaneByte(int width, int height, int *pStepBytes) { byte *ptr; *pStepBytes = ((int)ceil(width/16.0f))*16; //#ifdef __ALLOW_ALIGNED_MEMORY_MANAGEMENT // ptr = (byte *)_aligned_malloc(*pStepBytes * height, 16); //#else ptr = (byte *)malloc(*pStepBytes * height); //#endif return ptr; } /** ************************************************************************** * Memory allocator, returns aligned format frame with 16bpp float pixels. * * \param width [IN] - Width of image buffer to be allocated * \param height [IN] - Height of image buffer to be allocated * \param pStepBytes [OUT] - Step between two sequential rows * * \return Pointer to the created plane */ short *MallocPlaneShort(int width, int height, int *pStepBytes) { short *ptr; *pStepBytes = ((int)ceil((width*sizeof(short))/16.0f))*16; //#ifdef __ALLOW_ALIGNED_MEMORY_MANAGEMENT // ptr = (float *)_aligned_malloc(*pStepBytes * height, 16); //#else ptr = (short *)malloc(*pStepBytes * height); //#endif *pStepBytes = *pStepBytes / sizeof(short); return ptr; } /** ************************************************************************** * Memory allocator, returns aligned format frame with 32bpp float pixels. * * \param width [IN] - Width of image buffer to be allocated * \param height [IN] - Height of image buffer to be allocated * \param pStepBytes [OUT] - Step between two sequential rows * * \return Pointer to the created plane */ float *MallocPlaneFloat(int width, int height, int *pStepBytes) { float *ptr; *pStepBytes = ((int)ceil((width*sizeof(float))/16.0f))*16; //#ifdef __ALLOW_ALIGNED_MEMORY_MANAGEMENT // ptr = (float *)_aligned_malloc(*pStepBytes * height, 16); //#else ptr = (float *)malloc(*pStepBytes * height); //#endif *pStepBytes = *pStepBytes / sizeof(float); return ptr; } /** ************************************************************************** * Copies byte plane to float plane * * \param ImgSrc [IN] - Source byte plane * \param StrideB [IN] - Source plane stride * \param ImgDst [OUT] - Destination float plane * \param StrideF [IN] - Destination plane stride * \param Size [IN] - Size of area to copy * * \return None */ void CopyByte2Float(byte *ImgSrc, int StrideB, float *ImgDst, int StrideF, ROI Size) { for (int i=0; i=0; i--) { for (int j=0; j> 20 ; Img[i*Stride+j] = (byte)clamp_0_255(val); } } fclose(fh); return; } /** ************************************************************************** * This function performs dumping of bitmap luma on HDD * * \param FileName [OUT] - Image name to dump to * \param Img [IN] - Image luma to dump * \param Stride [IN] - Image stride * \param ImSize [IN] - Image size * * \return None */ void DumpBmpAsGray(char *FileName, byte *Img, int Stride, ROI ImSize) { FILE *fp = NULL; fp = fopen(FileName, "wb"); if (fp == NULL) { return; } BMPFileHeader FileHeader; BMPInfoHeader InfoHeader; //init headers FileHeader._bm_signature = 0x4D42; FileHeader._bm_file_size = 54 + 3 * ImSize.width * ImSize.height; FileHeader._bm_reserved = 0; FileHeader._bm_bitmap_data = 0x36; InfoHeader._bm_bitmap_size = 0; InfoHeader._bm_color_depth = 24; InfoHeader._bm_compressed = 0; InfoHeader._bm_hor_resolution = 0; InfoHeader._bm_image_height = ImSize.height; InfoHeader._bm_image_width = ImSize.width; InfoHeader._bm_info_header_size = 40; InfoHeader._bm_num_colors_used = 0; InfoHeader._bm_num_important_colors = 0; InfoHeader._bm_num_of_planes = 1; InfoHeader._bm_ver_resolution = 0; fwrite(&FileHeader, sizeof(BMPFileHeader), 1, fp); fwrite(&InfoHeader, sizeof(BMPInfoHeader), 1, fp); for (int i = ImSize.height - 1; i>=0; i--) { for (int j=0; j