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

控制台图形显示工具IMG.EXE

[复制链接]
发表于 2016-11-12 02:46:01 | 显示全部楼层 |阅读模式
本帖最后由 happy886rr 于 2016-11-13 00:34 编辑

强悍的控制台图形显示工具,支持jpg、png、bmp、gif(首帧)显示。内置脚本解析器,可以完成简单的goto跳转功能,用“::行号?循环次数”完成goto 跳转。使用gdiplus图形库;体积小巧、速度迅捷、功能实用、浅析脚本。
IMG.EXE
图片存为a.zip解压即是

-----------------------------------------------------------------------------
THE CONSOLE DISPLAYS PICTURE TOOL, VERSION 1.2, COPYRIGHT@2016~2018 BY HAPPY

img [options]
-----------------------------------------------------------------------------
    :                            Hide the cursor
    :::[time]                    Delay ms
    :::::                        Show images in desktop
    ::[L]?[CYC]                  Cycle CYC times from row L
    +{parameters}                Cyclic accumulator
    [imgfile]                    Direct display imgfile
    [imgfile]:{parameters}       Display the image with the given parameters
             :{parameters}       Erase the display with the given parameters
    [scriptfile]::[m],[n]        Get commands from [m] to [n] in the sfile
_____________________________________________________________________________
强悍的控制台图形显示工具,支持jpg、png、bmp、gif(首帧)显示。内置脚本解析器,
可以完成简单的goto跳转功能,用“::行号”完成goto 跳转。
_____________________________________________________________________________
隐藏光标
img :

延时500毫秒
:::500

在CMD窗口外显示图片
img :::::

省参数图形显示
img test.jpg

参数自动补全
img test.jpg:8,16
img test.jpg:8,16,100,200
img test.jpg:8,16,100,200,50,20

八参数图形显示
img test.jpg:cmd_x, cmd_y, size_W, size_H, src_x, src_y, disp_W, disp_H

擦除指定选区
img :start_cmd_x, end_cmd_x, start_cmd_y, end_cmd_y

从脚本中执行第3行到第8行
img test.txt::3,8

_____________________________________________________________________________
goto跳转语句 //::行号?循环次数
::12?80

循环累加器
+0,0,10,0
test.jpg:0,0,0,-1
::1?80

循环擦除
+0,0,10,0
test.jpg:0,0,0,-1
+0,10
:-10,0,0,800
::1?80

图片放大
:
:::::
+0,0,10,0
test.jpg:0,0,0,-1
::3?80
_____________________________________________________________________________


编译:
  1. gcc img.c -std=gnu99 -O3 -s -lgdi32 -lgdiplus -oimg.exe
