⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mga_tga.c

📁 linux下显示各种格式图片
💻 C
字号:
/* 	(c) HelDoRe	TGA unit ... v0.0.7			Supported formats :			8bpp 			16bpp			24bpp and 24bpp RLE			32bpp and 32bpp RLE			or ImageMagick*/			#define VERTGAS " save v0.0.1"#define VERTGA " unit v0.0.7"#include <stdio.h>#include "../mga_view.h"void help_tga(){#ifdef USE_MAG_TGAprintf("TGA%s\n   + uses ImageMagick\n",VERTGA);#elseprintf("TGA%s\n   +  8 bpp\n   + 16 bpp\n * + 24 bpp\n   + 24 bpp rle\n   + 32 bpp\n   + 32 bpp rle\n",VERTGA);#endif}int detect_tga(){    int flip,type;#ifndef USE_MAG_TGA    int cal,rle,rrle,grle,brle;#endif    fseek(plik,2,SEEK_CUR);    type=fgetc(plik);    fseek(plik,9,SEEK_CUR);    image_x=fgetc16b(plik);    image_y=fgetc16b(plik);    depth=fgetc(plik);    flip=fgetc(plik);    if (check_image(image_x,image_y,depth)==3) return 3;    printf("TGA%s",VERTGA);#ifdef USE_MAG_TGA    printf(" (IM)");#endif    printf("\nForce TGA read  : ");    printf("%ix%ix%i ",image_x,image_y,depth);    if ((type != 1) && (type !=10) && (type !=2)) {	switch (type) {	    case 0: /*printf("No Image Data !\n");*/break;	    case 1: printf("uncompressed color-mapped");break;	    case 2: printf("uncompressed RGB");break;	    case 3: printf("Uncompressed B/W");break;	    case 9: printf("RLE color-mapped");break;	    case 10: printf("RLE RGB");break;	    case 32:printf("Color-mapped Huffman , delta , RLE");break;	    case 33:printf("Color-mapped Huffman , delta , RLE , 4 pass");break;	}#ifndef USE_MAG_TGA		if ((type==3) || (type==9) || (type==32) || (type==33)) { 	printf(" not supported !\n"); //else printf("Unknown file type !\n");	return 2;	}#endif	}    printf("\n");#ifdef USE_MAG_TGA    detect_mag(0);#else    if ((depth !=24) && (depth !=8) && (depth !=32) && (depth !=16) && (depth !=15))     { printf("Only 32 , 24 , 16 ,15 and 8 bit TGA supported\n");        return 2;     }     if ((depth==24) || (depth==32)){     for (y=0;y<image_y;y++)      {      for (x=0;x<image_x;x++)       {       if ((flip==0) || (flip==8))        {         if (type==10) {	 rle=fgetc(plik);	 if (rle==-1) return 0;	 if (rle >= 128) {	    rrle=fgetc(plik);	    grle=fgetc(plik);	    brle=fgetc(plik);	    if (depth==32) fgetc(plik);	    for (tempi=0;tempi<rle-127;tempi++) {	        tabr[x+tempi+(image_y-1-y)*image_x]=rrle;	        tabg[x+tempi+(image_y-1-y)*image_x]=grle;	        tabb[x+tempi+(image_y-1-y)*image_x]=brle;	    }	    x+=tempi-1;	   } else {	    for (tempi=0;tempi<rle+1;tempi++) {	    rrle=fgetc(plik);	    grle=fgetc(plik);	    brle=fgetc(plik);	    if (depth==32) fgetc(plik);	        tabr[x+tempi+(image_y-1-y)*image_x]=rrle;	        tabg[x+tempi+(image_y-1-y)*image_x]=grle;	        tabb[x+tempi+(image_y-1-y)*image_x]=brle;	      }	    x+=tempi-1;	     }	 	 	 } else {	 tabb[x+(image_y-1-y)*image_x]=fgetc(plik);         tabg[x+(image_y-1-y)*image_x]=fgetc(plik);         tabr[x+(image_y-1-y)*image_x]=fgetc(plik);	 if (depth==32) tempi=fgetc(plik);         }	 }       if (flip==32) {         if (type==10) {	 rle=fgetc(plik);	 if (rle >= 128) {	    rrle=fgetc(plik);	    grle=fgetc(plik);	    brle=fgetc(plik);	    if (depth==32) fgetc(plik);	    for (tempi=0;tempi<rle-127;tempi++) {	        tabr[x+tempi+y*image_x]=rrle;	        tabg[x+tempi+y*image_x]=grle;	        tabb[x+tempi+y*image_x]=brle;	    }	    x+=tempi-1;	   } else {	    for (tempi=0;tempi<rle+1;tempi++) {	    rrle=fgetc(plik);	    grle=fgetc(plik);	    brle=fgetc(plik);	    if (depth==32) fgetc(plik);	        tabr[x+tempi+y*image_x]=rrle;	        tabg[x+tempi+y*image_x]=grle;	        tabb[x+tempi+y*image_x]=brle;	      }	    x+=tempi-1;	     }	 	 	 	 	 	 } else {                 tabb[x+y*image_x]=fgetc(plik);         tabg[x+y*image_x]=fgetc(plik);         tabr[x+y*image_x]=fgetc(plik);	 if (depth==32) tempi=fgetc(plik);         }	}       }      }      }            if (depth==8){         for (tempi=0;tempi<256;tempi++) {	   tab2b[tempi]=fgetc(plik);	   tab2g[tempi]=fgetc(plik);	   tab2r[tempi]=fgetc(plik);         }       for (y=0;y<image_y;y++)      {      for (x=0;x<image_x;x++)       {         tempi=fgetc(plik);	 if ((flip==0) || (flip==8)) {         tabr[x+(image_y-y)*image_x]=tab2r[tempi];         tabg[x+(image_y-y)*image_x]=tab2g[tempi];         tabb[x+(image_y-y)*image_x]=tab2b[tempi];	} else {         tabr[x+y*image_x]=tab2r[tempi];         tabg[x+y*image_x]=tab2g[tempi];         tabb[x+y*image_x]=tab2b[tempi];		}	}	}      }     if (depth==16){     for (y=0;y<image_y;y++)      {      for (x=0;x<image_x;x++)       {	 depth=fgetc(plik);	 tempi=fgetc(plik);	cal=((tempi << 1) & 0xf8);	cal+=(cal>>5);         if ((flip==0) || (flip==8)) {	    tabr[x+(image_y-1-y)*image_x]=cal;	    } else tabr[x+y*image_x]=cal;	 cal=((depth & 0xe0)>>2) + ((tempi & 0x03)<<6);	 cal+=(cal>>5);         if ((flip==0) || (flip==8)) {	    tabg[x+(image_y-1-y)*image_x]=cal;	    } else tabg[x+y*image_x]=cal;	cal=((depth<<3) & 0xf8);	cal+=(cal>>5);         if ((flip==0) || (flip==8)) {	    tabb[x+(image_y-1-y)*image_x]=cal;	    } else tabb[x+y*image_x]=cal;	       }      }      }     if (depth==15){     for (y=0;y<image_y;y++)      {      for (x=0;x<image_x;x++)       {	 depth=fgetc(plik);	 tempi=fgetc(plik);	cal=((tempi << 1) & 0xf8);	cal+=(cal>>5);         if ((flip==0) || (flip==8)) {	    tabr[x+(image_y-1-y)*image_x]=cal;	    } else tabr[x+y*image_x]=cal;	 cal=((depth & 0xe0)>>2) + ((tempi & 0x03)<<6);	 cal+=(cal>>5);         if ((flip==0) || (flip==8)) {	    tabg[x+(image_y-1-y)*image_x]=cal;	    } else tabg[x+y*image_x]=cal;	cal=((depth<<3) & 0xf8);	cal+=(cal>>5);         if ((flip==0) || (flip==8)) {	    tabb[x+(image_y-1-y)*image_x]=cal;	    } else tabb[x+y*image_x]=cal;	       }      }      }#endif    return 0;	}int save_tga() {    FILE *zapis;    char nazwa[100];            tempi=0;    do {	nazwa[tempi]=mee[tempi];	tempi++;	}    while ((mee[tempi] != 46) && (tempi!=strlen(mee)));    nazwa[tempi++]=46;    nazwa[tempi++]=116;    nazwa[tempi++]=103;    nazwa[tempi++]=97;    nazwa[tempi++]=0;    printf("TGA%s : Saving 24bpp\n",VERTGAS);    zapis=fopen(nazwa,"w");    fputc(0,zapis);    fputc(0,zapis);    fputc(2,zapis);    for (x=0;x<9;x++) fputc(0,zapis);    fputc((image_x & 0x00ff),zapis);    fputc((image_x & 0xffff)>>8,zapis);    fputc((image_y & 0x00ff),zapis);    fputc((image_y & 0xffff)>>8,zapis);    fputc(24,zapis);    fputc(32,zapis);    for (y=0;y<image_y;y++) {	for (x=0;x<image_x;x++) {	  fputc(tabb[x+(y*image_x)],zapis);	  fputc(tabg[x+(y*image_x)],zapis);	  fputc(tabr[x+(y*image_x)],zapis);	}       }              fclose(zapis);    printf("Done...\n");    return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -