Skip to content

Commit

Permalink
Merge pull request #1278 from overte-org/feature/base64_images
Browse files Browse the repository at this point in the history
Add support for base64 images in image entity URLs
  • Loading branch information
daleglass authored Dec 28, 2024
2 parents 2986352 + d71264f commit a55d019
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
4 changes: 3 additions & 1 deletion libraries/entities/src/EntityItemPropertiesDocs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -935,7 +935,9 @@
*
* @typedef {object} Entities.EntityProperties-Image
* @property {Vec3} dimensions=0.1,0.1,0.01 - The dimensions of the entity.
* @property {string} imageURL="" - The URL of the image to use.
* @property {string} imageURL="" - The URL of the image to use. It can also contain a base64 encoded image, in the same format as glTF.
* For network transmitted entities there's about 1000-character limit for the length of this field. For base64 image
* the property string needs to begin with `data:image/png;base64,`, `data:image/jpeg;base64,` or `data:image/webp;base64,`.
* @property {boolean} emissive=false - <code>true</code> if the image should be emissive (unlit), <code>false</code> if it
* shouldn't.
* @property {boolean} keepAspectRatio=true - <code>true</code> if the image should maintain its aspect ratio,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,18 @@ NetworkTexturePointer TextureCache::getTexture(const QUrl& url, image::TextureUs
if (url.scheme() == RESOURCE_SCHEME) {
return getResourceTexture(url);
}

QString urlString = url.toString();
if (content.isEmpty() && (urlString.startsWith("data:image/jpeg;base64,")
|| urlString.startsWith("data:image/png;base64,")
|| urlString.startsWith("data:image/webp;base64,"))) {
QString binaryUrl = urlString.split(",")[1];
auto decodedContent = binaryUrl.isEmpty() ? QByteArray() : QByteArray::fromBase64(binaryUrl.toUtf8());
if (!decodedContent.isEmpty()) {
return getTexture(url, type, decodedContent, maxNumPixels, sourceChannel);
}
}

QString decodedURL = QUrl::fromPercentEncoding(url.toEncoded());
if (decodedURL.startsWith("{")) {
return getTextureByUUID(decodedURL);
Expand Down
2 changes: 1 addition & 1 deletion libraries/model-serializers/src/GLTFSerializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1335,7 +1335,7 @@ HFMTexture GLTFSerializer::getHFMTexture(const cgltf_texture *texture) {
hfmTex.filename = textureUrl.toEncoded().append(QString::number(imageIndex).toUtf8());
}

if (url.contains("data:image/jpeg;base64,") || url.contains("data:image/png;base64,") || url.contains("data:image/webp;base64,")) {
if (url.startsWith("data:image/jpeg;base64,") || url.startsWith("data:image/png;base64,") || url.startsWith("data:image/webp;base64,")) {
hfmTex.content = requestEmbeddedData(url);
}
}
Expand Down

0 comments on commit a55d019

Please sign in to comment.