复制代码
源码:

  1. /*
  2.         THE CONSOLE DISPLAYS A PICTURE TOOL, VERSION 1.2
  3.         IMG.EXE
  4.         COPYRIGHT@2016~2018 BY HAPPY
  5. */

  6. //编译参数
  7. //gcc img.c -std=gnu99 -O3 -s -lgdi32 -lgdiplus -oimg.exe

  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <stdbool.h>
  11. #include <windows.h>
  12. #include <gdiplus\gdiplus.h>

  13. //定义参数行
  14. char* ARGS[16];

  15. //定义计步器
  16. int PEDO[9]={0};

  17. //引入WINAPI
  18. HWND WINAPI GetConsoleWindow();
  19. HWND   hCMD;

  20. //图形结构体
  21. typedef struct{
  22.     HBITMAP hBitmap;
  23.     int Width;
  24.     int Height;
  25. }IMAGE;

  26. /***************图形函数群***************/
  27. //图形加载
  28. IMAGE LoadImg(char *file, int _nWidth, int _nHeight)
  29. {
  30.         GpImage*     thisImage;
  31.         GpGraphics*  graphics=NULL;
  32.         ULONG_PTR    gdiplusToken;
  33.         IMAGE       _img;       
  34.         GdiplusStartupInput GSI={1, NULL, false, false};
  35.         HDC hdc=GetDC(NULL);
  36.         HDC hdcMem=CreateCompatibleDC(hdc);
  37.        
  38.         int nLen=MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, file, -1, NULL, 0);
  39.         if(nLen==0){
  40.                 exit(1);
  41.         }
  42.         wchar_t* wfile=(wchar_t*)malloc(sizeof(wchar_t)*nLen);
  43.         MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, file, -1, wfile, nLen);
  44.        
  45.         GdiplusStartup(&gdiplusToken, &GSI, NULL);   
  46.         if( GdipLoadImageFromFile(wfile, &thisImage) ){
  47.                 fputs("Load image error.\n", stdout);
  48.                 exit(2);
  49.         }
  50.         GdipGetImageWidth(thisImage, &_img.Width);
  51.         GdipGetImageHeight(thisImage, &_img.Height);
  52.         if      (_nHeight==-1 && _nWidth !=-1){
  53.                 _img.Height=(int)( (_img.Height*1.0 / _img.Width) * (_nWidth ) );
  54.                 _img.Width =_nWidth;
  55.         }else if(_nWidth ==-1 && _nHeight!=-1){
  56.                 _img.Width =(int)( (_img.Width*1.0 / _img.Height) * (_nHeight) );
  57.                 _img.Height=_nHeight;
  58.         }else if(_nHeight !=0 ){
  59.                 _img.Width =_nWidth;
  60.                 _img.Height=_nHeight;
  61.         }
  62.         _img.hBitmap=CreateCompatibleBitmap(hdc, _img.Width, _img.Height);
  63.        
  64.         SelectObject(hdcMem, _img.hBitmap);
  65.         GdipCreateFromHDC(hdcMem, &graphics);
  66.         GdipDrawImageRectI(graphics, thisImage, 0, 0, _img.Width, _img.Height);
  67.         GdipDisposeImage(thisImage);
  68.         GdipDeleteGraphics(graphics);
  69.         DeleteDC (hdcMem);
  70.         ReleaseDC(NULL, hdc);
  71.         GdiplusShutdown(gdiplusToken);
  72.         return _img;
  73. }
  74. //绘图函数
  75. int DrawImg(
  76.         char* imgfile,           //图形文件
  77.         int cmd_x,  int cmd_y,   //相对于cmd窗口位置
  78.         int size_W, int size_H,  //图片的显示宽高
  79.         int src_x,  int src_y,   //对相原图 截取位置
  80.         int disp_W, int disp_H   //要显示多少宽高
  81. ){
  82.         cmd_x +=PEDO[0]*PEDO[1], cmd_y +=PEDO[0]*PEDO[2];
  83.         size_W+=PEDO[0]*PEDO[3], size_H+=PEDO[0]*PEDO[4];
  84.         src_x +=PEDO[0]*PEDO[5], src_y +=PEDO[0]*PEDO[6];
  85.         disp_W+=PEDO[0]*PEDO[7], disp_H+=PEDO[0]*PEDO[8];
  86.         PEDO[0]=0;

  87.         IMAGE P   =LoadImg(imgfile, size_W, size_H);
  88.         HDC   hCUR=GetDC(hCMD);
  89.         HDC   hSRC=CreateCompatibleDC(hCUR);
  90.         SelectObject(hSRC, P.hBitmap);
  91.         if(disp_W !=0){
  92.                 P.Width =disp_W, P.Height=disp_H;
  93.         }
  94.         BitBlt(hCUR, cmd_x, cmd_y, P.Width, P.Height, hSRC, src_x, src_y, SRCCOPY);
  95.         DeleteObject(P.hBitmap);
  96.         ReleaseDC(hCMD, hCUR);
  97.         DeleteDC(hSRC);
  98.         return 0;
  99. }
  100. //擦除画布
  101. int CleanImg(
  102.         HWND  hd,     //句柄
  103.         LONG  left,   //左坐标
  104.         LONG  right,  //右坐标
  105.         LONG  top,    //上坐标
  106.         LONG  bottom  //底坐标
  107. ){
  108.         RECT  z={left+PEDO[0]*PEDO[1], top+PEDO[0]*PEDO[3], right+PEDO[0]*PEDO[2], bottom+PEDO[0]*PEDO[4]};
  109.         InvalidateRect(hd, &z, true);
  110.         return 0;
  111. }

  112. /***************功能函数群***************/
  113. //隐藏光标
  114. void HideCursor()
  115. {
  116.         CONSOLE_CURSOR_INFO cursor_info={1,0};
  117.         SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
  118. }
  119. //查找字符
  120. int strchp(const char* Str, const char delim)
  121. {
  122.         int i=-1;
  123.         while(Str[++i]!='\0'){
  124.                 if(Str[i]==delim){
  125.                         return i;
  126.                 }
  127.         }
  128.         return -1;
  129. }
  130. //切分参数
  131. int args(char* Str, int FLAG)
  132. {
  133.         int i=0;
  134.         if(FLAG==1){
  135.                 ARGS[i]=strtok(Str,  ":");
  136.         }else{
  137.                 ARGS[i]=strtok(Str+1,",");
  138.         }
  139.         while(ARGS[i]!=NULL){ARGS[++i]=strtok(NULL,",");}       
  140.         return i;
  141. }
  142. //读取脚本
  143. int FileScript(char* txtfile, int m, int n);
  144. //参数分析
  145. int AnalysiTreatment(char* command)
  146. {
  147.         int i=0, j=strchp(command, ':');
  148.         switch(j){
  149.                 case -1:
  150.                         if(
  151.                                 command[0]!='\0'&&
  152.                                 command[0]!='\r'&&
  153.                                 command[0]!='\t'&&
  154.                                 command[0]!=' '
  155.                         ){
  156.                                 DrawImg(command, 0, 0, 0, 0, 0, 0, 0, 0);
  157.                         }
  158.                         break;
  159.                 case  0:
  160.                         i=args(command, 0);
  161.                         if      (i==4){
  162.                                 CleanImg(hCMD, atol(ARGS[0]), atol(ARGS[1]), atol(ARGS[2]), atol(ARGS[3]));
  163.                         }else if(
  164.                                 command[1]=='\0'||
  165.                                 command[1]=='\r'||
  166.                                 command[1]=='\t'||
  167.                                 command[1]==' '
  168.                                 ){
  169.                                 HideCursor();
  170.                         }else{
  171.                                 fputs("Error options.\n",stdout);
  172.                         }
  173.                         break;
  174.                 default:
  175.                         i=args(command, 1);
  176.                         if      (command[j+1]==':' && i==3){
  177.                                 FileScript(ARGS[0], atoi(ARGS[1]+1), atoi(ARGS[2]));
  178.                         }else if(command[j+1]==':' && i==2){
  179.                                 FileScript(ARGS[0], atoi(ARGS[1]+1), atoi(ARGS[1]+1));
  180.                         }else if(i==3){
  181.                                 DrawImg(ARGS[0], atoi(ARGS[1]), atoi(ARGS[2]), 0,0,0,0,0,0);
  182.                         }else if(i==5){
  183.                                 DrawImg(ARGS[0], atoi(ARGS[1]), atoi(ARGS[2]), atoi(ARGS[3]), atoi(ARGS[4]), 0,0,0,0);
  184.                         }else if(i==7){
  185.                                 DrawImg(ARGS[0], atoi(ARGS[1]), atoi(ARGS[2]), atoi(ARGS[3]), atoi(ARGS[4]), atoi(ARGS[5]), atoi(ARGS[6]), 0,0);
  186.                         }else if(i==9){
  187.                                 DrawImg(ARGS[0], atoi(ARGS[1]), atoi(ARGS[2]), atoi(ARGS[3]), atoi(ARGS[4]), atoi(ARGS[5]), atoi(ARGS[6]), atoi(ARGS[7]), atoi(ARGS[8]));
  188.                         }else{
  189.                                 fputs("Error options.\n",stdout);
  190.                         }
  191.                         break;
  192.         }       
  193.         return 0;
  194. }
  195. //读取脚本
  196. int FileScript(char* txtfile, int m, int n)
  197. {
  198.         FILE* fp;
  199.         int i=0, j=0, k=0, c=0, MARK_PRE=0, MARK=0, MAX_ARGC=0;
  200.         n=(n<0||n<m)?2147483618:n;
  201.         if( (fp=fopen(txtfile, "rb"))==NULL ){
  202.                 fputs("Failed to read file.\n", stdout);
  203.                 exit(3);
  204.         }
  205.         char* Line=(char*)malloc(1024*sizeof(char));
  206.         while(!feof(fp)){
  207.                 memset(Line, 0,  1024*sizeof(char));
  208.                 fgets(Line, 1023, fp);
  209.                 i++;
  210.                 if(
  211.                         Line[0]=='\0'||
  212.                         Line[0]=='\r'||
  213.                         Line[0]=='\t'||
  214.                         Line[0]==' '
  215.                 ){
  216.                         continue;
  217.                 }
  218.                 if(Line[0]=='+'){
  219.                         MAX_ARGC=args(Line, 0);
  220.                         for(j=0; j<MAX_ARGC; j++){
  221.                                 PEDO[j+1]=atoi(ARGS[j]);       
  222.                         }
  223.                         PEDO[0]=k;
  224.                         continue;
  225.                 }
  226.                 if(
  227.                         Line[0]==':'&&
  228.                         Line[1]==':'

  229.                 ){
  230.                         if(
  231.                                 Line[2]!=':'&&
  232.                                 i>=MARK_PRE
  233.                         ){
  234.                                 k++;
  235.                                 if(k==c){
  236.                                         k=0;
  237.                                         continue;
  238.                                 }
  239.                                 m=atoi(strtok(Line+2,"?"));
  240.                                 c=atoi(strtok(NULL,  "?"));
  241.                                 MARK_PRE=i, i=0;
  242.                                 fseek(fp, 0, SEEK_SET);
  243.                         }
  244.                         else if(
  245.                                 Line[2]==':'&&
  246.                                 i>=m
  247.                         ){
  248.                                 if(Line[3]==':'&&Line[4]==':'){
  249.                                         hCMD=GetDesktopWindow();
  250.                                 }else{
  251.                                         Sleep(atoi(Line+3));
  252.                                 }
  253.                         }
  254.                         continue;
  255.                 }
  256.                 if(m<=i && i<=n){
  257.                         AnalysiTreatment(Line);
  258.                 }
  259.         }
  260.         free(Line);
  261.         fclose(fp);
  262.         return 0;
  263. }

  264. /*************MAIN主函数入口*************/
  265. int main(int argc, char** argv)
  266. {
  267.         if(argc==2){
  268.                 hCMD=GetConsoleWindow();
  269.                 AnalysiTreatment(argv[1]);
  270.                 return 0;
  271.         }
  272.         fputs(
  273.                 "THE CONSOLE DISPLAYS A PICTURE TOOL, VERSION 1.2\n"
  274.                 "COPYRIGHT@2016~2018 BY HAPPY\n"
  275.                 "-------------------------------------------------------------------------\n"
  276.                 "img [options]\n"
  277.                 "-------------------------------------------------------------------------\n"
  278.                 "    :                        Hide the cursor\n"
  279.                 "    :::[time]                Delay ms\n"
  280.                 "    :::::                    Show images in desktop\n"
  281.                 "    ::[L]?[CYC]              Cycle CYC times from row L\n"
  282.                 "    +{parameters}            Cyclic accumulator\n"
  283.                 "    [imgfile]                Direct display imgfile\n"
  284.                 "    [imgfile]:{parameters}   Display the image with the given parameters\n"
  285.                 "             :{parameters}   Erase the display with the given parameters\n"
  286.                 "    [scriptfile]::[m],[n]    Get commands from [m] to [n] in the sfile\n"
  287.                 "-------------------------------------------------------------------------\n"
  288.                 ,stdout
  289.         );
  290.         exit(1);
  291. }
