DALi Version 2.2.11
[platform/core/uifw/dali-toolkit.git] / dali-scene3d / public-api / loader / environment-map-loader.cpp
1 /*
2  * Copyright (c) 2023 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // FILE HEADER
19 #include <dali-scene3d/public-api/loader/environment-map-loader.h>
20
21 // INTERNAL INCLUDES
22 #include <dali-scene3d/public-api/loader/ktx-loader.h>
23
24 // EXTERNAL INCLUDES
25 #include <dali/devel-api/adaptor-framework/image-loading.h>
26 #include <string.h>
27 #include <filesystem>
28
29 namespace Dali
30 {
31 namespace
32 {
33 const std::string_view KTX_EXTENSION = ".ktx";
34
35 static constexpr uint32_t NUMBER_OF_ENVIRONMENT_MAP_TYPE = 5;
36 static constexpr uint32_t NUMBER_OF_CUBE_MAP_TYPE        = 4;
37
38 /**
39  * @brief cube map face index
40  * Cube map layer order is as fallows:
41  * POSITIVE_X, NEGATIVE_X, POSITIVE_Y, NEGATIVE_Y, POSITIVE_Z, NEGATIVE_Z. @see CubeMapLayer
42  * The indices are for 4 kind of environment cube map. Cross_horizontal, Array_horizontal, Cross_vertical, and Array_vertical.
43  */
44 static constexpr uint32_t CUBEMAP_INDEX_X[NUMBER_OF_CUBE_MAP_TYPE][6] = {{2, 0, 1, 1, 1, 3}, {0, 1, 2, 3, 4, 5}, {1, 1, 1, 1, 0, 2}, {0, 0, 0, 0, 0, 0}};
45 static constexpr uint32_t CUBEMAP_INDEX_Y[NUMBER_OF_CUBE_MAP_TYPE][6] = {{1, 1, 0, 2, 1, 1}, {0, 0, 0, 0, 0, 0}, {1, 3, 0, 2, 1, 1}, {0, 1, 2, 3, 4, 5}};
46
47 static constexpr Vector2 NUMBER_OF_CUBE_FACE[NUMBER_OF_CUBE_MAP_TYPE] =
48 {
49   Vector2(4, 3),
50   Vector2(6, 1),
51   Vector2(3, 4),
52   Vector2(1, 6)
53 };
54
55 static constexpr float expectedAspectRatios[NUMBER_OF_ENVIRONMENT_MAP_TYPE] =
56 {
57   4.0f / 3.0f,
58   6.0f / 1.0f,
59   3.0f / 4.0f,
60   1.0f / 6.0f,
61   2.0f / 1.0f
62 };
63
64 enum CubeType
65 {
66   CROSS_HORIZONTAL = 0, // Cross horizontal style cube map
67   ARRAY_HORIZONTAL,     // array horizontal style cube map
68   CROSS_VERTICAL,       // Cross vertical style cube map
69   ARRAY_VERTICAL,       // array vertical style cube map
70   NONE
71 };
72
73 uint8_t* GetCroppedBuffer(uint8_t* sourceBuffer, uint32_t bytesPerPixel, uint32_t width, uint32_t height, uint32_t xOffset, uint32_t yOffset, uint32_t xFaceSize, uint32_t yFaceSize)
74 {
75   uint32_t byteSize   = bytesPerPixel * xFaceSize * yFaceSize;
76   uint8_t* destBuffer = reinterpret_cast<uint8_t*>(malloc(byteSize + 4u));
77
78   int32_t srcStride  = width * bytesPerPixel;
79   int32_t destStride = xFaceSize * bytesPerPixel;
80   int32_t srcOffset  = xOffset * bytesPerPixel + yOffset * srcStride;
81   int32_t destOffset = 0;
82   for(uint16_t row = yOffset; row < yOffset + yFaceSize; ++row)
83   {
84     memcpy(destBuffer + destOffset, sourceBuffer + srcOffset, destStride);
85     srcOffset += srcStride;
86     destOffset += destStride;
87   }
88
89   return destBuffer;
90 }
91
92 PixelData GetCubeFace(Devel::PixelBuffer pixelBuffer, uint32_t faceIndex, CubeType cubeType, float faceWidth, float faceHeight)
93 {
94   PixelData pixelData;
95   if(cubeType != NONE)
96   {
97     uint8_t* imageBuffer   = pixelBuffer.GetBuffer();
98     uint32_t bytesPerPixel = Pixel::GetBytesPerPixel(pixelBuffer.GetPixelFormat());
99     uint32_t imageWidth    = pixelBuffer.GetWidth();
100     uint32_t imageHeight   = pixelBuffer.GetHeight();
101
102     uint32_t xOffset = CUBEMAP_INDEX_X[cubeType][faceIndex] * static_cast<uint32_t>(faceWidth);
103     uint32_t yOffset = CUBEMAP_INDEX_Y[cubeType][faceIndex] * static_cast<uint32_t>(faceHeight);
104
105     uint32_t finalFaceWidth  = (xOffset + static_cast<uint32_t>(faceWidth) < imageWidth) ? static_cast<uint32_t>(faceWidth) : imageWidth - xOffset;
106     uint32_t finalFaceHeight = (yOffset + static_cast<uint32_t>(faceHeight) < imageHeight) ? static_cast<uint32_t>(faceHeight) : imageHeight - yOffset;
107     uint8_t* tempImageBuffer = GetCroppedBuffer(imageBuffer, bytesPerPixel, imageWidth, imageHeight, xOffset, yOffset, finalFaceWidth, finalFaceHeight);
108     pixelData                = PixelData::New(tempImageBuffer, finalFaceWidth * finalFaceHeight * bytesPerPixel, finalFaceWidth, finalFaceHeight, pixelBuffer.GetPixelFormat(), PixelData::FREE);
109   }
110   return pixelData;
111 }
112
113 /**
114  * @brief Loads environment map data texture from an image url.
115  *
116  * @param[in] environmentMapUrl The environment map file url.
117  * @param[out] environmentMapData The data structure with all pixel data objects.
118  * @return bool True if the loading is succeded.
119  */
120 bool LoadEnvironmentMapData(const std::string& environmentMapUrl, Scene3D::Loader::EnvironmentMapData& environmentMapData)
121 {
122   // Diffuse Environment Map
123   if(environmentMapUrl.empty())
124   {
125     return false;
126   }
127
128   Devel::PixelBuffer pixelBuffer = LoadImageFromFile(environmentMapUrl);
129   if(pixelBuffer)
130   {
131     CubeType cubeType    = NONE;
132     uint32_t imageWidth  = pixelBuffer.GetWidth();
133     uint32_t imageHeight = pixelBuffer.GetHeight();
134     /**
135      * If the environment map type is not EQUIRECTANGULAR,
136      * The type should be defined internally.
137      * DALi checkes aspect ratio of input image and find the closest type.
138      * If the environment map type is CUBEMAP, DALi determines which of the following styles is closest:
139      * cross horizontal, cross vertical, array horizontal, and array vertical.
140      * When the environment map type is AUTO, it finds the closest type, including the Equirectangular type.
141      */
142     if(environmentMapData.GetEnvironmentMapType() != Scene3D::EnvironmentMapType::EQUIRECTANGULAR)
143     {
144       float aspectRatio = (float)imageWidth / (float)imageHeight;
145
146       float    minDistance = FLT_MAX;
147       uint32_t typeCount   = (environmentMapData.GetEnvironmentMapType() == Scene3D::EnvironmentMapType::CUBEMAP) ? NUMBER_OF_CUBE_MAP_TYPE : NUMBER_OF_ENVIRONMENT_MAP_TYPE;
148       for(uint32_t i = 0; i < typeCount; ++i)
149       {
150         float distance = fabs(aspectRatio - expectedAspectRatios[i]);
151         if(distance < minDistance)
152         {
153           minDistance = distance;
154           cubeType    = static_cast<CubeType>(i);
155         }
156       }
157     }
158
159     if(cubeType != NONE)
160     {
161       float faceWidth = imageWidth, faceHeight = imageHeight;
162       faceWidth /= NUMBER_OF_CUBE_FACE[static_cast<uint32_t>(cubeType)].x;
163       faceHeight /= NUMBER_OF_CUBE_FACE[static_cast<uint32_t>(cubeType)].y;
164       environmentMapData.mPixelData.resize(6);
165       for(uint32_t i = 0; i < 6; ++i)
166       {
167         environmentMapData.mPixelData[i].resize(1);
168       }
169       for(uint32_t i = 0; i < 6; ++i)
170       {
171         environmentMapData.mPixelData[i][0] = GetCubeFace(pixelBuffer, i, cubeType, faceWidth, faceHeight);
172       }
173       environmentMapData.SetEnvironmentMapType(Scene3D::EnvironmentMapType::CUBEMAP);
174     }
175     else
176     {
177       environmentMapData.mPixelData.resize(1);
178       environmentMapData.mPixelData[0].resize(1);
179       environmentMapData.mPixelData[0][0] = Devel::PixelBuffer::Convert(pixelBuffer);
180       environmentMapData.SetEnvironmentMapType(Scene3D::EnvironmentMapType::EQUIRECTANGULAR);
181     }
182     return true;
183   }
184   return false;
185 }
186 } // namespace
187
188 namespace Scene3D
189 {
190 namespace Loader
191 {
192
193 bool LoadEnvironmentMap(const std::string& environmentMapUrl, EnvironmentMapData& environmentMapData)
194 {
195   std::filesystem::path modelPath(environmentMapUrl);
196   std::string           extension = modelPath.extension();
197   std::transform(extension.begin(), extension.end(), extension.begin(), ::tolower);
198
199   return (extension == KTX_EXTENSION) ? Dali::Scene3D::Loader::LoadKtxData(environmentMapUrl, environmentMapData) : LoadEnvironmentMapData(environmentMapUrl, environmentMapData);
200 }
201
202 } // namespace Loader
203 } // namespace Scene3D
204 } // namespace Dali