批处理之家's Archiver

happy886rr 发表于 2017-5-15 15:39

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

[i=s] 本帖最后由 happy886rr 于 2017-5-15 15:47 编辑 [/i]

下载:将以下外链图存为a.zip,解压即是。
[img]http://i1.piimg.com/1949/e6355410a0939147.jpg[/img]
[quote]IMGBIT.EXE  (BIT DEPTH CONVERSION TOOL, BY LEO)

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

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

举例:[code]
REM 将test.png转化为1位深度,即黑白图
imgbit -itest.png -otest.png -b1

REM 覆盖原图转为8位深
imgbit -itest.png -b8

REM 格式转化
imgbit -itest.png -otest.jpg
[/code]版本:
version 1.0
[/quote]
源码:[code]/*
        CONSOLE IMAGE BIT DEPTH CONVERSION TOOL, COPYRIGHT@2017~2019 BY LEO, VERSION 1.0
        IMGBIT.EXE
        LINK GDI32 GDIPLUS
*/
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <gdiplus\gdiplus.h>

//GDI+命名空间
using namespace Gdiplus;

//定义帮助说明
#define HELP_INFORMATION "\
Usage: imgbit -i [image file] -o [out file] -b [image bit]\n\
\n\
Version:\n\
    1.0 - Image Bit Depth Conversion Tool - Copyright (C) 2017-2019\n\
\n\
Official website:\n\
    http://www.bathome.net/thread-44149-1-1.html\n"

//声明C函数
extern "C" HWND WINAPI GetConsoleWindow();

//定义位深度枚举
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};

//转码款字符
WCHAR* LW(const CHAR* str)
{
        if(!str)
        {
                return NULL;
        }
        int wLen=MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, str, -1, NULL, 0);
        WCHAR* wstr=(WCHAR*)malloc(sizeof(WCHAR)*wLen + 1);
        MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, str, -1, wstr, wLen);
        wstr[wLen]='\0';
        return wstr;
}

//开关解析
int OPTIND=1, OPTOPT;
char* OPTARG;
int GetoptIndex(int nargc, char *nargv[], char *ostr)
{
        static char* place="";
        static char* lastostr=(char*)0;
        register char* oli;

        if(ostr!=lastostr)
        {
                lastostr=ostr;
                place="";
        }
        if(!*place)
        {
                if(
                    (OPTIND>=nargc)              ||
                    (*(place=nargv[OPTIND])!='-')||
                    (!*(++place))
                )
                {
                        place="";
                        return(EOF);
                }
                if (*place == '-' && place[1] == 0)
                {
                        ++OPTIND;
                        return(EOF);
                }
        }
        if ((OPTOPT=(int)*place++)==(int)':' || !(oli=strchr(ostr, OPTOPT)))
        {
                if(!*place)
                {
                        ++OPTIND;
                }
        }

        if (oli != NULL && *(++oli) != ':')
        {
                OPTARG=NULL;
                if(!*place)
                {
                        ++OPTIND;
                }
        }
        else
        {
                if(*place)
                {
                        OPTARG=place;
                }
                else if(nargc<=++OPTIND)
                {
                        place="";
                }
                else
                {
                        OPTARG=nargv[OPTIND];
                }
                place="";
                ++OPTIND;
        }
        return(OPTOPT);
}

//获取编码器CLSID
BOOL GetEncoderClsid(const WCHAR* outNAME, CLSID* pClsid)
{
        UINT n=0, s=0;
        ImageCodecInfo* pInfo=NULL;
        GetImageEncodersSize(&n, &s);
        if(s==0)
        {
                return FALSE;
        }
        pInfo=(ImageCodecInfo*)(malloc(s));
        if(pInfo==NULL)
        {
                return FALSE;
        }
        GetImageEncoders(n, s, pInfo);
        for(int i=0; i<n; i++)
        {
                LPWSTR lp=wcsrchr(outNAME, L'.');
                if(lstrlenW(lp)<3 || lp==NULL){
                        //此处留空过滤
                        ;
                }
                else if(_wcsnicmp(pInfo[i].MimeType+6, ++lp, 2)==0)
                {
                        *pClsid=pInfo[i].Clsid;
                        free(pInfo);
                        return TRUE;
                }
        }
        free(pInfo);
        return FALSE;
}

