Skip to content

Commit 19974e2

Browse files
committed
FEAT: macOS support for DirectDraw Surface dds encoding/decoding
1 parent 244c5b8 commit 19974e2

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

src/mezz/codec-image-osx.reb

+10
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ register-codec [
2020
identify: func [data [binary!]][parse data [4 skip #{6674797068656963} to end]]
2121
]
2222

23+
register-codec [
24+
name: 'dds
25+
title: "DirectDraw Surface"
26+
suffixes: [%.dds]
27+
28+
decode: func [data [binary!]][lib/image/load/as data 'DDS]
29+
encode: func [data [image! ]][lib/image/save/as none data 'DDS]
30+
identify: func [data [binary!]][parse data [#{444453207C000000} to end]]
31+
]
32+
2333
register-codec [
2434
name: 'tiff
2535
title: "Tagged Image File Format"

src/os/osx/sys-codecs.m

+13-6
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "sys-codecs.h"
3030
#define kSDUTTypeHEIC ((__bridge CFStringRef)@"public.heic")
3131
#define kSDUTTypeHEIF ((__bridge CFStringRef)@"public.heif")
32+
#define kSDUTTypeDDS ((__bridge CFStringRef)@"com.microsoft.dds")
3233

3334

3435
#ifdef unused
@@ -112,6 +113,7 @@ int EncodeImageToFile(const char *uri, REBCDI *codi)
112113
case CODI_IMG_BMP: type = kUTTypeBMP ; break; // Device independent bitmap
113114
case CODI_IMG_TIFF: type = kUTTypeTIFF ; break; // Tagged Image File Format
114115
case CODI_IMG_HEIF: type = kSDUTTypeHEIC ; break;
116+
case CODI_IMG_DDS: type = kSDUTTypeDDS ; break; // Microsoft DirectDraw Surface
115117
default:
116118
codi->error = 1;
117119
return codi->error;
@@ -125,7 +127,6 @@ int EncodeImageToFile(const char *uri, REBCDI *codi)
125127
CGColorSpaceRelease(colorSpace);
126128
ASSERT_NOT_NULL(img, 2, "create an image");
127129

128-
129130
if(uri == NULL) {
130131
// writing into preallocated buffer (fixed size!)
131132
dataDst = CFDataCreateWithBytesNoCopy(NULL, codi->data, codi->len, NULL);
@@ -138,14 +139,20 @@ int EncodeImageToFile(const char *uri, REBCDI *codi)
138139
}
139140
ASSERT_NOT_NULL(imgDst, 4, "create a destination image");
140141
// TODO: handle user defined options
141-
prop = CFDictionaryCreateMutable(NULL,0,NULL,NULL);
142-
CFDictionaryAddValue(prop, kCGImageDestinationLossyCompressionQuality, "0.2");
142+
143143
CGFloat quality = 0.6;
144-
CGImageDestinationAddImage(imgDst, img, (__bridge CFDictionaryRef)@{(__bridge NSString *)kCGImageDestinationLossyCompressionQuality: @(quality)});
145-
CGImageDestinationFinalize(imgDst);
144+
CGImageDestinationAddImage(imgDst, img, (__bridge CFDictionaryRef)@{
145+
(__bridge NSString *)kCGImageDestinationLossyCompressionQuality: @(quality)
146+
});
147+
148+
if(!CGImageDestinationFinalize(imgDst)) {
149+
// failed to finalize!
150+
error = 6;
151+
}
146152
} while(FALSE);
147-
if(uri == NULL) {
153+
if(uri == NULL && !error) {
148154
codi->len = CFDataGetLength(dataDst);
155+
if(codi->len == 0) error = 5;
149156
//CFRelease(dataDst);
150157
}
151158
SAFE_CF_RELEASE(img);

0 commit comments

Comments
 (0)