From 2b9539c1ff80299b076dae102fc430786a86bae9 Mon Sep 17 00:00:00 2001
From: scuri <scuri>
Date: Tue, 18 Aug 2009 05:13:18 +0000
Subject: *** empty log message ***

---
 html/en/history.html           |   3 +
 html/examples/animate_gif.wlua | 188 ++++++++++++++++++++++++++++-------------
 src/im_convertcolor.cpp        |   6 +-
 3 files changed, 137 insertions(+), 60 deletions(-)

diff --git a/html/en/history.html b/html/en/history.html
index 528f656..7d48c83 100644
--- a/html/en/history.html
+++ b/html/en/history.html
@@ -16,6 +16,9 @@
     <ul>
         <li><span style="color: #0000FF">New:</span> function <strong>
 		imImageGetOpenGLData</strong>.</li>
+		<li><span style="color: #0000FF">New:</span> functions <strong>
+		im.ConvertDataTypeNew</strong>, <strong>im.ConvertColorSpaceNew</strong> 
+		and <strong>im.ConvertToBitmapNew</strong> in Lua.</li>
 		<li><span style="color: #008000">Changed:</span> libPNG updated to version 
 	1.2.37. Removed changes to the library that made it incompatible with other 
 		libPNG distributions.</li>
diff --git a/html/examples/animate_gif.wlua b/html/examples/animate_gif.wlua
index de96d48..4334f37 100644
--- a/html/examples/animate_gif.wlua
+++ b/html/examples/animate_gif.wlua
@@ -1,90 +1,160 @@
+-- Based on a code from Stuart P. Bentley
+
 require"imlua"
 require"cdlua"
 require"cdluaim"
 require"iuplua"
 require"iupluacd"
+require"iupluaimglib"
 
-gimgs={}
-gimgs.delays={}
-
-ggif=im.FileOpen(iup.GetFile("*.*"))
+images={}
+frame=1
 
-for i=1, select(3,ggif:GetInfo()) do
-  gimgs[i]=ggif:LoadImage(i-1)
-  err, gimgs[i] = im.ConvertColorSpaceNew(gimgs[i], im.RGB, true)
-  local delay=gimgs[i]:GetAttribute "Delay"
-  if delay then
-    gimgs.delays[i]=delay[1]*10
+function print_error(err)
+  local msg = {}
+  msg[im.ERR_OPEN] = "Error Opening File."
+  msg[im.ERR_MEM] = "Insuficient memory."
+  msg[im.ERR_ACCESS] = "Error Accessing File."
+  msg[im.ERR_DATA] = "Image type not Suported."
+  msg[im.ERR_FORMAT] = "Invalid Format."
+  msg[im.ERR_COMPRESS] = "Invalid or unsupported compression."
+  
+  if msg[err] then
+    print(msg[err])
   else
-    gimgs.delays[i]=10
+    print("Unknown Error.")
   end
 end
 
-ggif:Close()
-
-function gifanimator(gimgs, behavior, start, final, runyn, initial)
-
-  start=start or 1
-  final=final or #gimgs
-  local increment=1
-  local frame=initial or start
-
-  --hack to get data to canvas action
-  anii=frame
-  return iup.timer{
-    time=gimgs.delays[frame],
-    run=runyn or "YES",
-    action_cb=function(self)
-      self.run="NO"
-      if frame==final then
-        if behavior=="LOOP" then
-          frame=start
-        elseif behavior=="BOUNCE" then
-          increment=-1
-          frame=frame+increment
-        else
-          return nil
-        end
-      elseif frame==start and behavior=="BOUNCE" then
-        increment=1
-        frame=frame+increment
+function load_frames(file_name)
+
+  ifile, err=im.FileOpen(file_name)
+  if not ifile then
+      print_error(err)
+      return
+  end
+
+  images={}
+  images.delays={}
+  images.disposal={}
+  frame=1
+
+  for i=1, select(3,ifile:GetInfo()) do
+    images[i]=ifile:LoadBitmap(i-1)
+    err, images[i] = im.ConvertColorSpaceNew(images[i], im.RGB, true)
+    local delay=images[i]:GetAttribute("Delay") -- time to wait betweed frames in 1/100 of a second]
+    if delay then
+      images.delays[i]=delay[1]*10 -- timer in miliseconds
+    else
+      if (i == 1) then 
+        images.delays[i]=100 
       else
-        frame=frame+increment
+        images.delays[i]=images.delays[i-1]
       end
-      self.time=gimgs.delays[frame]
-      self.run="YES"
-
-      --for the canvas redrawing function
-      anii=frame
-      --redraw the canvas. yeah, it's a bit of a hack.
-      cnv.action()
-    end}
+    end
+    images.disposal[i]=images[i]:GetAttribute("Disposal", true) --  [UNDEF, LEAVE, RBACK, RPREV]
+  end
+
+  ifile:Close()
+  
+  cnv.rastersize = images[1]:Width().."x"..images[1]:Height()
+  dlg.size=nil
+  iup.Refresh(cnv)
 end
 
-anit=gifanimator(gimgs,"LOOP")--,"BOUNCE",3,20)
+t = iup.timer{}
+
+function start_timer()
+  dlg.title = "Animated Gif"
+  dlg.play_bt.image="IUP_MediaPause" dlg.play_bt.title="Pause"
+  t.run = "NO"
+  t.time = images.delays[frame]
+  t.run = "YES"
+  iup.Update(cnv)
+end
 
