diff options
| -rw-r--r-- | dmagick/Options.d | 62 | ||||
| -rw-r--r-- | dmagick/Utils.d | 126 |
2 files changed, 134 insertions, 54 deletions
diff --git a/dmagick/Options.d b/dmagick/Options.d index faf19db..8798a6d 100644 --- a/dmagick/Options.d +++ b/dmagick/Options.d @@ -10,17 +10,17 @@ module dmagick.Options; import std.conv; -import std.math; import core.stdc.stdio; import core.stdc.string; +import dmagick.Utils; + import dmagick.c.cacheView; import dmagick.c.colorspace; import dmagick.c.compress; import dmagick.c.draw; import dmagick.c.geometry; import dmagick.c.image; -import dmagick.c.magickString; import dmagick.c.magickType; import dmagick.c.memory; import dmagick.c.quantize; @@ -41,7 +41,12 @@ class Options quantizeInfo = cast(QuantizeInfo*)AcquireMagickMemory(QuantizeInfo.sizeof); drawInfo = cast(DrawInfo*)AcquireMagickMemory(DrawInfo.sizeof); - //In D all strings are UTF encoded. + //Initialize with defaults. + GetImageInfo(imageInfo); + GetDrawInfo(imageInfo, drawInfo); + GetQuantizeInfo(quantizeInfo); + + //In D strings are UTF encoded. textEncoding("UTF-8"); } @@ -1041,55 +1046,4 @@ class Options //MagickBooleanType measure_error; //size_t signature; //DitherMethod dither_method; - - /** - * Copy a string into a static array used - * by ImageMagick for some atributes. - */ - private void copyString(ref char[MaxTextExtent] dest, string source) - { - if ( source.length < MaxTextExtent ) - throw new Exception("text is to long"); //TODO: a proper exception. - - dest[0 .. source.length] = source; - dest[source.length] = '\0'; - } - - /** - * Our implementation of ImageMagick's CloneString. - * - * We use this since using CloneString forces us to - * append a \0 to the end of the string, and the realocation - * whould be wastefull if we are just going to copy it - */ - private void copyString(ref char* dest, string source) - { - if ( source is null ) - { - if ( dest !is null ) - DestroyString(dest); - return; - } - - if ( ~source.length < MaxTextExtent ) - throw new Exception("UnableToAcquireString"); //TODO: a proper exception. - - if ( dest is null ) - dest = cast(char*)AcquireQuantumMemory(source.length+MaxTextExtent, dest.sizeof); - else - dest = cast(char*)ResizeQuantumMemory(dest, source.length+MaxTextExtent, dest.sizeof); - - if ( dest is null ) - throw new Exception("UnableToAcquireString"); //TODO: a proper exception. - - if ( source.length > 0 ) - dest[0 .. source.length] = source; - - dest[source.length] = '\0'; - } - - real degreesToRadians(real deg) - { - return deg*PI/180; - } } diff --git a/dmagick/Utils.d b/dmagick/Utils.d new file mode 100644 index 0000000..f853350 --- /dev/null +++ b/dmagick/Utils.d @@ -0,0 +1,126 @@ +/** + * A collection of helper functions used in DMagick. + * + * Copyright: Mike Wey 2011 + * License: To be determined + * Authors: Mike Wey + */ + +module dmagick.Utils; + +import std.math; + +import dmagick.c.memory; +import dmagick.c.magickString; +import dmagick.c.magickType; + +/** + * Copy a string into a static array used + * by ImageMagick for some atributes. + */ +private void copyString(ref char[MaxTextExtent] dest, string source) +{ + if ( source.length < MaxTextExtent ) + throw new Exception("text is to long"); //TODO: a proper exception. + + dest[0 .. source.length] = source; + dest[source.length] = '\0'; +} + +/** + * Our implementation of ImageMagick's CloneString. + * + * We use this since using CloneString forces us to + * append a \0 to the end of the string, and the realocation + * whould be wastefull if we are just going to copy it + */ +private void copyString(ref char* dest, string source) +{ + if ( source is null ) + { + if ( dest !is null ) + DestroyString(dest); + return; + } + + if ( ~source.length < MaxTextExtent ) + throw new Exception("UnableToAcquireString"); //TODO: a proper exception. + + if ( dest is null ) + dest = cast(char*)AcquireQuantumMemory(source.length+MaxTextExtent, dest.sizeof); + else + dest = cast(char*)ResizeQuantumMemory(dest, source.length+MaxTextExtent, dest.sizeof); + + if ( dest is null ) + throw new Exception("UnableToAcquireString"); //TODO: a proper exception. + + if ( source.length > 0 ) + dest[0 .. source.length] = source; + + dest[source.length] = '\0'; +} + +real degreesToRadians(real deg) +{ + return deg*PI/180; +} + +struct RefCounted(alias pred, T) + if ( !is(T == class) && is(typeof(pred(null)) == void) ) +{ + T* payload; + + private bool isInitialized; + private size_t* refcount; + + alias payload this; + + this(T* payload) + { + this.payload = payload; + + refcount = new size_t; + *refcount = 1; + + isInitialized = true; + } + + this(this) + { + if ( isInitialized ) + (*refcount)++; + } + + ~this() + { + (*refcount)--; + + if ( *refcount == 0 ) + pred(payload); + } + + @property size_t refCount() + { + return *refcount; + } +} + +unittest +{ + int x = 10; + int y = 20; + + alias RefCounted!( (void* t){ x = 20; }, int ) IntRef; + + auto a = IntRef(&x); + assert( a.refCount == 1 ); + auto b = a; + assert( a.refCount == 2 ); + + b = IntRef(&y); + assert( a.refCount == 1 ); + assert( b.refCount == 1 ); + a = b; + assert( b.refCount == 2 ); + assert( x == 20 ); +} |
