forked from facebook/igl
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Summary: Extract this function. Will add code that can write a texture to bitmap Reviewed By: EricGriffith Differential Revision: D63259599 fbshipit-source-id: 0ced538629791dbb1ed2271a7da8e021fead568c
- Loading branch information
1 parent
a32165f
commit 40bc1f2
Showing
2 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
#include <IGLU/bitmap/BitmapWriter.h> | ||
|
||
#include <fstream> | ||
#include <igl/Common.h> | ||
|
||
namespace igl::iglu { | ||
namespace { | ||
struct BMPHeader { | ||
// Bitmap file header | ||
uint16_t signature = 0x4D42; // Signature ("BM" in ASCII), default value 0x4D42 for BMP format | ||
uint32_t fileSize = 0; | ||
uint16_t reserved1 = 0; | ||
uint16_t reserved2 = 0; | ||
uint32_t dataOffset = sizeof(BMPHeader); // Offset to the start of image data | ||
|
||
// DIB header (Bitmap information header) | ||
uint32_t headerSize = 40; // Size of the DIB header | ||
int32_t imageWidth = 0; | ||
int32_t imageHeight = 0; | ||
uint16_t colorPlanes = 1; // Number of color planes | ||
uint16_t bitsPerPixel = 24; | ||
uint32_t compression = 0; // Compression method (initialized to 0 for no compression) | ||
uint32_t imageSizeBytes = 0; | ||
int32_t horizontalResolution = 0; // Horizontal resolution in pixels per meter | ||
int32_t verticalResolution = 0; // Vertical resolution in pixels per meter | ||
uint32_t numColors = 0; // Number of colors in the color palette | ||
uint32_t importantColors = 0; // Number of important colors used | ||
} __attribute__((packed)); | ||
|
||
} // namespace | ||
|
||
void writeBitmap(const char* filename, const uint8_t* imageData, uint32_t width, uint32_t height) { | ||
std::ofstream file; | ||
file.open(filename, std::ios::out | std::ios::binary); | ||
if (!IGL_VERIFY(file)) { | ||
return; | ||
} | ||
|
||
uint32_t imageSize = width * height * 3; | ||
|
||
BMPHeader header{ | ||
.fileSize = static_cast<uint32_t>(sizeof(BMPHeader) + imageSize), | ||
.imageWidth = static_cast<int32_t>(width), | ||
.imageHeight = static_cast<int32_t>(height), | ||
.imageSizeBytes = static_cast<uint32_t>(imageSize), | ||
}; | ||
|
||
file.write(reinterpret_cast<const char*>(&header), sizeof(header)); | ||
file.write(reinterpret_cast<const char*>(imageData), imageSize); | ||
file.close(); | ||
} | ||
|
||
} // namespace igl::iglu |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <cstdint> | ||
|
||
namespace igl::iglu { | ||
|
||
void writeBitmap(const char* filename, const uint8_t* imageData, uint32_t width, uint32_t height); | ||
|
||
} // namespace igl::iglu |