00001
00002
00003
00004
00005 from util import *
00006 from tempimage import *
00007 from imagesettings import *
00008 from referencefile import *
00009
00010 class GIFProcessor:
00011 """Handles GIF file generation and it's settings.
00012 """
00013
00014 def __init__(self):
00015 """Constructor.
00016
00017 Data members:
00018 Interlace : Generate interlaced GIF images?
00019 BitDepth : Minimum bit depth to generate images with.
00020 Optimizations : Level of optimization to use:
00021 0 - none
00022 1 - lossless optimizations (same results as with none)
00023 2 - some optimizations
00024 3 - more optimizations
00025 4 - all optimizations
00026 """
00027 self.set_all("light")
00028
00029 def set_all(self, all):
00030 """Sets all GIF settings according to given mode.
00031
00032 fastest means all optimizations and least generated files
00033 extreme means no optimizations and all generated files
00034 heavy means lossless optimizations only - i.e. same result as extreme
00035 """
00036 if all == "extreme":
00037 self.Interlace = True
00038 self.Optimizations = 0
00039 self.BitDepth = 1
00040 elif all == "heavy":
00041 self.Interlace = True
00042 self.Optimizations = 1
00043 self.BitDepth = 1
00044 elif all == "medium":
00045 self.Interlace = False
00046 self.Optimizations = 2
00047 self.BitDepth = 3
00048 elif all == "light":
00049 self.Interlace = False
00050 self.Optimizations = 3
00051 self.BitDepth = 5
00052 elif all == "fastest":
00053 self.Interlace = False
00054 self.Optimizations = 4
00055 self.BitDepth = 7
00056 else:
00057 warning("wrong value passed by --gifall : using default")
00058
00059 def set_optimizations(self, opt):
00060 """Set optimization level to use.
00061 """
00062 try:
00063 self.Optimizations = clamp(int(opt),0,5)
00064 except ValueException:
00065 warning("wrong value passed by --gifopt : using default")
00066
00067 def set_interlace(self):
00068 """Turn generation of interlaced GIF images on.
00069 """
00070 self.Interlace = True
00071
00072 def set_min_depth(self, d):
00073 """Sets minimum bit depth to use.
00074 """
00075 try:
00076 if(int(d) in range(1,9)):
00077 self.BitDepth = int(d)
00078 else:
00079 warning("wrong value passed by --g-depth : using default")
00080 except ValueException:
00081 warning("wrong value passed by --g-depth : using default")
00082
00083 def get_interlace_vals(self):
00084 """Parses stored interlace data, returns list of used interlace values.
00085 """
00086 out = [False]
00087 if self.Interlace:
00088 out.append(True)
00089 return out
00090
00091 def get_bitdepth_vals(self):
00092 """Parses stored bitdepth data, returns list of used bitdepth values.
00093 """
00094 return range(self.BitDepth, 9)
00095
00096 def get_vals_str(self, str):
00097 """Returns all possible values of setting requested by input string.
00098 """
00099 if str == "interlace":
00100 return self.get_interlace_vals()
00101 if str == "bitdepth":
00102 return self.get_bitdepth_vals()
00103 else:
00104 warning("unknown value passed to GIFProcessor.get_vals_str()")
00105
00106 def get_max_files(self):
00107 """Returns maximum number of files generated in optimization.
00108
00109 Used for % feedback.
00110 """
00111 if self.Interlace:
00112 return 2 * len(self.get_bitdepth_vals())
00113 return len(self.get_bitdepth_vals())
00114
00115 def process(self, reffile, filenoext, queue=None, proc=0):
00116 """Generate GIF files according to settings and return a list of them.
00117 """
00118 output(str(proc) + " processing gif")
00119 outfilelist = []
00120
00121 filecount = 0.0
00122 for depth in self.get_bitdepth_vals():
00123 for inter in self.get_interlace_vals():
00124 outfilelist.append(TempImage(globals.TEMPDIR + filenoext +
00125 str(inter) + str(depth) + ".gif", "gif",
00126 GIFSettings(inter, depth), reffile))
00127 filecount += 1.0
00128 output("gif " + str(proc) + " " + str(filecount /
00129 self.get_max_files() * 100) + " %", 3)
00130 if queue is not None:
00131 queue.put(outfilelist)
00132 else:
00133 return outfilelist