00001
00002
00003
00004
00005 from tempimage import *
00006 from imagesettings import *
00007 from referencefile import *
00008 class JPGProcessor:
00009 """Handles JPG file generation and it's settings.
00010 """
00011 def __init__(self):
00012 """Constructor.
00013
00014 Data members:
00015 MinQuality : Minimal JPG quality to use. Can be int 1-100.
00016 Step : Step to increase quality by when generating files.
00017 Interlace : Generate interlaced JPG images ?
00018 Optimize : Generate images with optimized JPG compression?
00019 SFactors : Subsampling factors to use when generating images.
00020 "standard" means common factors 1x1,2x1 and 2x2 are used.
00021 "all" means that all available factors are used.
00022 Otherwise this is a string of comma separated factors
00023 in axb format.
00024 Available factors are:
00025 1x1 (no subsampling)
00026 1x2
00027 1x4
00028 2x1
00029 2x2
00030 2x4
00031 4x1
00032 4x2
00033 Grayscale : Generate grayscale JPG images ?
00034 Optimizations : Level of optimization to use:
00035 0 - none
00036 1 - lossless optimizations (same results as with none)
00037 2 - some optimizations
00038 3 - more optimizations
00039 4 - all optimizations
00040 """
00041 self.set_all("light")
00042
00043 def set_all(self, all):
00044 """Sets all JPG settings according to given mode.
00045
00046 fastest means all optimizations and least generated files
00047 extreme means no optimizations and all generated files
00048 heavy means lossless optimizations only - i.e. same result as extreme
00049 """
00050 if all == "extreme":
00051 self.MinQuality = 1
00052 self.Step = 1
00053 self.Interlace = True
00054 self.Optimize = True
00055 self.SFactors = "all"
00056 self.Optimizations = 0
00057 self.Grayscale = True
00058 elif all == "heavy":
00059
00060 self.MinQuality = 1
00061 self.Step = 1
00062 self.Interlace = True
00063 self.Optimize = True
00064 self.SFactors = "all"
00065 self.Optimizations = 1
00066 self.Grayscale = True
00067 elif all == "medium":
00068
00069 self.MinQuality = 1
00070 self.Step = 3
00071 self.Interlace = True
00072 self.Optimize = True
00073 self.SFactors = "1x1,2x1,2x2,2x4,4x2"
00074 self.Optimizations = 2
00075 self.Grayscale = True
00076 elif all == "light":
00077
00078 self.MinQuality = 1
00079 self.Step = 6
00080 self.Interlace = True
00081 self.Optimize = True
00082 self.SFactors = "standard"
00083 self.Optimizations = 3
00084 self.Grayscale = False
00085 elif all == "fastest":
00086
00087 self.MinQuality = 1
00088 self.Step = 9
00089 self.Interlace = True
00090 self.Optimize = True
00091 self.SFactors = "2x2"
00092 self.Optimizations = 4
00093 self.Grayscale = False
00094 else:
00095 warning("wrong value passed by --jpgall : using default")
00096
00097 def set_optimizations(self, opt):
00098 """Set optimization level to use.
00099 """
00100 try:
00101 self.Optimizations = clamp(int(opt),0,5)
00102 except ValueException:
00103 warning("wrong value passed by --jpgopt : using default")
00104
00105 def set_min_quality(self, q):
00106 """Sets minimum quality to use
00107 """
00108 self.MinQuality = valid_number(q, 50, "--jpgqual", 1, 100)
00109
00110 def set_step(self, s):
00111 """Sets step to increase quality by.
00112 """
00113 self.Step = valid_number(s, 50, "--jpgqual", 1, 99)
00114
00115 def set_interlace(self):
00116 """Turns interlacing on.
00117 """
00118 self.Interlace = True
00119
00120 def set_optimize(self):
00121 """Turns optimized compression on.
00122 """
00123 self.Optimize = True
00124
00125 def set_sfactors(self, f):
00126 """Sets subsampling factors to use.
00127 """
00128 if f in ("standard", "all"):
00129 self.SFactors = f
00130 return
00131 for fac in f.split(","):
00132 if fac not in ("1x1", "1x2", "1x4", "2x1", "2x2", "2x4", "4x1",\
00133 "4x2"):
00134 warning("WARNING: wrong value passed by --jpgsampfactors :\
00135 using default")
00136 return
00137 self.SFactors = f
00138
00139 def set_grayscale(self, f):
00140 """Turns generation of grayscale images on.
00141 """
00142 self.Grayscale = True
00143
00144 def get_quality_vals(self):
00145 """Parses stored quality settings, returns list of used quality values.
00146 """
00147 out = [self.MinQuality]
00148 quality = self.MinQuality
00149 while quality < 100:
00150 quality = quality + self.Step
00151 if quality >100:
00152 quality = 100
00153 out.append(quality)
00154 return out
00155
00156 def get_sfactor_vals(self):
00157 """Parses stored subsampling factor data and returns a list of factors.
00158 """
00159 if(self.SFactors == "standard"):
00160 return ["1x1", "2x1", "2x2"]
00161 elif(self.SFactors == "all"):
00162 return ["1x1", "1x2", "1x4", "2x1", "2x2", "2x4", "4x1", "4x2"]
00163 else:
00164 return self.SFactors.split(",")
00165
00166 def get_interlace_vals(self):
00167 """Parses stored interlace data, returns list of used interlace values.
00168 """
00169 if self.Interlace:
00170 return [False, True]
00171 return [False]
00172
00173 def get_optimize_vals(self):
00174 """Parses stored optimize data, returns list of used optimize values.
00175 """
00176 if self.Optimize:
00177 return [False, True]
00178 return [False]
00179
00180 def get_grayscale_vals(self):
00181 """Parses stored grayscale data, returns list of used grayscale values.
00182 """
00183 if self.Grayscale:
00184 return [False, True]
00185 return [False]
00186
00187 def get_vals_str(self, str):
00188 """Returns all possible values of setting requested by input string.
00189 """
00190 if str == "quality":
00191 return self.get_quality_vals()
00192 elif str == "interlace":
00193 return self.get_interlace_vals()
00194 elif str == "optimize":
00195 return self.get_optimize_vals()
00196 elif str == "sampfactor":
00197 return self.get_sfactor_vals()
00198 elif str == "grayscale":
00199 return self.get_grayscale_vals()
00200 else:
00201 warning("unknown value passed to JPGProcessor.get_vals_str()")
00202
00203 def get_max_files(self):
00204 """Returns maximum number of files generated in optimization.
00205
00206 Used for % feedback.
00207 """
00208 out = len(self.get_quality_vals()) * len(self.get_optimize_vals()) *\
00209 len(self.get_interlace_vals()) * len(self.get_sfactor_vals())*\
00210 len(self.get_grayscale_vals())
00211 if self.Optimizations > 0:
00212 out /= 2
00213 return out
00214
00215 def process(self, reffile, filenoext, queue=None, proc=0):
00216 """Generates temporary jpg files and returns a list of them.
00217 """
00218 output(str(proc) + " processing jpg")
00219 outfilelist = []
00220
00221 fcount = 0.0
00222 for quality in self.get_quality_vals():
00223 for opt in self.get_optimize_vals():
00224 for inter in self.get_interlace_vals():
00225 for gray in self.get_grayscale_vals():
00226
00227
00228
00229 if self.Optimizations >= 1:
00230 if (opt and inter) or ((not opt) and (not inter)):
00231 continue
00232 for factor in self.get_sfactor_vals():
00233 outfilename = globals.TEMPDIR + filenoext + "_" +factor\
00234 + str(inter) + str(opt) + str(quality)\
00235 + str(gray) + ".jpg";
00236 outfilelist.append(TempImage(outfilename, "jpg",
00237 JPGSettings(quality, inter,
00238 opt, factor, gray), reffile))
00239 fcount += 1.0
00240 output("jpg " + str(proc) + " " + str(fcount /
00241 self.get_max_files() * 100)+" %",3)
00242 if queue is not None:
00243 queue.put(outfilelist)
00244 return
00245 else:
00246 return outfilelist