Permalink
Browse files

use scope guard in the gif encoder

  • Loading branch information...
1 parent 7be01eb commit 42cb392df0a68d0b42267e2719f2a24d783d119a @pkrumins pkrumins committed Aug 13, 2010
Showing with 9 additions and 34 deletions.
  1. +9 −34 src/gif_encoder.cpp
View
@@ -3,6 +3,8 @@
#include <cstdlib>
#include <cstring>
+#include "loki/ScopeGuard.h"
+
#include "gif_encoder.h"
#include "palette.h"
#include "quantize.h"
@@ -148,48 +150,36 @@ GifEncoder::encode()
int color_map_size = 256;
ColorMapObject *output_color_map = MakeMapObject(256, ext_web_safe_palette);
- if (!output_color_map) {
+ LOKI_ON_BLOCK_EXIT(FreeMapObject, output_color_map);
+ if (!output_color_map)
throw "MakeMapObject in GifEncoder::encode failed";
- }
GifByteType *gif_buf = (GifByteType *)malloc(sizeof(GifByteType)*width*height);
- if (!gif_buf) {
- FreeMapObject(output_color_map);
+ LOKI_ON_BLOCK_EXIT(free, gif_buf);
+ if (!gif_buf)
throw "malloc in GifEncoder::encode failed";
- }
if (web_safe_quantize(width, height, rgb.red, rgb.green, rgb.blue, gif_buf) == GIF_ERROR)
- {
- FreeMapObject(output_color_map);
- free(gif_buf);
throw "web_safe_quantize in GifEncoder::encode failed";
- }
/*
if (QuantizeBuffer(width, height, &color_map_size,
rgb.red, rgb.green, rgb.blue,
gif_buf, output_color_map->Colors) == GIF_ERROR)
{
- FreeMapObject(output_color_map);
- free(gif_buf);
throw "QuantizeBuffer in GifEncoder::encode failed";
}
*/
GifFileType *gif_file = EGifOpen(&gif, gif_writer);
- if (!gif_file) {
- FreeMapObject(output_color_map);
- free(gif_buf);
+ LOKI_ON_BLOCK_EXIT(EGifCloseFile, gif_file);
+ if (!gif_file)
throw "EGifOpen in GifEncoder::encode failed";
- }
EGifSetGifVersion("89a");
if (EGifPutScreenDesc(gif_file, width, height,
color_map_size, 0, output_color_map) == GIF_ERROR)
{
- FreeMapObject(output_color_map);
- free(gif_buf);
- EGifCloseFile(gif_file);
throw "EGifPutScreenDesc in GifEncoder::encode failed";
}
@@ -206,26 +196,15 @@ GifEncoder::encode()
}
if (EGifPutImageDesc(gif_file, 0, 0, width, height, FALSE, NULL) == GIF_ERROR) {
- FreeMapObject(output_color_map);
- free(gif_buf);
- EGifCloseFile(gif_file);
throw "EGifPutImageDesc in GifEncoder::encode failed";
}
GifByteType *gif_bufp = gif_buf;
for (int i = 0; i < height; i++) {
- if (EGifPutLine(gif_file, gif_bufp, width) == GIF_ERROR) {
- FreeMapObject(output_color_map);
- free(gif_buf);
- EGifCloseFile(gif_file);
+ if (EGifPutLine(gif_file, gif_bufp, width) == GIF_ERROR)
throw "EGifPutLine in GifEncoder::encode failed";
- }
gif_bufp += width;
}
-
- FreeMapObject(output_color_map);
- free(gif_buf);
- EGifCloseFile(gif_file);
}
void
@@ -300,11 +279,7 @@ AnimatedGifEncoder::new_frame(unsigned char *data, int delay)
RGBator rgb(data, width, height, buf_type);
if (web_safe_quantize(width, height, rgb.red, rgb.green, rgb.blue, gif_buf) == GIF_ERROR)
- {
- FreeMapObject(output_color_map);
- free(gif_buf);
throw "web_safe_quantize in AnimatedGifEncoder::new_frame failed";
- }
/*
if (QuantizeBuffer(width, height, &color_map_size,

0 comments on commit 42cb392

Please sign in to comment.