def work2JPG(filename, isPng = False): filepath = FileHelper.realpath(filename) filedir = FileHelper.dirname(filepath) name = FileHelper.basename(filepath) os.chdir(tpDir) jpgCMD = """%s -quality 90 %s %s """ % (convertBin, filepath, filepath) os.system(jpgCMD) #return tmpfilename = FileHelper.join(filedir, hashlib.md5(name.encode('utf-8')).hexdigest()) isSuccess = True with open(tmpfilename, 'wb+') as tmpFile: try: tmpFile.write(b'MNG') rgbname = filepath FileHelper.writeWithSize(tmpFile, filepath) except Exception: Log.printDetailln ("error00 !!!", filename, "cannot convert.") isSuccess = False finally: pass if isSuccess: FileHelper.remove(filepath) FileHelper.rename(tmpfilename, filepath) return 5 else: return 2
def work_file_PVR(filename, isDTC4Module = False, isTC4 = False): filepath = FileHelper.realpath(filename) filedir = FileHelper.dirname(filepath) sys.stdout.flush() os.chdir(toolsPath) isTC4 = True isAlphaJPG = False if isDTC4Module: isTC4 = False preCMD = " -p " preCMD = "" info = ImageInfo.size(filepath) # 只支持png纹理 if info[0] != 'PNG': return 2 width = info[1] height = info[2] # 只支持正方形2的幂纹理 if width & (width-1) != 0 or width != height: return 2 rgbCMD = """ %s -f PVRTC1_4_RGB %s -q pvrtcbest -i %s -o %s """ % (pvrTexToolBin, preCMD, filepath, filepath.replace(".png", ".pvr")) alphaCMD = """%s %s -alpha extract %s """ % (convertBin, filepath, filepath.replace(".png", ".alpha.jpg")) alphaJPGCMD = """ %s -f PVRTC1_4_RGB -q pvrtcbest -i %s -o %s """ % (pvrTexToolBin, filepath.replace(".png", ".alpha.jpg"), filepath.replace(".png", ".alpha.pvr")) if isTC4: rgbCMD = """ %s -f PVRTC1_4 %s -q pvrtcbest -i %s -o %s """ % (pvrTexToolBin, preCMD, filepath, filepath.replace(".png", ".pvr")) try: FileHelper.remove(filepath.replace(".png", ".pkm")) FileHelper.remove(filepath.replace(".png", "_alpha.pkm")) os.system(rgbCMD) if not isTC4: os.system(alphaCMD) if not isAlphaJPG and not isTC4: os.system(alphaJPGCMD) if not FileHelper.exists(filepath.replace(".png", ".pvr")): return 2 os.rename(filepath.replace(".png", ".pvr"), filepath.replace(".png", ".pkm")) if not isTC4: if not isAlphaJPG: os.rename(filepath.replace(".png", ".alpha.jpg"), filepath.replace(".png", "_alpha.pkm")) else: os.rename(filepath.replace(".png", ".alpha.pvr"), filepath.replace(".png", "_alpha.pkm")) FileHelper.remove(filepath.replace(".png", ".alpha.jpg")) FileHelper.remove(filepath.replace(".png", ".alpha.pvr")) except Exception: Log.printError() return 2 finally: pass tmpfilename = filepath.replace(".png", ".tmp") FileHelper.remove(tmpfilename) isSuccess = True with open(tmpfilename, 'wb+') as tmpFile: try: tmpFile.write(b'MNG') rgbname = filepath.replace(".png", ".pkm") statinfo = os.stat(rgbname) fileSize = statinfo.st_size tmpFile.write(pack("i", fileSize)) rgbfile = open(rgbname, "rb") tmpFile.write(rgbfile.read()) rgbfile.close() alphaname = filepath.replace(".png", "_alpha.pkm") if not isTC4: statinfo = os.stat(alphaname) fileSize = statinfo.st_size tmpFile.write(pack("i", fileSize)) alphafile = open(alphaname, "rb") tmpFile.write(alphafile.read()) alphafile.close() # if preAlpha: # tmpFile.write('p') # else: # tmpFile.write('P') if not isSaveTransFile: FileHelper.remove(rgbname) FileHelper.remove(alphaname) except Exception: t, v, tb = sys.exc_info() Log.printDetailln(t, v) isSuccess = False pass finally: pass if isSuccess: if isUseGzip: gzip_cmd = gzipBin + tmpfilename + " -n -f -9" os.system(gzip_cmd) FileHelper.remove(tmpfilename.replace(".tmp", ".png")) FileHelper.rename(tmpfilename + ".gz", tmpfilename.replace(".tmp", ".png")) return 3 else: FileHelper.remove(tmpfilename.replace(".tmp", ".png")) FileHelper.rename(tmpfilename, tmpfilename.replace(".tmp", ".png")) return 5
def work_png(filename): filepath = FileHelper.realpath(filename) filedir = FileHelper.dirname(filepath) os.chdir(tpDir) isSaveTransFile = False isPng = True useGZIP = False if isPng: jpgCMD = """%s %s -background black %s """ % (convertBin, filepath, filepath.replace(".png", ".rgb.jpg")) alphaCMD = """%s %s -alpha extract %s """ % (convertBin, filepath, filepath.replace(".png", ".alpha.jpg")) try: os.system(jpgCMD) os.system(alphaCMD) except Exception: Log.printDetailln ("error33 !!!", filename, "cannot convert.") return 2 finally: pass tmpfilename = filepath.replace(".png", ".tmp") FileHelper.remove(tmpfilename) isSuccess = True with open(tmpfilename, 'wb+') as tmpFile: try: tmpFile.write(b'MNG') rgbname = filepath.replace(".png", ".rgb.jpg") FileHelper.writeWithSize(tmpFile, rgbname) alphaname = filepath.replace(".png", ".alpha.jpg") FileHelper.writeWithSize(tmpFile, alphaname) if not isSaveTransFile: FileHelper.remove(rgbname) FileHelper.remove(alphaname) except Exception: Log.printError() isSuccess = False pass finally: pass if isSuccess: if useGZIP: gzip_cmd = gzipBin + tmpfilename + " -n -f -9" os.system(gzip_cmd) FileHelper.remove(tmpfilename.replace(".tmp", ".png")) FileHelper.rename(tmpfilename + ".gz", tmpfilename.replace(".tmp", ".png")) return 3 else: FileHelper.remove(tmpfilename.replace(".tmp", ".png")) FileHelper.rename(tmpfilename, tmpfilename.replace(".tmp", ".png")) return 5 else: return 2
def work_file_ETC(filename, isAlphaJPG = False, isFast = False): filepath = FileHelper.realpath(filename) filedir = FileHelper.dirname(filepath) sys.stdout.flush() #preAlpha = needPreAplha(filedir) preAlpha = False preCMD = " -p " if not preAlpha: preCMD = "" os.chdir(toolsPath) isPng = True if filename.find(".png") != -1: isPng = True elif filename.find(".jpg") != -1: isPng = False else: return 2 if isFast: quality = 'etcfast' else: quality = 'etcslow' rgbCMD = """ %s -f ETC1 %s -q %s -i %s -o %s """ % (pvrTexToolBin, preCMD, quality, filepath, filepath.replace(".png", ".pvr")) alphaCMD = """%s %s -alpha extract %s """ % (convertBin, filepath, filepath.replace(".png", ".alpha.jpg")) alphaJPGCMD = """ %s -f ETC1 -q %s -i %s -o %s """ % (pvrTexToolBin, quality, filepath.replace(".png", ".alpha.jpg"), filepath.replace(".png", ".alpha.pvr")) try: if isPng: FileHelper.remove(filepath.replace(".png", ".pkm")) FileHelper.remove(filepath.replace(".png", "_alpha.pkm")) os.system(rgbCMD) os.system(alphaCMD) if not isAlphaJPG: os.system(alphaJPGCMD) FileHelper.rename(filepath.replace(".png", ".pvr"), filepath.replace(".png", ".pkm")) if isAlphaJPG: FileHelper.rename(filepath.replace(".png", ".alpha.jpg"), filepath.replace(".png", "_alpha.pkm")) else: FileHelper.rename(filepath.replace(".png", ".alpha.pvr"), filepath.replace(".png", "_alpha.pkm")) FileHelper.remove(filepath.replace(".png", ".alpha.jpg")) FileHelper.remove(filepath.replace(".png", ".alpha.pvr")) else: FileHelper.remove(filepath.replace(".jpg", ".pkm")) rgbCMD = """ %s -f ETC1 -p -q %s -i %s -o %s """ % (pvrTexToolBin, quality, filepath, filepath.replace(".jpg", ".pvr")) os.system(rgbCMD) FileHelper.rename(filepath.replace(".jpg", ".pvr"), filepath.replace(".jpg", ".pkm")) except Exception: t, v, tb = sys.exc_info() Log.printDetailln(t, v) pass finally: pass if isPng: tmpfilename = filepath.replace(".png", ".tmp") FileHelper.remove(tmpfilename) isSuccess = True with open(tmpfilename, 'wb+') as tmpFile: try: tmpFile.write(b'MNG') rgbname = filepath.replace(".png", ".pkm") alphaname = filepath.replace(".png", "_alpha.pkm") FileHelper.writeWithSize(tmpFile, rgbname) FileHelper.writeWithSize(tmpFile, alphaname) # if preAlpha: # tmpFile.write('p') # else: # tmpFile.write('P') if not isSaveTransFile: FileHelper.remove(rgbname) FileHelper.remove(alphaname) except Exception: Log.printError() isSuccess = False pass finally: pass if isSuccess: if isUseGzip: gzip_cmd = gzipBin + tmpfilename + " -n -f -9" os.system(gzip_cmd) FileHelper.remove(tmpfilename.replace(".tmp", ".png")) FileHelper.rename(tmpfilename + ".gz", tmpfilename.replace(".tmp", ".png")) return 3 else: FileHelper.remove(tmpfilename.replace(".tmp", ".png")) FileHelper.rename(tmpfilename, tmpfilename.replace(".tmp", ".png")) return 5 else: FileHelper.remove(tmpfilename) return 2 else: tmpfilename = filepath.replace(".jpg", ".pkm") if not FileHelper.exists(tmpfilename): Log.printDetailln ("error !!!", filepath, "cannot convert.") return 2 if isUseGzip: gzip_cmd = gzipBin + tmpfilename + " -n -f -9" os.system(gzip_cmd) FileHelper.remove(tmpfilename.replace(".pkm", ".jpg")) FileHelper.rename(tmpfilename + ".gz", tmpfilename.replace(".pkm", ".jpg")) return 3 else: FileHelper.remove(tmpfilename.replace(".pkm", ".jpg")) FileHelper.rename(tmpfilename, tmpfilename.replace(".pkm", ".jpg")) return 4