找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 8959|回复: 0

控制台图片位深调整工具 imgbit.exe

[复制链接]
发表于 2017-5-15 15:39:52 | 显示全部楼层 |阅读模式
本帖最后由 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.                 //获取位图尺寸
  142.                 int imgWITH=srcIMG->GetWidth(), imgHIGH=srcIMG->GetHeight();
  143.                 Rect rect(0, 0, imgWITH, imgHIGH);
  144.                
  145.                 //如果位图尺寸为零,判定为文件不存在
  146.                 if(imgWITH==0 && imgHIGH==0)
  147.                 {
  148.                         fprintf(stderr, "Read the image failed");
  149.                         exit(1);
  150.                 }
  151.        
  152.                 //转化位深度
  153.                 Bitmap* covIMG=new Bitmap(imgWITH, imgHIGH, imgBIT);
  154.                 Graphics* graph=new Graphics(covIMG);
  155.                
  156.                 //清除背景色
  157.                 Color myColor(0,0,0,0);  
  158.                 graph->Clear(myColor);
  159.                
  160.                 //绘制位图
  161.                 graph->DrawImage(srcIMG, rect);

  162.                 //对位图进行编码
  163.                 CLSID clsid;
  164.                 if(GetEncoderClsid(outNAME, &clsid))
  165.                 {
  166.                         if(_wcsicmp(srcNAME, outNAME)==0){
  167.                                 //释放内存
  168.                                 delete srcIMG;
  169.                                
  170.                                 if(imgBIT==0){
  171.                                         return 1;
  172.                                 }
  173.                         }
  174.                                
  175.                         if(imgBIT==0){
  176.                                 if(srcIMG->Save(outNAME, &clsid, NULL) != S_OK){
  177.                                         fprintf(stderr, "Conv image failed\n");
  178.                                         exit(1);
  179.                                 }
  180.                                
  181.                         }else{
  182.                                 if(covIMG->Save(outNAME, &clsid, NULL) != S_OK)
  183.                                 {
  184.                                         fprintf(stderr, "Save image failed\n");
  185.                                         exit(1);
  186.                                 }
  187.                         }
  188.                 }
  189.                 else
  190.                 {
  191.                         fprintf(stderr, "Encode image failed\n");
  192.                         exit(1);
  193.                 }

  194.                 return 0;
  195. }

  196. //主函数入口
  197. int main(int argc, char* argv[])
  198. {
  199.         if(argc<2)
  200.         {
  201.                 //无参数则退出
  202.                 fprintf(stdout, HELP_INFORMATION);
  203.                 exit(0);
  204.         }

  205.         //设置文件名
  206.         LPCWSTR srcNAME=NULL, outNAME=NULL;
  207.         INT imgBIT=0, K;
  208.        
  209.         //开关解析
  210.         while((K=GetoptIndex(argc,argv,"hi:o:b:HI:O:B:"))!=-1)
  211.         {
  212.                 switch(K)
  213.                 {
  214.                 case 'h':
  215.                 case 'H':
  216.                         fprintf(stdout, HELP_INFORMATION);
  217.                         exit(0);

  218.                 case 'i':
  219.                 case 'I':
  220.                         if(OPTARG !=NULL)
  221.                         {
  222.                                 srcNAME=LW(OPTARG);
  223.                         }
  224.                         break;

  225.                 case 'o':
  226.                 case 'O':
  227.                         if(OPTARG !=NULL)
  228.                         {
  229.                                 outNAME=LW(OPTARG);
  230.                         }
  231.                         break;

  232.                 case 'b':
  233.                 case 'B':
  234.                         if(OPTARG !=NULL)
  235.                         {
  236.                                 INT i=atoi(OPTARG);
  237.                                 if(i<0 || 64<i)
  238.                                 {
  239.                                         fprintf(stderr, "Wrong bit depth\n");
  240.                                         exit(1);
  241.                                 }
  242.                                 imgBIT=BITDEEP[i];
  243.                         }
  244.                         break;

  245.                 default:
  246.                         fprintf(stderr, "Unknown switch '-%c'\n", K);
  247.                         exit(1);
  248.                 }
  249.         }

  250.         if(srcNAME==NULL)
  251.         {
  252.                 fprintf(stderr, "Needs input image file\n");
  253.                 exit(1);
  254.         }

  255.         //初始化GDI+
  256.         GdiplusStartupInput gdiplusstartupinput;
  257.         ULONG_PTR gdiplustoken;
  258.         GdiplusStartup(&gdiplustoken, &gdiplusstartupinput, NULL);
  259.        
  260.         //调用位深转化函数
  261.         BitmapDeepConv(srcNAME, (outNAME==NULL)?srcNAME:outNAME, imgBIT);

  262.         //关闭GDI+
  263.         GdiplusShutdown(gdiplustoken);
  264.        
  265.         return 0;
  266. }
复制代码

评分

参与人数 3技术 +3 收起 理由
taofan712 + 1 辛勤码农
老刘1号 + 1 666、正好用到
freesoft00 + 1 +1

查看全部评分

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-17 04:47 , Processed in 0.020473 second(s), 9 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表