复制代码
发表于 2016-11-12 08:37:15 | 显示全部楼层

还真是做技术和开发的料子啊。
发表于 2016-11-12 08:58:55 | 显示全部楼层
本帖最后由 523066680 于 2016-11-12 09:05 编辑

嗖噶,有时候还是windows提供的接口比较省事。
比如之前用iconv就觉得有点繁琐,用 stringapiset.h 还直接

不过winapi 上面有一点我觉得很难接受,就是类型的命名感觉很别扭:
  DWORD, LPCTSTR, LRESULT, HWND
发表于 2016-11-12 09:11:44 | 显示全部楼层
应该是图片显示吧
 楼主| 发表于 2016-11-13 00:50:20 | 显示全部楼层
回复 2# codegay
就是用来辅助批处理动画、游戏的,用这个可以放大缩小图片,后续还会添加图像旋转,gif解码,图形变换,伪3D等特效技术。之后还会写个3dimg.exe,用来在控制台窗口显示3d图形、3d建模,3d演示;由于是C语言构建,加上算法每天在不断的优化,性能貌似总比其他语言强太多。而img是一个浅析脚本,img有自己的脚本风格,且支持bat混编。
  1. @echo off&img %~nx0::3,-1&exit /b
  2. >>>IMG图显脚本
  3. :
  4. +0,0,10,0
  5. test.jpg:0,0,0,-1
  6. +0,10
  7. :-10,0,0,800
  8. :::10
  9. >>>
  10. ::12?80

  11. +10,0,0,0
  12. test.jpg:0,0,0,0
  13. +0,10
  14. :-10,0,0,800
  15. :::10
  16. >>>
  17. ::10?80
