2 * Copyright (c) 2022 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali-scene3d/public-api/loader/cube-loader.h>
22 #include <dali/devel-api/adaptor-framework/image-loading.h>
23 #include <dali/devel-api/adaptor-framework/pixel-buffer.h>
24 #include <dali/public-api/common/vector-wrapper.h>
25 #include <dali/public-api/rendering/texture.h>
26 #include <dali/integration-api/debug.h>
34 * @brief cube map face index
35 * Cube map layer order is as fallows:
36 * POSITIVE_X, NEGATIVE_X, POSITIVE_Y, NEGATIVE_Y, POSITIVE_Z, NEGATIVE_Z. @see CubeMapLayer
37 * The indices are for 4 kind of environment cube map. Cross_horizontal, Array_horizontal, Cross_vertical, and Array_vertical.
39 const uint32_t CUBEMAP_INDEX_X[4][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}};
40 const uint32_t CUBEMAP_INDEX_Y[4][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}};
44 CROSS_HORIZONTAL = 0, // Cross horizontal style cube map
45 ARRAY_HORIZONTAL, // array horizontal style cube map
46 CROSS_VERTICAL, // Cross vertical style cube map
47 ARRAY_VERTICAL, // array vertical style cube map
51 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)
53 uint32_t byteSize = bytesPerPixel * xFaceSize * yFaceSize;
54 uint8_t* destBuffer = reinterpret_cast<uint8_t*>(malloc(byteSize + 4u));
56 int32_t srcStride = width * bytesPerPixel;
57 int32_t destStride = xFaceSize * bytesPerPixel;
58 int32_t srcOffset = xOffset * bytesPerPixel + yOffset * srcStride;
59 int32_t destOffset = 0;
60 for(uint16_t row = yOffset; row < yOffset + yFaceSize; ++row)
62 memcpy(destBuffer + destOffset, sourceBuffer + srcOffset, destStride);
63 srcOffset += srcStride;
64 destOffset += destStride;
70 PixelData GetCubeFace(Devel::PixelBuffer pixelBuffer, uint32_t faceIndex, CubeType cubeType, uint32_t faceSize)
75 uint8_t* imageBuffer = pixelBuffer.GetBuffer();
76 uint32_t bytesPerPixel = Pixel::GetBytesPerPixel(pixelBuffer.GetPixelFormat());
77 uint32_t imageWidth = pixelBuffer.GetWidth();
78 uint32_t imageHeight = pixelBuffer.GetHeight();
80 uint32_t xOffset = CUBEMAP_INDEX_X[cubeType][faceIndex] * faceSize;
81 uint32_t yOffset = CUBEMAP_INDEX_Y[cubeType][faceIndex] * faceSize;
83 uint8_t* tempImageBuffer = GetCroppedBuffer(imageBuffer, bytesPerPixel, imageWidth, imageHeight, xOffset, yOffset, faceSize, faceSize);
84 pixelData = PixelData::New(tempImageBuffer, faceSize * faceSize * bytesPerPixel, faceSize, faceSize, pixelBuffer.GetPixelFormat(), PixelData::FREE);
94 bool LoadCubeData(const std::string& path, CubeData& cubedata)
103 Devel::PixelBuffer pixelBuffer = LoadImageFromFile(path);
106 uint32_t imageWidth = pixelBuffer.GetWidth();
107 uint32_t imageHeight = pixelBuffer.GetHeight();
109 uint32_t faceSize = 0;
110 CubeType cubeType = NONE;
111 if(imageWidth / 4 == imageHeight / 3)
113 cubeType = CROSS_HORIZONTAL;
114 faceSize = imageWidth / 4;
116 else if(imageWidth / 6 == imageHeight)
118 cubeType = ARRAY_HORIZONTAL;
119 faceSize = imageHeight;
121 else if(imageWidth / 3 == imageHeight / 4)
123 cubeType = CROSS_VERTICAL;
124 faceSize = imageHeight / 4;
126 else if(imageWidth == imageHeight / 6)
128 cubeType = ARRAY_VERTICAL;
129 faceSize = imageWidth;
133 DALI_LOG_ERROR("The image is not a cube map\n");
137 cubedata.data.resize(6);
138 for(uint32_t i = 0; i < 6; ++i)
140 cubedata.data[i].resize(1);
142 for(uint32_t i = 0; i < 6; ++i)
144 cubedata.data[i][0] = GetCubeFace(pixelBuffer, i, cubeType, faceSize);
152 } // namespace Loader
153 } // namespace Scene3D