Board logo

标题: 控制台图片位深调整工具 imgbit.exe [打印本页]

作者: happy886rr    时间: 2017-5-15 15:39     标题: 控制台图片位深调整工具 imgbit.exe

本帖最后由 happy886rr 于 2017-5-15 15:47 编辑

下载:将以下外链图存为a.zip,解压即是。

IMGBIT.EXE  (BIT DEPTH CONVERSION TOOL, BY LEO)

摘要:
===============================================================
CMD控制台图片位深调整工具,支持的位深有1、4、8、16、32、48、64;同时兼具图片格式转化功能。
===============================================================

用法:
imgbit -i [待处理图片名] -o [输出后的图片名] -b [位深度]

举例:
  1. REM 将test.png转化为1位深度,即黑白图
  2. imgbit -itest.png -otest.png -b1
  3. REM 覆盖原图转为8位深
  4. imgbit -itest.png -b8
  5. REM 格式转化
  6. imgbit -itest.png -otest.jpg
复制代码
版本:
version 1.0

源码:
  1. /*
  2. CONSOLE IMAGE BIT DEPTH CONVERSION TOOL, COPYRIGHT@2017~2019 BY LEO, VERSION 1.0
  3. IMGBIT.EXE
  4. LINK GDI32 GDIPLUS
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <windows.h>
  9. #include <gdiplus\gdiplus.h>
  10. //GDI+命名空间
  11. using namespace Gdiplus;
  12. //定义帮助说明
  13. #define HELP_INFORMATION "\
  14. Usage: imgbit -i [image file] -o [out file] -b [image bit]\n\
  15. \n\
  16. Version:\n\
  17.     1.0 - Image Bit Depth Conversion Tool - Copyright (C) 2017-2019\n\
  18. \n\
  19. Official website:\n\
  20.     http://www.bathome.net/thread-44149-1-1.html\n"
  21. //声明C函数
  22. extern "C" HWND WINAPI GetConsoleWindow();
  23. //定义位深度枚举
  24. INT BITDEEP[65]={0,PixelFormat1bppIndexed,0,0,PixelFormat4bppIndexed,0,0,0,PixelFormat8bppIndexed,0,0,0,0,0,0,0,PixelFormat16bppRGB555,0,0,0,0,0,0,0,PixelFormat24bppRGB,0,0,0,0,0,0,0,PixelFormat32bppARGB,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,PixelFormat48bppRGB,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,PixelFormat64bppARGB};
  25. //转码款字符
  26. WCHAR* LW(const CHAR* str)
  27. {
  28. if(!str)
  29. {
  30. return NULL;
  31. }
  32. int wLen=MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, str, -1, NULL, 0);
  33. WCHAR* wstr=(WCHAR*)malloc(sizeof(WCHAR)*wLen + 1);
  34. MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, str, -1, wstr, wLen);
  35. wstr[wLen]='\0';
  36. return wstr;
  37. }
  38. //开关解析
  39. int OPTIND=1, OPTOPT;
  40. char* OPTARG;
  41. int GetoptIndex(int nargc, char *nargv[], char *ostr)
  42. {
  43. static char* place="";
  44. static char* lastostr=(char*)0;
  45. register char* oli;
  46. if(ostr!=lastostr)
  47. {
  48. lastostr=ostr;
  49. place="";
  50. }
  51. if(!*place)
  52. {
  53. if(
  54.     (OPTIND>=nargc)              ||
  55.     (*(place=nargv[OPTIND])!='-')||
  56.     (!*(++place))
  57. )
  58. {
  59. place="";
  60. return(EOF);
  61. }
  62. if (*place == '-' && place[1] == 0)
  63. {
  64. ++OPTIND;
  65. return(EOF);
  66. }
  67. }
  68. if ((OPTOPT=(int)*place++)==(int)':' || !(oli=strchr(ostr, OPTOPT)))
  69. {
  70. if(!*place)
  71. {
  72. ++OPTIND;
  73. }
  74. }
  75. if (oli != NULL && *(++oli) != ':')
  76. {
  77. OPTARG=NULL;
  78. if(!*place)
  79. {
  80. ++OPTIND;
  81. }
  82. }
  83. else
  84. {
  85. if(*place)
  86. {
  87. OPTARG=place;
  88. }
  89. else if(nargc<=++OPTIND)
  90. {
  91. place="";
  92. }
  93. else
  94. {
  95. OPTARG=nargv[OPTIND];
  96. }
  97. place="";
  98. ++OPTIND;
  99. }
  100. return(OPTOPT);
  101. }
  102. //获取编码器CLSID
  103. BOOL GetEncoderClsid(const WCHAR* outNAME, CLSID* pClsid)
  104. {
  105. UINT n=0, s=0;
  106. ImageCodecInfo* pInfo=NULL;
  107. GetImageEncodersSize(&n, &s);
  108. if(s==0)
  109. {
  110. return FALSE;
  111. }
  112. pInfo=(ImageCodecInfo*)(malloc(s));
  113. if(pInfo==NULL)
  114. {
  115. return FALSE;
  116. }
  117. GetImageEncoders(n, s, pInfo);
  118. for(int i=0; i<n; i++)
  119. {
  120. LPWSTR lp=wcsrchr(outNAME, L'.');
  121. if(lstrlenW(lp)<3 || lp==NULL){
  122. //此处留空过滤
  123. ;
  124. }
  125. else if(_wcsnicmp(pInfo[i].MimeType+6, ++lp, 2)==0)
  126. {
  127. *pClsid=pInfo[i].Clsid;
  128. free(pInfo);
  129. return TRUE;
  130. }
  131. }
  132. free(pInfo);
  133. return FALSE;
  134. }
  135. //位深转化函数
  136. int BitmapDeepConv(LPCWSTR srcNAME, LPCWSTR outNAME, int imgBIT)
  137. {
  138. //读取图像
  139. Bitmap* srcIMG=new Bitmap(srcNAME);
  140. //获取位图尺寸
  141. int imgWITH=srcIMG->GetWidth(), imgHIGH=srcIMG->GetHeight();
  142. Rect rect(0, 0, imgWITH, imgHIGH);
  143. //如果位图尺寸为零,判定为文件不存在
  144. if(imgWITH==0 && imgHIGH==0)
  145. {
  146. fprintf(stderr, "Read the image failed");
  147. exit(1);
  148. }
  149. //转化位深度
  150. Bitmap* covIMG=new Bitmap(imgWITH, imgHIGH, imgBIT);
  151. Graphics* graph=new Graphics(covIMG);
  152. //清除背景色
  153. Color myColor(0,0,0,0);  
  154. graph->Clear(myColor);
  155. //绘制位图
  156. graph->DrawImage(srcIMG, rect);
  157. //对位图进行编码
  158. CLSID clsid;
  159. if(GetEncoderClsid(outNAME, &clsid))
  160. {
  161. if(_wcsicmp(srcNAME, outNAME)==0){
  162. //释放内存
  163. delete srcIMG;
  164. if(imgBIT==0){
  165. return 1;
  166. }
  167. }
  168. if(imgBIT==0){
  169. if(srcIMG->Save(outNAME, &clsid, NULL) != S_OK){
  170. fprintf(stderr, "Conv image failed\n");
  171. exit(1);
  172. }
  173. }else{
  174. if(covIMG->Save(outNAME, &clsid, NULL) != S_OK)
  175. {
  176. fprintf(stderr, "Save image failed\n");
  177. exit(1);
  178. }
  179. }
  180. }
  181. else
  182. {
  183. fprintf(stderr, "Encode image failed\n");
  184. exit(1);
  185. }
  186. return 0;
  187. }
  188. //主函数入口
  189. int main(int argc, char* argv[])
  190. {
  191. if(argc<2)
  192. {
  193. //无参数则退出
  194. fprintf(stdout, HELP_INFORMATION);
  195. exit(0);
  196. }
  197. //设置文件名
  198. LPCWSTR srcNAME=NULL, outNAME=NULL;
  199. INT imgBIT=0, K;
  200. //开关解析
  201. while((K=GetoptIndex(argc,argv,"hi:o:b:HI:O:B:"))!=-1)
  202. {
  203. switch(K)
  204. {
  205. case 'h':
  206. case 'H':
  207. fprintf(stdout, HELP_INFORMATION);
  208. exit(0);
  209. case 'i':
  210. case 'I':
  211. if(OPTARG !=NULL)
  212. {
  213. srcNAME=LW(OPTARG);
  214. }
  215. break;
  216. case 'o':
  217. case 'O':
  218. if(OPTARG !=NULL)
  219. {
  220. outNAME=LW(OPTARG);
  221. }
  222. break;
  223. case 'b':
  224. case 'B':
  225. if(OPTARG !=NULL)
  226. {
  227. INT i=atoi(OPTARG);
  228. if(i<0 || 64<i)
  229. {
  230. fprintf(stderr, "Wrong bit depth\n");
  231. exit(1);
  232. }
  233. imgBIT=BITDEEP[i];
  234. }
  235. break;
  236. default:
  237. fprintf(stderr, "Unknown switch '-%c'\n", K);
  238. exit(1);
  239. }
  240. }
  241. if(srcNAME==NULL)
  242. {
  243. fprintf(stderr, "Needs input image file\n");
  244. exit(1);
  245. }
  246. //初始化GDI+
  247. GdiplusStartupInput gdiplusstartupinput;
  248. ULONG_PTR gdiplustoken;
  249. GdiplusStartup(&gdiplustoken, &gdiplusstartupinput, NULL);
  250. //调用位深转化函数
  251. BitmapDeepConv(srcNAME, (outNAME==NULL)?srcNAME:outNAME, imgBIT);
  252. //关闭GDI+
  253. GdiplusShutdown(gdiplustoken);
  254. return 0;
  255. }
复制代码





欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2