GifByteType *buffer;
mm_util_debug("__convert_gif_to_rgba");
- if ((decoded->frames = (mm_util_gif_frame_data **) realloc(decoded->frames, sizeof(mm_util_gif_frame_data *)))
- == NULL) {
+ if ((decoded->data = (void *)malloc(width * height * 4)) == NULL) {
mm_util_error("Failed to allocate memory required, aborted.");
return MM_UTIL_ERROR_OUT_OF_MEMORY;
}
- if ((decoded->frames[0] = (mm_util_gif_frame_data *) calloc(1, sizeof(mm_util_gif_frame_data)))
- == NULL) {
- mm_util_error("Failed to allocate memory required, aborted.");
- return MM_UTIL_ERROR_OUT_OF_MEMORY;
- }
-
- if ((decoded->frames[0]->data = (void *)malloc(width * height * 4)) == NULL) {
- mm_util_error("Failed to allocate memory required, aborted.");
- return MM_UTIL_ERROR_OUT_OF_MEMORY;
- }
-
- buffer = (GifByteType *) decoded->frames[0]->data;
+ buffer = (GifByteType *) decoded->data;
for (i = 0; i < height; i++) {
gif_row = screen_buffer[i];
for (j = 0; j < width; j++) {
goto error;
}
- __convert_gif_to_rgba(decoded, ColorMap, screen_buffer, GifFile->SWidth, GifFile->SHeight);
+ ret = __convert_gif_to_rgba(decoded, ColorMap, screen_buffer, GifFile->SWidth, GifFile->SHeight);
+ if (ret != MM_UTIL_ERROR_NONE) {
+ mm_util_error("could not convert gif to rgba");
+ ret = MM_UTIL_ERROR_INVALID_OPERATION;
+ goto error;
+ }
ret = MM_UTIL_ERROR_NONE;
error:
return ret;
}
-static void __load_rgb_from_buffer(GifByteType *buffer, GifByteType **red, GifByteType **green, GifByteType **blue, unsigned long width, unsigned long height)
-{
- unsigned long i, j;
- unsigned long Size;
- GifByteType *redP, *greenP, *blueP;
-
- mm_util_debug("__load_rgb_from_buffer");
- Size = ((long)width) * height * sizeof(GifByteType);
-
- if ((*red = (GifByteType *) malloc((unsigned int)Size)) == NULL || (*green = (GifByteType *) malloc((unsigned int)Size)) == NULL || (*blue = (GifByteType *) malloc((unsigned int)Size)) == NULL) {
- mm_util_error("Failed to allocate memory required, aborted.");
- return;
- }
-
- redP = *red;
- greenP = *green;
- blueP = *blue;
-
- for (i = 0; i < height; i++) {
- for (j = 0; j < width; j++) {
- *redP++ = *buffer++;
- *greenP++ = *buffer++;
- *blueP++ = *buffer++;
- buffer++;
- }
- }
- return;
-}
-
-static int __save_buffer_to_gif(GifFileType *GifFile, GifByteType *OutputBuffer, ColorMapObject *OutputColorMap, mm_util_gif_frame_data * frame)
-{
- unsigned long i, j, pixels = 0;
- GifByteType *Ptr = OutputBuffer;
- static unsigned char extension[4] = {0, };
- int z;
- int transparent_index = 0, minDist = 256 * 256 * 256;
- GifColorType *color;
-
- mm_util_debug("__save_buffer_to_gif");
-
- if (GifFile == NULL) {
- mm_util_error("Invalid parameter: GifFileType");
- return MM_UTIL_ERROR_INVALID_PARAMETER;
- }
-
- /* Find closest color in first color map for the transparent color. */
- color = OutputColorMap->Colors;
- for (z = 0; z < OutputColorMap->ColorCount; z++) {
- int dr = ABS(color[z].Red - frame->transparent_color.Red);
- int dg = ABS(color[z].Green - frame->transparent_color.Green);
- int db = ABS(color[z].Blue - frame->transparent_color.Blue);
-
- int dist = dr * dr + dg * dg + db * db;
-
- if (minDist > dist) {
- minDist = dist;
- transparent_index = z;
- }
- }
-
- extension[0] = frame->disposal_mode << 2;
- if (frame->is_transparent) {
- extension[0] |= 1;
- extension[3] = transparent_index;
- } else {
- extension[3] = -1;
- }
- extension[1] = frame->delay_time % 256;
- extension[2] = frame->delay_time / 256;
-
- /* Dump graphics control block. */
- EGifPutExtension(GifFile, GRAPHICS_EXT_FUNC_CODE, 4, extension);
-
- if (EGifPutImageDesc(GifFile, frame->x, frame->y, frame->width, frame->height, false, OutputColorMap) == GIF_ERROR) {
- mm_util_error("could not put image description");
- return MM_UTIL_ERROR_INVALID_OPERATION;
- }
-
- for (i = 0; i < frame->height; i++) {
- for (j = 0; j < frame->width; j++) {
- unsigned char c = Ptr[pixels++];
- if (EGifPutPixel(GifFile, c) == GIF_ERROR) {
- mm_util_error("could not put pixel");
- return MM_UTIL_ERROR_INVALID_OPERATION;
- }
- }
- }
-
- return MM_UTIL_ERROR_NONE;
-}
-
static int __write_function(GifFileType *gft, const GifByteType *data, int size)
{
write_data *write_data_ptr = (write_data *) gft->UserData;
return MM_UTIL_ERROR_NONE;
}
-static int __write_gif(mm_util_gif_data *encoded)
-{
- int ColorMapSize;
- unsigned int i = encoded->current_count;
- GifByteType *red = NULL, *green = NULL, *blue = NULL, *OutputBuffer = NULL;
- ColorMapObject *OutputColorMap = NULL;
-
- if (encoded->GifFile == NULL) {
- mm_util_error("Invalid parameter");
- return MM_UTIL_ERROR_INVALID_PARAMETER;
- }
-
- if (!encoded->screen_desc_updated) {
- if (EGifPutScreenDesc(encoded->GifFile, encoded->frames[0]->width, encoded->frames[0]->height, 8, 0, NULL) == GIF_ERROR) {
- mm_util_error("could not put screen description");
- mm_util_encode_close_gif(encoded);
- return MM_UTIL_ERROR_INVALID_OPERATION;
- }
- encoded->screen_desc_updated = true;
- }
-
- for (i = encoded->current_count; i < encoded->image_count; i++) {
- /* allocate output buffer */
- if ((OutputBuffer = (GifByteType *) malloc(encoded->frames[i]->width * encoded->frames[i]->height * sizeof(GifByteType))) == NULL) {
- mm_util_error("Failed to allocate memory required, aborted.");
- mm_util_encode_close_gif(encoded);
- return MM_UTIL_ERROR_INVALID_OPERATION;
- }
-
- /* make colormap and quantization for the color table of gif */
- ColorMapSize = 1 << 8;
- __load_rgb_from_buffer((GifByteType *) encoded->frames[i]->data, &red, &green, &blue, encoded->frames[i]->width, encoded->frames[i]->height);
-
- if ((OutputColorMap = GifMakeMapObject(ColorMapSize, NULL)) == NULL) {
- mm_util_error("could not map object");
- RGB_FREE(red, green, blue);
- MMUTIL_SAFE_FREE(OutputBuffer);
- mm_util_encode_close_gif(encoded);
- return MM_UTIL_ERROR_INVALID_OPERATION;
- }
- if (GifQuantizeBuffer(encoded->frames[i]->width, encoded->frames[i]->height, &ColorMapSize, red, green, blue, OutputBuffer, OutputColorMap->Colors) == GIF_ERROR) {
- mm_util_error("could not quantize buffer");
- RGB_FREE(red, green, blue);
- MMUTIL_SAFE_FREE(OutputBuffer);
- GifFreeMapObject(OutputColorMap);
- mm_util_encode_close_gif(encoded);
- return MM_UTIL_ERROR_INVALID_OPERATION;
- }
- RGB_FREE(red, green, blue);
-
- encoded->frames[i]->transparent_color.Red = 0xff;
- encoded->frames[i]->transparent_color.Green = 0xff;
- encoded->frames[i]->transparent_color.Blue = 0xff;
-
- if (!encoded->frames[i]->x && !encoded->frames[i]->y) {
- encoded->frames[i]->x = (encoded->frames[0]->width - encoded->frames[i]->width) / 2;
- encoded->frames[i]->y = (encoded->frames[0]->height - encoded->frames[i]->height) / 2;
- }
-
- encoded->frames[i]->disposal_mode = MM_UTIL_GIF_DISPOSAL_UNSPECIFIED;
- encoded->frames[i]->is_transparent = false;
-
- /* encode OutputBuffer to GifFile */
- if (__save_buffer_to_gif(encoded->GifFile, OutputBuffer, OutputColorMap, encoded->frames[i]) != MM_UTIL_ERROR_NONE) {
- mm_util_error("save_buffer_to_gif is failed");
- MMUTIL_SAFE_FREE(OutputBuffer);
- GifFreeMapObject(OutputColorMap);
- mm_util_encode_close_gif(encoded);
- return MM_UTIL_ERROR_INVALID_OPERATION;
- }
-
- MMUTIL_SAFE_FREE(OutputBuffer);
- GifFreeMapObject(OutputColorMap);
- encoded->current_count++;
- }
- encoded->size = encoded->write_data_ptr.size;
-
- mm_util_encode_close_gif(encoded);
- return MM_UTIL_ERROR_NONE;
-}
-
-int mm_util_encode_gif(mm_util_gif_data *encoded)
-{
- int ret;
-
- mm_util_debug("mm_util_encode_gif");
- ret = __write_gif(encoded);
-
- return ret;
-}
-
-#ifdef GIF_ENCODER_V2
static const int DEFAULT_COLORMAP_SIZE = (1 << 8);
int _gif_image_alloc_glob_ext_block(GifFileType *gif_file, int function, int byte_count, ExtensionBlock **ext_block);
return MM_UTIL_ERROR_NONE;
}
-#if 0
-int mm_util_gif_encode_set_bg_color(mm_gif_file_h gif_file_h, const mm_util_gif_color_s bg_color)
-{
- gif_file_s *gif_file = (gif_file_s *)gif_file_h;
- GifColorType *colors = NULL;
- int color_count = 1 << gif_file->color_res;
-
- mm_util_retvm_if(gif_file == NULL, MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid parameter");
-
- colors = (GifColorType *)calloc(1, color_count * sizeof(GifColorType));
- mm_util_retvm_if(colors == NULL, MM_UTIL_ERROR_OUT_OF_MEMORY, "memory allocation failed");
-
- colors[0].Red = bg_color.red;
- colors[0].Green = bg_color.green;
- colors[0].Blue = bg_color.blue;
-
- gif_file->color_map = GifMakeMapObject(color_count, colors);
- mm_util_retvm_if(gif_file->color_map == NULL, MM_UTIL_ERROR_INVALID_OPERATION, "could not make color map");
-
- gif_file->background_color = 0; /* index of color map */
-
- return MM_UTIL_ERROR_NONE;
-}
-#endif
-
int mm_util_gif_encode_set_repeat(mm_gif_file_h gif_file_h, gboolean repeat_mode, const unsigned short repeat_count)
{
gif_file_s *gif_file = (gif_file_s *)gif_file_h;
MMUTIL_SAFE_FREE(gif_file->filename);
MMUTIL_SAFE_FREE(gif_file->buffer);
}
-#endif
#define ENCODED_FILEPATH "/opt/usr/home/owner/media/mm_util_test.gif"
#define ANIMATED_FRAME_MAX 100
-#ifdef GIF_ENCODER_V2
static gboolean g_encode_mem = FALSE;
-#endif
typedef struct {
char file_name[PATH_MAX];
mm_util_gif_data decoded;
} gif_test_data_s;
-#ifdef GIF_ENCODER_V2
+
static int _write_file(const char *file_name, void *data, size_t data_size)
{
FILE *fp = NULL;
return TRUE;
}
-#endif
static inline void flush_stdin()
{
if (argc < 2) {
fprintf(stderr, "\t[usage]\n");
fprintf(stderr, "\t\t1. decode : %s decode filepath\n", argv[0]);
-#ifdef GIF_ENCODER_V2
fprintf(stderr, "\t\t2. encode-agif/encode-mem-agif : %s encode-agif dirpath\n", argv[0]);
-#endif
return 0;
}
return 0;
}
nfiles++;
- }
-#ifdef GIF_ENCODER_V2
- else if (!strcmp("encode-agif", argv[1]) || !strcmp("encode-mem-agif", argv[1])) {
+ } else if (!strcmp("encode-agif", argv[1]) || !strcmp("encode-mem-agif", argv[1])) {
if (!strcmp("encode-mem-agif", argv[1]))
g_encode_mem = TRUE;
}
}
}
- }
-#endif
- else {
+ } else {
fprintf(stderr, "\tunknown command [%s]\n", argv[1]);
return 0;
}
}
}
-#ifdef GIF_ENCODER_V2
mm_gif_file_h gif_file = NULL;
mm_gif_image_h gif_image = NULL;
unsigned char *encoded_gif_mem = NULL;
fprintf(stderr, "\t mm_util_gif_enc_set_resolution [%d]\n", ret);
/* repeat */
for (i = 0; i < nfiles; i++) {
- if (files[i].decoded.frames) {
+ if (files[i].decoded.data) {
ret = mm_util_gif_image_create(gif_file, &gif_image);
fprintf(stderr, "\t mm_util_gif_image_create [%d]\n", ret);
- ret = mm_util_gif_image_set_image(gif_image, files[i].decoded.frames[0]->data);
+
+ ret = mm_util_gif_image_set_image(gif_image, files[i].decoded.data);
fprintf(stderr, "\t mm_util_gif_image_set_image [%d]\n", ret);
+
ret = mm_util_gif_image_set_delay_time(gif_image, 20);
fprintf(stderr, "\t mm_util_gif_image_set_delay_time [%d]\n", ret);
+
ret = mm_util_gif_image_set_position(gif_image, 0, 0, files[i].decoded.width, files[i].decoded.height);
fprintf(stderr, "\t mm_util_gif_image_set_position [%d]\n", ret);
+
ret = mm_util_gif_image_set_disposal_mode(gif_image, MM_UTIL_GIF_DISPOSAL_UNSPECIFIED);
fprintf(stderr, "\t mm_util_gif_image_set_disposal_mode [%d]\n", ret);
-#if 0 /* for advanced */
- if (files[i].decoded.frames[0]->is_transparent) {
- mm_util_gif_color_s tp_color;
- tp_color.red = files[i].decoded.frames[0]->transparent_color.Red;
- tp_color.green = files[i].decoded.frames[0]->transparent_color.Green;
- tp_color.blue = files[i].decoded.frames[0]->transparent_color.Blue;
- ret = mm_util_gif_image_set_tp_color(gif_image, tp_color);
- fprintf(stderr, "\t mm_util_gif_image_set_tp_color [%d]\n", ret);
- }
-#endif
+
ret = mm_util_gif_encode_add_image(gif_file, gif_image);
fprintf(stderr, "\t mm_util_gif_enc_add_image [%d]\n", ret);
- mm_util_gif_image_destory(gif_image);
- free(files[i].decoded.frames[0]->data);
- free(files[i].decoded.frames);
+ mm_util_gif_image_destory(gif_image);
+ free(files[i].decoded.data);
}
}
/* repeat */
if (g_encode_mem)
_write_file(ENCODED_FILEPATH, (void *)encoded_gif_mem, (size_t)encoded_gif_size);
mm_util_gif_encode_destroy(gif_file);
-#else
- if (files[0].decoded.frames) {
- fprintf(stderr, "\t##Decoded data##: %p\t width: %lu\t height:%lu\t size: %llu\n", files[0].decoded.frames[0], files[0].decoded.width, files[0].decoded.height, files[0].decoded.size);
- files[0].decoded.image_count = 1;
- ret = mm_util_encode_open_gif_file(&files[0].decoded, ENCODED_FILEPATH);
- ret = mm_util_encode_gif(&files[0].decoded);
- ret = mm_util_encode_close_gif(&files[0].decoded);
-
- free(files[0].decoded.frames[0]->data);
- free(files[0].decoded.frames);
- } else {
- fprintf(stderr, "\tDECODED data is NULL\n");
- }
-#endif
+
return 0;
}