Permalink
Browse files

Command line tool now uses the library's sources for compression/deco…

…mpression.
  • Loading branch information...
1 parent 6a6c3c7 commit 9333a77f368d3d16c7971d41197b771e022304aa @st3fan st3fan committed Jan 17, 2010
Showing with 115 additions and 184 deletions.
  1. +2 −2 tools/Makefile
  2. +113 −182 tools/rle.cc
View
@@ -19,8 +19,8 @@
all: rle raw
-rle: rle.cc
- c++ -g -framework ApplicationServices -o rle rle.cc
+rle: rle.cc ../src/AnimationCompression.c
+ c++ -g -framework ApplicationServices -o rle rle.cc ../src/AnimationCompression.c
raw: raw.cc
c++ -g -framework ApplicationServices -o raw raw.cc
View
@@ -18,82 +18,15 @@
*/
//
-// rle.cc - compress a collection of images to an animation container
+// rle.cc - compress a collection of images to an animation container. images are
+// compressed using a simple run-length encoding.
//
-// usage: raw destination.animation width height files*
+// usage: raw destination.animation width height files*
//
#include <ApplicationServices/ApplicationServices.h>
#include "../src/AnimationCommon.h"
-
-inline uint32_t RLEWriteRun(uint32_t*& dst, uint32_t n, uint32_t c)
-{
- //printf(" Writing a run of %d x %x\n", n, c);
-
- *dst++ = n;
- *dst++ = c;
-
- return 2;
-}
-
-unsigned int RLECompressPixels(uint32_t* dst, uint32_t* src, unsigned int count)
-{
- //printf("Compressing RGBA pixel data with %d pixels\n", count);
-
- unsigned int compressedLength = 0;
-
- uint32_t* p = src;
- uint32_t c;
- uint32_t n = 0;
-
- for (int i = 0; i < count; i++, p++)
- {
- //printf(" Looking at 0x%.8x\n", *p);
-
- if (n == 0) {
- //fprintf(stderr, " Starting a new run: n = 1 c = 0x%.8x\n", c);
- c = *p;
- n = 1;
- } else {
- if (*p == c) {
- n++;
- //fprintf(stderr, " More of the same: n = %d c = 0x%.8x\n", n, c);
- } else {
- //fprintf(stderr, " Run changed, writing run: n = %d c = 0x%.8x\n", n, c);
- compressedLength += RLEWriteRun(dst, n, c);
- c = *p;
- n = 1;
- }
- }
- }
-
- if (n != 0) {
- compressedLength += RLEWriteRun(dst, n, c);
- }
-
- return compressedLength * 4;
-}
-
-uint32_t RLEUncompressPixels(uint32_t* dst, uint32_t* src, uint32_t count)
-{
- uint32_t length = 0;
-
- while (count != 0)
- {
- uint32_t n = *src++;
- uint32_t c = *src++;
-
- //printf("Uncompressing a run of %d x %.8x\n", n, c);
-
- for (int i = 0; i < n; i++) {
- *dst++ = c;
- count--;
- length += 4;
- }
- }
-
- return length;
-}
+#include "../src/AnimationCompression.h"
int main(int argc, char** argv)
{
@@ -105,138 +38,136 @@ int main(int argc, char** argv)
// Create the animation container
int fd = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC, 0644);
- if (fd != -1)
- {
- // Create a buffer for the images
+ if (fd == -1) {
+ fprintf(stderr, "Cannot create %s: %s\n", argv[1], strerror(errno));
+ exit(1);
+ }
- uint32_t* buffer = (uint32_t*) calloc(width * height, sizeof(uint32_t));
+ // Create a buffer for the images
- // Write the container header
+ uint32_t* buffer = (uint32_t*) calloc(width * height, sizeof(uint32_t));
- AnimationContainerGlobalHeader containerHeader;
- containerHeader.magic = 'anim';
- containerHeader.version = 1;
- containerHeader.width = width;
- containerHeader.height = height;
- containerHeader.frameRate = 12;
- containerHeader.frameCount = argc - 4;
+ // Write the container header
- write(fd, &containerHeader, sizeof(containerHeader));
+ AnimationContainerGlobalHeader containerHeader;
+ containerHeader.magic = 'anim';
+ containerHeader.version = 1;
+ containerHeader.width = width;
+ containerHeader.height = height;
+ containerHeader.frameRate = 12;
+ containerHeader.frameCount = argc - 4;
- // Write all the images
+ write(fd, &containerHeader, sizeof(containerHeader));
- for (int i = 4; i < argc; i++)
- {
- char* path = argv[i];
- printf("Processing %s\n", path);
-
- // Uncompress the image
+ // Write all the images
- memset(buffer, 0x00, width * height * sizeof(uint32_t));
-
- CGDataProviderRef provider = CGDataProviderCreateWithFilename(path);
- if (provider != NULL)
+ for (int i = 4; i < argc; i++)
+ {
+ char* path = argv[i];
+ printf("Processing %s\n", path);
+
+ // Uncompress the image
+
+ memset(buffer, 0x00, width * height * sizeof(uint32_t));
+
+ CGDataProviderRef provider = CGDataProviderCreateWithFilename(path);
+ if (provider != NULL)
+ {
+ CGImageRef image = CGImageCreateWithPNGDataProvider(provider, NULL, false, kCGRenderingIntentDefault);
+ if (image != NULL)
{
- CGImageRef image = CGImageCreateWithPNGDataProvider(provider, NULL, false, kCGRenderingIntentDefault);
- if (image != NULL)
- {
- size_t imageWidth = CGImageGetWidth(image);
- size_t imageHeight = CGImageGetHeight(image);
-
- if (imageWidth != width || imageHeight != height) {
- fprintf(stderr, "Image %s is not of size %dx%d\n", path, width, height);
- exit(1);
- }
-
- // Create a new bitmap
-
- CGContextRef context = CGBitmapContextCreate(
- (void*) buffer,
- width,
- height,
- 8, // Bits per component
- width * 4, // Bytes per row
- CGImageGetColorSpace(image),
- kCGImageAlphaPremultipliedLast // RRRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA
- );
+ size_t imageWidth = CGImageGetWidth(image);
+ size_t imageHeight = CGImageGetHeight(image);
+
+ if (imageWidth != width || imageHeight != height) {
+ fprintf(stderr, "Image %s is not of size %dx%d\n", path, width, height);
+ exit(1);
+ }
+
+ // Create a new bitmap
+
+ CGContextRef context = CGBitmapContextCreate(
+ (void*) buffer,
+ width,
+ height,
+ 8, // Bits per component
+ width * 4, // Bytes per row
+ CGImageGetColorSpace(image),
+ kCGImageAlphaPremultipliedLast // RRRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA
+ );
- if (context != NULL)
- {
- CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, width, height), image);
- CGContextRelease(context);
- }
-
- CGImageRelease(image);
+ if (context != NULL)
+ {
+ CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, width, height), image);
+ CGContextRelease(context);
}
- CFRelease(provider);
+ CGImageRelease(image);
}
-
- // Compress the buffer
- uint32_t* compressedBuffer = (uint32_t*) malloc(width * height * sizeof(uint32_t) * 2);
- if (compressedBuffer == NULL) {
- printf("Can't allocate memory\n");
- exit(1);
- }
+ CFRelease(provider);
+ }
+
+ // Compress the buffer
+
+ uint32_t* compressedBuffer = (uint32_t*) malloc(width * height * sizeof(uint32_t) * 2);
+ if (compressedBuffer == NULL) {
+ printf("Can't allocate memory\n");
+ exit(1);
+ }
- uint32_t compressedLength = RLECompressPixels(compressedBuffer, buffer, width * height);
- printf("Compressed length = %d\n", compressedLength);
+ uint32_t compressedLength = AnimationCompressRunLengthEncodedPixels(compressedBuffer, buffer, width * height);
+ printf("Compressed length = %d\n", compressedLength);
#if 0
- printf("Compressed buffer:\n");
- for (int i = 0; i < compressedLength / sizeof(uint32_t); i++) {
- printf(" Buffer[%.4d] = 0x%.8x\n", i, compressedBuffer[i]);
- }
+ printf("Compressed buffer:\n");
+ for (int i = 0; i < compressedLength / sizeof(uint32_t); i++) {
+ printf(" Buffer[%.4d] = 0x%.8x\n", i, compressedBuffer[i]);
+ }
#endif
#if 1
- // Sanity check
-
- uint32_t* uncompressedBuffer = (uint32_t*) malloc(width * height * sizeof(uint32_t));
- if (uncompressedBuffer == NULL) {
- printf("Can't allocate memory\n");
- exit(1);
- }
-
- uint32_t uncompressedLength = RLEUncompressPixels(uncompressedBuffer, compressedBuffer, width * height);
- printf("Uncompressed length = %d\n", uncompressedLength);
-
- if (uncompressedLength != (width * height * sizeof(uint32_t))) {
- printf("Decompression fail. Data length not equal to original.\n");
- exit(1);
- }
+ // Sanity check
+
+ uint32_t* uncompressedBuffer = (uint32_t*) malloc(width * height * sizeof(uint32_t));
+ if (uncompressedBuffer == NULL) {
+ printf("Can't allocate memory\n");
+ exit(1);
+ }
- if (memcmp(buffer, uncompressedBuffer, width * height * sizeof(uint32_t)) != 0) {
- printf("Decompression fail. Buffers are not equal!\n");
- exit(1);
- }
+ AnimationDecompressRunLengthEncodedPixels(uncompressedBuffer, compressedBuffer, width * height);
+
+ if (memcmp(buffer, uncompressedBuffer, width * height * sizeof(uint32_t)) != 0) {
+ printf("Decompression fail. Buffers are not equal!\n");
+ exit(1);
+ }
#endif
-
- // Write the image header
-
- AnimationContainerImageHeader imageHeader;
- imageHeader.width = width;
- imageHeader.height = height;
- imageHeader.xoffset = 0;
- imageHeader.yoffset = 0;
- imageHeader.format = AnimationContainerImageFormatRunLengthCompressedPixels;
- imageHeader.dataLength = compressedLength;
-
- write(fd, &imageHeader, sizeof(imageHeader));
-
- // Write the compressed image data
-
- write(fd, compressedBuffer, compressedLength);
- free(compressedBuffer);
-
- // Write the padding if needed
-
- if ((compressedLength % 4) > 0) {
- write(fd, "\0\0\0", 4 - (compressedLength % 4));
- }
+
+ // Write the image header
+
+ AnimationContainerImageHeader imageHeader;
+ imageHeader.width = width;
+ imageHeader.height = height;
+ imageHeader.xoffset = 0;
+ imageHeader.yoffset = 0;
+ imageHeader.format = AnimationContainerImageFormatRunLengthCompressedPixels;
+ imageHeader.dataLength = compressedLength;
+
+ write(fd, &imageHeader, sizeof(imageHeader));
+
+ // Write the compressed image data
+
+ write(fd, compressedBuffer, compressedLength);
+ free(compressedBuffer);
+
+ // Write the padding if needed
+
+ if ((compressedLength % 4) > 0) {
+ write(fd, "\0\0\0", 4 - (compressedLength % 4));
}
-
- close(fd);
}
+
+ // Close the animation container
+
+ close(fd);
}

0 comments on commit 9333a77

Please sign in to comment.