-cnv = iup.canvas{rastersize = gimgs[1]:Width().."x"..gimgs[1]:Height(),
- border = "NO",
- expand = "YES"}
+function stop_timer()
+  dlg.title = "Animated Gif "..frame.."/"..#images
+  dlg.play_bt.image="IUP_MediaPlay" dlg.play_bt.title="Play" 
+  t.run="NO"
+  iup.Update(cnv)
+end
+
+function set_frame(f)
+  frame = f
+  if frame > #images then
+    frame = #images
+  end
+  if frame < 1 then
+    frame = 1
+  end
+  stop_timer()
+end
+
+function t:action_cb()
+  frame = frame + 1
+  if frame == #images+1 then
+    frame = 1
+  end
+  
+  start_timer()
+end
+    
+cnv = iup.canvas{border = "NO"}
 
 function cnv:map_cb()-- the CD canvas can only be created when the IUP canvas is mapped
-  cdanvas = cd.CreateCanvas(cd.IUP, self)
+  canvas = cd.CreateCanvas(cd.IUP, self)
+  canvas:Activate()
+  
+  start_timer()
 end
 
 function cnv:action()-- called everytime the IUP canvas needs to be repainted
-  cdanvas:Activate()
-  gimgs[anii]:cdCanvasPutImageRect(cdanvas, 0, 0, 0, 0, 0, 0, 0, 0) -- use default values
+  canvas:Activate()
+  if (images.disposal[frame] == "RBACK") then
+    canvas:Clear()
+  end
+  images[frame]:cdCanvasPutImageRect(canvas, 0, 0, 0, 0, 0, 0, 0, 0) -- use default values
 end
 
-dlg = iup.dialog{cnv,title="Animated Gif Player"}
+function cnv:resize_cb()
+  canvas:Activate()
+  canvas:Clear()
+end
+
+function cnv:button_cb(but, pressed)
+  if (but == iup.BUTTON1 and pressed==1) then
+    local file_name, error = iup.GetFile("*.*")
+    if error ~= 0 then
+      return
+    end
+    
+    load_frames(file_name)  
+    canvas:Activate()
+    canvas:Clear()
+    start_timer()
+  end
+end
+
+buts = iup.hbox{
+  iup.button{title="First", image="IUP_MediaGotoBegin", action=function(self) set_frame(1) end}, 
+  iup.button{title="Previous", image="IUP_MediaRewind", action=function(self) set_frame(frame-1) end}, 
+  iup.button{title="Pause", image="IUP_MediaPause", action=function(self) if (t.run=="YES") then stop_timer() else start_timer() end end}, 
+  iup.button{title="Next", image="IUP_MediaForward", action=function(self) set_frame(frame+1) end}, 
+  iup.button{title="Last", image="IUP_MediaGoToEnd", action=function(self) set_frame(#images) end}, 
+  }
+dlg = iup.dialog{iup.vbox{cnv, buts},title="Animated Gif", margin="5x5", gap=10}
+dlg.play_bt = dlg[1][2][3]
 
 function dlg:close_cb()
-  iup.Destroy(anit)
-  gimgs=nil --Destroys will be called by the garbage collector
-  cdanvas:Kill()
+  iup.Destroy(t)
+  images=nil --Destroys will be called by the garbage collector
+  canvas:Kill()
   self:destroy()
   return iup.IGNORE -- because we destroy the dialog
 end
 
+load_frames(iup.GetFile("*.*"))
+
 dlg:show()
 iup.MainLoop()
diff --git a/src/im_convertcolor.cpp b/src/im_convertcolor.cpp
index 8e96ccc..e68730f 100644
--- a/src/im_convertcolor.cpp
+++ b/src/im_convertcolor.cpp
@@ -2,7 +2,7 @@
  * \brief Image Conversion
  *
  * See Copyright Notice in im_lib.h
- * $Id: im_convertcolor.cpp,v 1.2 2009/08/18 02:23:33 scuri Exp $
+ * $Id: im_convertcolor.cpp,v 1.3 2009/08/18 05:13:18 scuri Exp $
  */
 
 #include "im.h"
@@ -963,6 +963,8 @@ int imConvertColorSpace(const imImage* src_image, imImage* dst_image)
       imbyte* transp_color = (imbyte*)imImageGetAttribute(src_image, "TransparencyColor", NULL, NULL);
       if (transp_color)
         iConvertSetTranspColor((imbyte**)dst_image->data, dst_image->count, *(transp_color+0), *(transp_color+1), *(transp_color+2));
+      else
+        memset(dst_image->data[3], 255, dst_image->count);
     }
     else
     {
@@ -973,6 +975,8 @@ int imConvertColorSpace(const imImage* src_image, imImage* dst_image)
         iConvertSetTranspMap((imbyte*)src_image->data[0], (imbyte*)dst_image->data[3], dst_image->count, transp_map, transp_count);
       else if (transp_index)
         iConvertSetTranspIndex((imbyte*)src_image->data[0], (imbyte*)dst_image->data[3], dst_image->count, *transp_index);
+      else
+        memset(dst_image->data[3], 255, dst_image->count);
     }
   }
 
-- 
cgit v1.2.3