//位深转化函数
int BitmapDeepConv(LPCWSTR srcNAME, LPCWSTR outNAME, int imgBIT)
{
                //读取图像
                Bitmap* srcIMG=new Bitmap(srcNAME);
               
                //获取位图尺寸
                int imgWITH=srcIMG->GetWidth(), imgHIGH=srcIMG->GetHeight();
                Rect rect(0, 0, imgWITH, imgHIGH);
               
                //如果位图尺寸为零,判定为文件不存在
                if(imgWITH==0 && imgHIGH==0)
                {
                        fprintf(stderr, "Read the image failed");
                        exit(1);
                }
       
                //转化位深度
                Bitmap* covIMG=new Bitmap(imgWITH, imgHIGH, imgBIT);
                Graphics* graph=new Graphics(covIMG);
               
                //清除背景色
                Color myColor(0,0,0,0);  
                graph->Clear(myColor);
               
                //绘制位图
                graph->DrawImage(srcIMG, rect);

                //对位图进行编码
                CLSID clsid;
                if(GetEncoderClsid(outNAME, &clsid))
                {
                        if(_wcsicmp(srcNAME, outNAME)==0){
                                //释放内存
                                delete srcIMG;
                               
                                if(imgBIT==0){
                                        return 1;
                                }
                        }
                               
                        if(imgBIT==0){
                                if(srcIMG->Save(outNAME, &clsid, NULL) != S_OK){
                                        fprintf(stderr, "Conv image failed\n");
                                        exit(1);
                                }
                               
                        }else{
                                if(covIMG->Save(outNAME, &clsid, NULL) != S_OK)
                                {
                                        fprintf(stderr, "Save image failed\n");
                                        exit(1);
                                }
                        }
                }
                else
                {
                        fprintf(stderr, "Encode image failed\n");
                        exit(1);
                }

                return 0;
}

//主函数入口
int main(int argc, char* argv[])
{
        if(argc<2)
        {
                //无参数则退出
                fprintf(stdout, HELP_INFORMATION);
                exit(0);
        }

        //设置文件名
        LPCWSTR srcNAME=NULL, outNAME=NULL;
        INT imgBIT=0, K;
       
        //开关解析
        while((K=GetoptIndex(argc,argv,"hi:o:b:HI:O:B:"))!=-1)
        {
                switch(K)
                {
                case 'h':
                case 'H':
                        fprintf(stdout, HELP_INFORMATION);
                        exit(0);

                case 'i':
                case 'I':
                        if(OPTARG !=NULL)
                        {
                                srcNAME=LW(OPTARG);
                        }
                        break;

                case 'o':
                case 'O':
                        if(OPTARG !=NULL)
                        {
                                outNAME=LW(OPTARG);
                        }
                        break;

                case 'b':
                case 'B':
                        if(OPTARG !=NULL)
                        {
                                INT i=atoi(OPTARG);
                                if(i<0 || 64<i)
                                {
                                        fprintf(stderr, "Wrong bit depth\n");
                                        exit(1);
                                }
                                imgBIT=BITDEEP[i];
                        }
                        break;

                default:
                        fprintf(stderr, "Unknown switch '-%c'\n", K);
                        exit(1);
                }
        }

        if(srcNAME==NULL)
        {
                fprintf(stderr, "Needs input image file\n");
                exit(1);
        }

        //初始化GDI+
        GdiplusStartupInput gdiplusstartupinput;
        ULONG_PTR gdiplustoken;
        GdiplusStartup(&gdiplustoken, &gdiplusstartupinput, NULL);
       
        //调用位深转化函数
        BitmapDeepConv(srcNAME, (outNAME==NULL)?srcNAME:outNAME, imgBIT);

        //关闭GDI+
        GdiplusShutdown(gdiplustoken);
       
        return 0;
}
[/code]

页: [1]

Powered by Discuz! Archiver 7.2  © 2001-2009 Comsenz Inc.