复制代码
我只得出一个结论:在相同算法前提下,C语言比其他所有高级语言都快,python的大数运算很快,但我用C语言实现的大数运算速度是python的100倍,用C语言实现的其他项目也比C++写的快2到3倍。不过C语言开发效率很慢,你得清楚底层原理,有时还得用内联汇编,一天才写三四百行,慢的要死。

评分

参与人数 1技术 +1 收起 理由
CrLf + 1 666

查看全部评分

 楼主| 发表于 2016-11-13 00:55:02 | 显示全部楼层
回复 3# 523066680
微软那些类型名称,我也看得眼花,正如windows系统,其实只是新瓶装旧酒,微软的一贯作风。
 楼主| 发表于 2016-11-13 00:57:22 | 显示全部楼层
回复 4# pcl_test
是的,只是比bmp.exe功能多了几项,支持的图片格式多了几种,支持img自己的脚本。
发表于 2016-11-13 01:13:35 | 显示全部楼层
回复 7# happy886rr


    版主的意思可能是,字面上,图形和图像包含的东西不一样。图像是像素化的,图形可能是矢量的。(逃
发表于 2016-11-13 01:48:16 | 显示全部楼层
骚年,我看好你哦
话说能丰富一下注释更好...(然而自己的都没加)捂脸...逃
 楼主| 发表于 2016-11-13 10:13:33 | 显示全部楼层
感谢大师提醒,只是自己对语言掌握还不是很熟练,很多非常高深的数学变换,不知道该如何用程序语言去描述。这也是一个思维的转变,就是你必须学会机器的思维过程,写机器能理解的代码。机器喜欢做加法、位运算,所以你要尽量把代码里的乘法除法改成加法或位运算。同时,C语言的位运算速度令我惊呆了。
发表于 2016-11-18 22:34:12 | 显示全部楼层
回复 10# happy886rr


    Xp x64光荣测试失败了
 楼主| 发表于 2016-11-18 22:38:57 | 显示全部楼层
回复 11# 老刘1号


    很正常,这个调用的是gdi32.dll  你的64位xp可能存在兼容性问题。
发表于 2016-11-18 22:49:19 | 显示全部楼层
回复 12# happy886rr


    好吧……
 楼主| 发表于 2016-11-18 23:00:37 | 显示全部楼层
回复 13# 老刘1号
对了,你再试一下image能否在64xp下工作。
 楼主| 发表于 2016-11-18 23:01:40 | 显示全部楼层
你是不是用的64位的cmd
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-3-17 00:42 , Processed in 0.026751 second(s), 9 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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