Debian Codegraph @

Samples/2_Concepts_and_Techniques/dct8x8/BmpUtil.cpp

raw
1 /* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
2 *
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions
5 * are met:
6 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution.
11 * * Neither the name of NVIDIA CORPORATION nor the names of its
12 * contributors may be used to endorse or promote products derived
13 * from this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 /**
29 **************************************************************************
30 * \file BmpUtil.cpp
31 * \brief Contains basic image operations implementation.
32 *
33 * This file contains implementation of basic bitmap loading, saving,
34 * conversions to different representations and memory management routines.
35 */
36
37 #include "Common.h"
38 #include "BmpUtil.h"
39
40 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
41 #pragma warning(disable : 4996) // disable deprecated warning
42 #endif
43
44 /**
45 **************************************************************************
46 * The routine clamps the input value to integer byte range [0, 255]
47 *
48 * \param x [IN] - Input value
49 *
50 * \return Pointer to the created plane
51 */
52 int clamp_0_255(int x) { return (x < 0) ? 0 : ((x > 255) ? 255 : x); }
53
54 /**
55 **************************************************************************
56 * Float round to nearest value
57 *
58 * \param num [IN] - Float value to round
59 *
60 * \return The closest to the input float integer value
61 */
62 float round_f(float num) {
63 float NumAbs = fabs(num);
64 int NumAbsI = (int)(NumAbs + 0.5f);
65 float sign = num > 0 ? 1.0f : -1.0f;
66 return sign * NumAbsI;
67 }
68
69 /**
70 **************************************************************************
71 * Memory allocator, returns aligned format frame with 8bpp pixels.
72 *
73 * \param width [IN] - Width of image buffer to be allocated
74 * \param height [IN] - Height of image buffer to be allocated
75 * \param pStepBytes [OUT] - Step between two sequential rows
76 *
77 * \return Pointer to the created plane
78 */
79 byte *MallocPlaneByte(int width, int height, int *pStepBytes) {
80 byte *ptr;
81 *pStepBytes = ((int)ceil(width / 16.0f)) * 16;
82 //#ifdef __ALLOW_ALIGNED_MEMORY_MANAGEMENT
83 // ptr = (byte *)_aligned_malloc(*pStepBytes * height, 16);
84 //#else
85 ptr = (byte *)malloc(*pStepBytes * height);
86 //#endif
87 return ptr;
88 }
89
90 /**
91 **************************************************************************
92 * Memory allocator, returns aligned format frame with 16bpp float pixels.
93 *
94 * \param width [IN] - Width of image buffer to be allocated
95 * \param height [IN] - Height of image buffer to be allocated
96 * \param pStepBytes [OUT] - Step between two sequential rows
97 *
98 * \return Pointer to the created plane
99 */
100 short *MallocPlaneShort(int width, int height, int *pStepBytes) {
101 short *ptr;
102 *pStepBytes = ((int)ceil((width * sizeof(short)) / 16.0f)) * 16;
103 //#ifdef __ALLOW_ALIGNED_MEMORY_MANAGEMENT
104 // ptr = (float *)_aligned_malloc(*pStepBytes * height, 16);
105 //#else
106 ptr = (short *)malloc(*pStepBytes * height);
107 //#endif
108 *pStepBytes = *pStepBytes / sizeof(short);
109 return ptr;
110 }
111
112 /**
113 **************************************************************************
114 * Memory allocator, returns aligned format frame with 32bpp float pixels.
115 *
116 * \param width [IN] - Width of image buffer to be allocated
117 * \param height [IN] - Height of image buffer to be allocated
118 * \param pStepBytes [OUT] - Step between two sequential rows
119 *
120 * \return Pointer to the created plane
121 */
122 float *MallocPlaneFloat(int width, int height, int *pStepBytes) {
123 float *ptr;
124 *pStepBytes = ((int)ceil((width * sizeof(float)) / 16.0f)) * 16;
125 //#ifdef __ALLOW_ALIGNED_MEMORY_MANAGEMENT
126 // ptr = (float *)_aligned_malloc(*pStepBytes * height, 16);
127 //#else
128 ptr = (float *)malloc(*pStepBytes * height);
129 //#endif
130 *pStepBytes = *pStepBytes / sizeof(float);
131 return ptr;
132 }
133
134 /**
135 **************************************************************************
136 * Copies byte plane to float plane
137 *
138 * \param ImgSrc [IN] - Source byte plane
139 * \param StrideB [IN] - Source plane stride
140 * \param ImgDst [OUT] - Destination float plane
141 * \param StrideF [IN] - Destination plane stride
142 * \param Size [IN] - Size of area to copy
143 *
144 * \return None
145 */
146 void CopyByte2Float(byte *ImgSrc, int StrideB, float *ImgDst, int StrideF,
147 ROI Size) {
148 for (int i = 0; i < Size.height; i++) {
149 for (int j = 0; j < Size.width; j++) {
150 ImgDst[i * StrideF + j] = (float)ImgSrc[i * StrideB + j];
151 }
152 }
153 }
154
155 /**
156 **************************************************************************
157 * Copies float plane to byte plane (with clamp)
158 *
159 * \param ImgSrc [IN] - Source float plane
160 * \param StrideF [IN] - Source plane stride
161 * \param ImgDst [OUT] - Destination byte plane
162 * \param StrideB [IN] - Destination plane stride
163 * \param Size [IN] - Size of area to copy
164 *
165 * \return None
166 */
167 void CopyFloat2Byte(float *ImgSrc, int StrideF, byte *ImgDst, int StrideB,
168 ROI Size) {
169 for (int i = 0; i < Size.height; i++) {
170 for (int j = 0; j < Size.width; j++) {
171 ImgDst[i * StrideB + j] =
172 (byte)clamp_0_255((int)(round_f(ImgSrc[i * StrideF + j])));
173 }
174 }
175 }
176
177 /**
178 **************************************************************************
179 * Memory deallocator, deletes aligned format frame.
180 *
181 * \param ptr [IN] - Pointer to the plane
182 *
183 * \return None
184 */
185 void FreePlane(void *ptr) {
186 //#ifdef __ALLOW_ALIGNED_MEMORY_MANAGEMENT
187 // if (ptr)
188 // {
189 // _aligned_free(ptr);
190 // }
191 //#else
192 if (ptr) {
193 free(ptr);
194 }
195
196 //#endif
197 }
198
199 /**
200 **************************************************************************
201 * Performs addition of given value to each pixel in the plane
202 *
203 * \param Value [IN] - Value to add
204 * \param ImgSrcDst [IN/OUT] - Source float plane
205 * \param StrideF [IN] - Source plane stride
206 * \param Size [IN] - Size of area to copy
207 *
208 * \return None
209 */
210 void AddFloatPlane(float Value, float *ImgSrcDst, int StrideF, ROI Size) {
211 for (int i = 0; i < Size.height; i++) {
212 for (int j = 0; j < Size.width; j++) {
213 ImgSrcDst[i * StrideF + j] += Value;
214 }
215 }
216 }
217
218 /**
219 **************************************************************************
220 * Performs multiplication of given value with each pixel in the plane
221 *
222 * \param Value [IN] - Value for multiplication
223 * \param ImgSrcDst [IN/OUT] - Source float plane
224 * \param StrideF [IN] - Source plane stride
225 * \param Size [IN] - Size of area to copy
226 *
227 * \return None
228 */
229 void MulFloatPlane(float Value, float *ImgSrcDst, int StrideF, ROI Size) {
230 for (int i = 0; i < Size.height; i++) {
231 for (int j = 0; j < Size.width; j++) {
232 ImgSrcDst[i * StrideF + j] *= Value;
233 }
234 }
235 }
236
237 /**
238 **************************************************************************
239 * This function performs acquisition of image dimensions
240 *
241 * \param FileName [IN] - Image name to load
242 * \param Width [OUT] - Image width from file header
243 * \param Height [OUT] - Image height from file header
244 *
245 * \return Status code
246 */
247 int PreLoadBmp(char *FileName, int *Width, int *Height) {
248 BMPFileHeader FileHeader;
249 BMPInfoHeader InfoHeader;
250 FILE *fh;
251
252 if (!(fh = fopen(FileName, "rb"))) {
253 return 1; // invalid filename
254 }
255
256 fread(&FileHeader, sizeof(BMPFileHeader), 1, fh);
257
258 if (FileHeader._bm_signature != 0x4D42) {
259 return 2; // invalid file format
260 }
261
262 fread(&InfoHeader, sizeof(BMPInfoHeader), 1, fh);
263
264 if (InfoHeader._bm_color_depth != 24) {
265 return 3; // invalid color depth
266 }
267
268 if (InfoHeader._bm_compressed) {
269 return 4; // invalid compression property
270 }
271
272 *Width = InfoHeader._bm_image_width;
273 *Height = InfoHeader._bm_image_height;
274
275 fclose(fh);
276 return 0;
277 }
278
279 /**
280 **************************************************************************
281 * This function performs loading of bitmap luma
282 *
283 * \param FileName [IN] - Image name to load
284 * \param Stride [IN] - Image stride
285 * \param ImSize [IN] - Image size
286 * \param Img [OUT] - Prepared buffer
287 *
288 * \return None
289 */
290 void LoadBmpAsGray(char *FileName, int Stride, ROI ImSize, byte *Img) {
291 BMPFileHeader FileHeader;
292 BMPInfoHeader InfoHeader;
293 FILE *fh;
294 fh = fopen(FileName, "rb");
295
296 fread(&FileHeader, sizeof(BMPFileHeader), 1, fh);
297 fread(&InfoHeader, sizeof(BMPInfoHeader), 1, fh);
298
299 for (int i = ImSize.height - 1; i >= 0; i--) {
300 for (int j = 0; j < ImSize.width; j++) {
301 int r = 0, g = 0, b = 0;
302 fread(&b, 1, 1, fh);
303 fread(&g, 1, 1, fh);
304 fread(&r, 1, 1, fh);
305 int val = (313524 * r + 615514 * g + 119537 * b + 524288) >> 20;
306 Img[i * Stride + j] = (byte)clamp_0_255(val);
307 }
308 }
309
310 fclose(fh);
311 return;
312 }
313
314 /**
315 **************************************************************************
316 * This function performs dumping of bitmap luma on HDD
317 *
318 * \param FileName [OUT] - Image name to dump to
319 * \param Img [IN] - Image luma to dump
320 * \param Stride [IN] - Image stride
321 * \param ImSize [IN] - Image size
322 *
323 * \return None
324 */
325 void DumpBmpAsGray(char *FileName, byte *Img, int Stride, ROI ImSize) {
326 FILE *fp = NULL;
327 fp = fopen(FileName, "wb");
328
329 if (fp == NULL) {
330 return;
331 }
332
333 BMPFileHeader FileHeader;
334 BMPInfoHeader InfoHeader;
335
336 // init headers
337 FileHeader._bm_signature = 0x4D42;
338 FileHeader._bm_file_size = 54 + 3 * ImSize.width * ImSize.height;
339 FileHeader._bm_reserved = 0;
340 FileHeader._bm_bitmap_data = 0x36;
341 InfoHeader._bm_bitmap_size = 0;
342 InfoHeader._bm_color_depth = 24;
343 InfoHeader._bm_compressed = 0;
344 InfoHeader._bm_hor_resolution = 0;
345 InfoHeader._bm_image_height = ImSize.height;
346 InfoHeader._bm_image_width = ImSize.width;
347 InfoHeader._bm_info_header_size = 40;
348 InfoHeader._bm_num_colors_used = 0;
349 InfoHeader._bm_num_important_colors = 0;
350 InfoHeader._bm_num_of_planes = 1;
351 InfoHeader._bm_ver_resolution = 0;
352
353 fwrite(&FileHeader, sizeof(BMPFileHeader), 1, fp);
354 fwrite(&InfoHeader, sizeof(BMPInfoHeader), 1, fp);
355
356 for (int i = ImSize.height - 1; i >= 0; i--) {
357 for (int j = 0; j < ImSize.width; j++) {
358 fwrite(&(Img[i * Stride + j]), 1, 1, fp);
359 fwrite(&(Img[i * Stride + j]), 1, 1, fp);
360 fwrite(&(Img[i * Stride + j]), 1, 1, fp);
361 }
362 }
363
364 fclose(fp);
365 }
366
367 /**
368 **************************************************************************
369 * This function performs dumping of 8x8 block from float plane
370 *
371 * \param PlaneF [IN] - Image plane
372 * \param StrideF [IN] - Image stride
373 * \param Fname [OUT] - File name to dump to
374 *
375 * \return None
376 */
377 void DumpBlockF(float *PlaneF, int StrideF, char *Fname) {
378 FILE *fp = fopen(Fname, "wb");
379
380 for (int i = 0; i < 8; i++) {
381 for (int j = 0; j < 8; j++) {
382 fprintf(fp, "%.*f ", 14, PlaneF[i * StrideF + j]);
383 }
384
385 fprintf(fp, "\n");
386 }
387
388 fclose(fp);
389 }
390
391 /**
392 **************************************************************************
393 * This function performs dumping of 8x8 block from byte plane
394 *
395 * \param Plane [IN] - Image plane
396 * \param Stride [IN] - Image stride
397 * \param Fname [OUT] - File name to dump to
398 *
399 * \return None
400 */
401 void DumpBlock(byte *Plane, int Stride, char *Fname) {
402 FILE *fp = fopen(Fname, "wb");
403
404 for (int i = 0; i < 8; i++) {
405 for (int j = 0; j < 8; j++) {
406 fprintf(fp, "%.3d ", Plane[i * Stride + j]);
407 }
408
409 fprintf(fp, "\n");
410 }
411
412 fclose(fp);
413 }
414
415 /**
416 **************************************************************************
417 * This function performs evaluation of Mean Square Error between two images
418 *
419 * \param Img1 [IN] - Image 1
420 * \param Img2 [IN] - Image 2
421 * \param Stride [IN] - Image stride
422 * \param Size [IN] - Image size
423 *
424 * \return Mean Square Error between images
425 */
426 float CalculateMSE(byte *Img1, byte *Img2, int Stride, ROI Size) {
427 uint32 Acc = 0;
428
429 for (int i = 0; i < Size.height; i++) {
430 for (int j = 0; j < Size.width; j++) {
431 int TmpDiff = Img1[i * Stride + j] - Img2[i * Stride + j];
432 TmpDiff *= TmpDiff;
433 Acc += TmpDiff;
434 }
435 }
436
437 return ((float)Acc) / (Size.height * Size.width);
438 }
439
440 /**
441 **************************************************************************
442 * This function performs evaluation of Peak Signal to Noise Ratio between
443 * two images
444 *
445 * \param Img1 [IN] - Image 1
446 * \param Img2 [IN] - Image 2
447 * \param Stride [IN] - Image stride
448 * \param Size [IN] - Image size
449 *
450 * \return Peak Signal to Noise Ratio between images
451 */
452 float CalculatePSNR(byte *Img1, byte *Img2, int Stride, ROI Size) {
453 float MSE = CalculateMSE(Img1, Img2, Stride, Size);
454 return 10 * log10(255 * 255 / MSE);
455 }