📄 cmdoptions.cpp
字号:
/* //////////////////////////////////////////////////////////////////////////// INTEL CORPORATION PROPRIETARY INFORMATION// This software is supplied under the terms of a license agreement or// nondisclosure agreement with Intel Corporation and may not be copied// or disclosed except in accordance with the terms of that agreement.// Copyright(c) 2002-2005 Intel Corporation. All Rights Reserved.//////*//*// Command options class interprets IPP JPEG 2000 lossless example// specific command line parameters.//////*/#include <math.h>#include <string.h>#include "cmdoptions.h"#include "cmdline.h"#include "jp2const.h"#include "cmdoptionswarning.h"#include "cmdoptionsexception.h"#include "stricmp.h"static const int RI_BMP = 8; // bitmap dynamic rangeCmdOptions::CmdOptions(char *argv[], int argc, BDiagnOutput &diagnOutput): m_diagnOutputPtr(diagnOutput), m_isPrintLibInfo(true), m_isUseMCT(true), m_maxAllowedWTLevel(5), m_ratio(0.0), m_grayscaleMode(autoDetectGrayscale), m_srcFileType(bmp), m_dstFileType(jp2), m_isDerivedQuant(false), m_isDisplay(false){ ////////////////////////////////////////////////////////////////////////// // // Setup switches and parse command-line // CommandLine cmdLine; double dquant = 0.0; StringA strGrayscaleMode("auto"); StringA strPNMOutputMode("binary"); StringA strWTMode("wt53"); cmdLine.AddKey ("i", "defines the input (.bmp or .jp2) file name", m_srcFileName); cmdLine.AddKey ("o", "defines the output (.jp2 or .bmp) file name, " "don't specify output file to display image from jp2 file on the screen", m_dstFileName); cmdLine.AddKey ("mct", "defines the use of multicomponent transform", m_isUseMCT); cmdLine.AddKey ("wlev", "defines the maximum level of wavelet transform", m_maxAllowedWTLevel); cmdLine.AddKey ("ratio", "defines the minimal compression ratio for lossy compression rate-distortion optimization," " and zero value (default) specifies lossless mode", m_ratio); cmdLine.AddKey ("linf", "show/hide library information", m_isPrintLibInfo); cmdLine.AddKey ("gray", "grayscale encoding mode for 8-bit palettized images " "('auto' - to detect grayscale palette, " "'force' - to discard actual palette information and keep only single component data, " "'never' - always save palette information in jp2 file)", strGrayscaleMode); cmdLine.AddKey ("wt", "defines the type of wavelet transform " "('wt53' - for reversible integer transform and" "'wt97' - for irreversible float point transform)", strWTMode); cmdLine.AddKey ("dquant", "defines the mode of quantization and its basic step size, " "zero value (default) specifies the " "use of expounded quantization mode with fixed " "quantization step equal to 1 for all subbands, " "any other values specifies derived quantization and value specifies " "step for deepest LL subband quantization", dquant); cmdLine.AddKey ("pnm", "defines binary/plain output for PNM format", strPNMOutputMode); cmdLine.AddHelpKey("h", "help key"); if(!cmdLine.Parse(argv, argc)) throw DiagnDescrCT<CmdOptionsException,cannotParseCmdLine>(); ////////////////////////////////////////////////////////////////////////// // // Check file-names // if(!m_srcFileName.Size()) throw DiagnDescrCT<CmdOptionsException,srcFileNameIsAbsent>(); m_srcFileType = ExtensionType(m_srcFileName); if(m_srcFileType == unknown) throw DiagnDescrCT<CmdOptionsException,srcFileHasUnknownExtension>(); m_dstFileType = ExtensionType(m_dstFileName); m_isPerformEncoding = (m_srcFileType != jp2 && m_srcFileType != j2k); if(m_isPerformEncoding) { if(!m_dstFileName.Size()) { m_dstFileName = m_srcFileName; ChangeExtension(m_dstFileName, "jp2"); } m_isDisplay = false; if( m_dstFileType != jp2) throw DiagnDescrCT<CmdOptionsException,dstFileHasUnknownExtension>(); } else { if(m_dstFileName.Size()) { m_isDisplay = false; if(m_dstFileType == jp2 || m_dstFileType == unknown) throw DiagnDescrCT<CmdOptionsException,dstFileHasUnknownExtension>(); } else m_isDisplay = true; } ////////////////////////////////////////////////////////////////////////// // // Check PNM specific switches and setup specific info // if (strPNMOutputMode == "binary") m_isPlainPNMOutput = false; else if(strPNMOutputMode == "plain" ) m_isPlainPNMOutput = true; else throw DiagnDescrCT<CmdOptionsException,badPNMOutputMode>(); ////////////////////////////////////////////////////////////////////////// // // Check JPEG 2000 specific switches and setup specific info // if(m_maxAllowedWTLevel < 0) throw DiagnDescrCT<CmdOptionsException,wlevExceedBounds>(); if(m_ratio < 0) throw DiagnDescrCT<CmdOptionsException,ratioExceedBounds>(); if (strGrayscaleMode == "auto" ) m_grayscaleMode = autoDetectGrayscale; else if(strGrayscaleMode == "force") m_grayscaleMode = forceGrayscale; else if(strGrayscaleMode == "never") m_grayscaleMode = neverUseGrayscale; else throw DiagnDescrCT<CmdOptionsException,badGrayscaleMode>(); if (strWTMode == "wt53") { m_isWT53Used = true; } else if(strWTMode == "wt97") { m_isWT53Used = false; if(!(m_ratio > 0)) throw DiagnDescrCT<CmdOptionsException,incompatibleWT97AndLosslessMode>(); } else throw DiagnDescrCT<CmdOptionsException,badWTMode>(); if(dquant > 0) { if(dquant < 1.0) m_diagnOutputPtr->Warning(DIAGN_DESCR_RT(CmdOptionsQuantWarning, DangerDelta)(dquant)); int dynRange = RI_BMP + DYN_RANGE_GAIN_LxLy; m_isDerivedQuant = true; m_derivedQuantBaseValue = QuantFromDelta((float)dquant, dynRange); if(m_derivedQuantBaseValue.Expn() < 0) { m_derivedQuantBaseValue = QuantValue(2047, 0); double delta = m_derivedQuantBaseValue.Delta(dynRange); m_diagnOutputPtr->Warning(DIAGN_DESCR_RT(CmdOptionsQuantWarning, WrongDelta)(delta)); } else if(m_derivedQuantBaseValue.Expn() > 31) { m_derivedQuantBaseValue = QuantValue(0, 31); double delta = m_derivedQuantBaseValue.Delta(dynRange); m_diagnOutputPtr->Warning(DIAGN_DESCR_RT(CmdOptionsQuantWarning, WrongDelta)(delta)); } }}char* CmdOptions::GetFileExtension(const char* fileName){ char* pointPos = strrchr(fileName, '.'); if(!pointPos) return 0; pointPos++; if(strpbrk(pointPos, "\"<>|/:*?\\")) return 0; return pointPos;}void CmdOptions::ChangeExtension(StringA &fileName, const char *newExt){ const char *data = fileName; const char *pos = GetFileExtension(data); if(pos) { int mainPartLen = (int)(pos - data); StringA newFileName(data, mainPartLen); newFileName = newFileName + newExt; fileName = newFileName; }}CmdOptions::FileType CmdOptions::ExtensionType(const char* fileName){ const char *extension = GetFileExtension(fileName); if(!extension) return unknown; if(0 == Stricmp(extension, "bmp")) return bmp; if(0 == Stricmp(extension, "pbm")) return pnm; if(0 == Stricmp(extension, "pgm")) return pnm; if(0 == Stricmp(extension, "ppm")) return pnm; if(0 == Stricmp(extension, "pnm")) return pnm; if(0 == Stricmp(extension, "jp2")) return jp2; if(0 == Stricmp(extension, "j2k")) return j2k; if(0 == Stricmp(extension, "jpc")) return j2k; if(0 == Stricmp(extension, "j2c")) return j2k; return unknown;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -