MCPcopy Create free account
hub / github.com/CesiumGS/cesium-native / writeTgaImpl

Function writeTgaImpl

CesiumNativeTests/src/writeTga.cpp:15–95  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

13namespace CesiumNativeTests {
14namespace {
15void writeTgaImpl(
16 const std::filesystem::path& outputPath,
17 const std::byte* pData,
18 int32_t channels,
19 int32_t width,
20 int32_t height) {
21 std::ofstream stream(outputPath, std::ios::binary | std::ios::out);
22 CESIUM_ASSERT(stream.good());
23
24 uint64_t zeroes = 0;
25
26 // No image identification field included.
27 stream.write(reinterpret_cast<const char*>(&zeroes), 1);
28 // No color map included - raw RGB.
29 stream.write(reinterpret_cast<const char*>(&zeroes), 1);
30 // Data Type 2 - Unmapped RGB
31 const uint8_t dataType = 2;
32 stream.write(reinterpret_cast<const char*>(&dataType), 1);
33 // Color Map Specification is five bytes long - we don't need it, but we need
34 // to write that many bytes.
35 stream.write(reinterpret_cast<const char*>(&zeroes), 5);
36 // X origin
37 stream.write(reinterpret_cast<const char*>(&zeroes), 2);
38 // Y origin
39 stream.write(reinterpret_cast<const char*>(&zeroes), 2);
40 // Width
41 const uint16_t width16 = (uint16_t)width;
42 stream.write(reinterpret_cast<const char*>(&width16), 2);
43 // Height
44 const uint16_t height16 = (uint16_t)height;
45 stream.write(reinterpret_cast<const char*>(&height16), 2);
46 // Bits per pixel
47 const uint8_t bpp = 32;
48 stream.write(reinterpret_cast<const char*>(&bpp), 1);
49 // Image Descriptor Byte
50 // Bits 0-3 are the attribute bit count. For Targa 32 it's 8 bits. We don't
51 // care about the other flags.
52 const uint8_t descriptor = 8;
53 stream.write(reinterpret_cast<const char*>(&descriptor), 1);
54 // No image identification field
55 // No color map data
56
57 // Image Data Field
58 for (int i = 0; i < width; i++) {
59 for (int j = 0; j < height; j++) {
60 // All images written as RGBA no matter the actual channel count
61 // Blue
62 stream.write(
63 (channels > 2 ? reinterpret_cast<const char*>(
64 &pData[(size_t)((i * width + j) * channels + 2)])
65 : reinterpret_cast<const char*>(&zeroes)),
66 1);
67 // Green
68 stream.write(
69 (channels > 2 ? reinterpret_cast<const char*>(
70 &pData[(size_t)((i * width + j) * channels + 1)])
71 : reinterpret_cast<const char*>(&zeroes)),
72 1);

Callers 1

writeImageToTgaFileFunction · 0.85

Calls 1

writeMethod · 0.45

Tested by

no test coverage detected