Huge change, and it's almost working, too :)
[libopano.git] / src / utils_image.c
diff --git a/src/utils_image.c b/src/utils_image.c
new file mode 100644 (file)
index 0000000..fe4f1a7
--- /dev/null
@@ -0,0 +1,143 @@
+/*
+ *  Copyright (C) 2007  Florian octo Forster <octo at verplant.org>
+ *
+ *  This program is free software; you can redistribute it and/or modify it
+ *  under the terms of the GNU General Public License as published by the Free
+ *  Software Foundation; either version 2 of the License, or (at your option)
+ *  any later version.
+ *
+ *  This program is distributed in the hope that it will be useful, but
+ *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ *  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ *  for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc., 51
+ *  Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ **/
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
+#include <wand/magick-wand.h>
+
+#include "utils_image.h"
+
+ui_image_t *ui_create (uint32_t width, uint32_t height)
+{
+  ui_image_t *img = NULL;
+
+  img = (ui_image_t *) malloc (sizeof (ui_image_t));
+  if (img == NULL)
+    return (NULL);
+  memset (img, '\0', sizeof (ui_image_t));
+
+  img->width = width;
+  img->height = height;
+  img->type = UIT_RGB;
+
+  img->data = (uint8_t **) malloc (3 * sizeof (uint8_t *));
+  if (img->data == NULL)
+  {
+    free (img);
+    return (NULL);
+  }
+
+  /* FIXME: s/4/3 when bug is fixed! */
+  fprintf (stderr, "Theres still a bug in %s, line %i!\n",
+      __FILE__, __LINE__);
+  img->data[0] = (uint8_t *) malloc (4 * width * height * sizeof (uint8_t));
+  if (img->data[0] == NULL)
+  {
+    free (img->data);
+    free (img);
+    return (NULL);
+  }
+  memset (img->data[0], '\0', 3 * width * height * sizeof (uint8_t));
+  /* FIXME: Remove +16 */
+  img->data[1] = img->data[0] + (width * height);
+  img->data[2] = img->data[1] + (width * height);
+
+  return (img);
+} /* ui_image_t *ui_create */
+
+ui_image_t *ui_create_file (const char *file)
+{
+  MagickWand *mwand; 
+  MagickBooleanType status;
+
+  ui_image_t *img = NULL;
+
+  uint32_t width;
+  uint32_t height;
+  int i;
+
+  mwand = NewMagickWand ();
+  if (mwand == NULL)
+    return (NULL);
+
+  status = MagickReadImage (mwand, file);
+  if (status == MagickFalse)
+  {
+    DestroyMagickWand (mwand);
+    return (NULL);
+  }
+
+  width = (uint32_t) MagickGetImageWidth (mwand);
+  height = (uint32_t) MagickGetImageHeight (mwand);
+
+  img = ui_create (width, height);
+  if (img == NULL)
+  {
+    DestroyMagickWand (mwand);
+    return (NULL);
+  }
+
+  for (i = 0; i < 3; i++)
+  {
+    char *map;
+
+    switch (i)
+    {
+      case 0: map = "R"; break;
+      case 1: map = "G"; break;
+      case 2: map = "B"; break;
+      default: map = "*invalid*";
+    };
+
+    status = MagickGetImagePixels (mwand,
+       0, 0,
+       img->width, img->height,
+       map, CharPixel, (void *) img->data[i]);
+    if (status == MagickFalse)
+      break;
+  } /* for (i = 0, 1, 2) */
+  if (status == MagickFalse)
+  {
+    ui_destroy (img);
+    img = NULL;
+  }
+
+  DestroyMagickWand (mwand);
+  
+  return (img);
+} /* ui_image_t *ui_create_file */
+
+void ui_destroy (ui_image_t *img)
+{
+  if (img != NULL)
+  {
+    if (img->data != NULL)
+    {
+      if (img->data[0] != NULL)
+       free (img->data[0]);
+      free (img->data);
+    }
+    free (img);
+  }
+} /* void ui_destroy */
+
+/*
+ * vim: set tabstop=8 softtabstop=2 shiftwidth=2 :
+ */