tga格式转化为jpg格式
综述
很多时候我们需要将tga格式转化为jpg格式。这方面代码很少。我给出一个使用matlab实现的
操作
将下面几个代码全部放到自己的matlab目录下即可:
入口程序main.m
main.m
[I,map] = tga_read_image('testimages/50.tga');figurea =imshow(I,map);saveas(a,'myfig.jpg')
tga_read_image.m
function [I,Map] = tga_read_image(info)
% function for reading image of Truevision TARGA file, TGA, VDA, ICB VST
%
% [I,Map] = tga_read_image(file-header)
%
% or,
%
% [I,Map] = tga_read_image(filename)
%
% examples:
% 1: info = tga_read_header()
% I = tga_read_image(info);
% imshow(I(:,:,1:3),[]);
%
% 2: [I,map] = tga_read_image('testimages/test9.tga');
% figure, imshow(I,map);if(~isstruct(info)), info=tga_read_header(info); end
fid=fopen(info.Filename,'rb','l');if(fid<0)fprintf('could not open file %s\n',info.Filename);return
endfseek(fid,info.HeaderSize,'bof');bytesp=ceil(info.Depth)/8;
npixels=info.Width*info.Height*bytesp;
if(~info.Rle)V = fread(fid,npixels,'uint8=>uint8');
elseV = zeros([npixels 1],'uint8'); nV=0;while(nV<npixels)RCfield=fread(fid,1,'uint8=>uint8');RunLengthPacket=bitget(RCfield,8);Npix=double(mod(RCfield,128))+1;if(RunLengthPacket)PixelData=fread(fid,bytesp,'uint8=>uint8');PixelData=repmat(PixelData(:),[Npix 1]);else % Raw PacketsPixelData=fread(fid,Npix*bytesp,'uint8=>uint8');endnVnew=nV+length(PixelData);V(nV+1:nVnew)=PixelData;nV=nVnew;endV=V(1:npixels);
endswitch(info.Depth)case 8;I = permute(reshape(V,[info.Width info.Height]),[2 1]);case 16V =uint16(V); V=V(1:2:end)+256*V(2:2:end);Vbits=getbits(V,16);B = Vbits(:,1)+Vbits(:,2)*2+Vbits(:,3)*4+Vbits(:,4)*8+Vbits(:,5)*16;G = Vbits(:,6)+Vbits(:,7)*2+Vbits(:,8)*4+Vbits(:,9)*8+Vbits(:,10)*16;R = Vbits(:,11)+Vbits(:,12)*2+Vbits(:,13)*4+Vbits(:,14)*8+Vbits(:,15)*16;R = permute(reshape(R,[info.Width info.Height]),[2 1]);G = permute(reshape(G,[info.Width info.Height]),[2 1]);B = permute(reshape(B,[info.Width info.Height]),[2 1]);I(:,:,1)=R*8;I(:,:,2)=G*8;I(:,:,3)=B*8;I=uint8(I);case 24I = permute(reshape(V,[3 info.Width info.Height]),[3 2 1]);I=I(:,:,3:-1:1);case 32I = permute(reshape(V,[4 info.Width info.Height]),[3 2 1]);I=I(:,:,[3 2 1 4]);
end
fclose(fid);Map = info.ColorMap;switch(info.ImageOrigin);case 'bottom left'I=I(end:-1:1,:,:);case 'bottom right'I=I(end:-1:1,end:-1:1,:);case 'top left'I=I(:,:,:);case 'top right'I=I(:,end:-1:1,:);
endfunction bits=getbits(a,nbits)
a=double(a(:));
bits=zeros([length(a) nbits]);
for i=1:nbits, a=a/2; af=floor(a); bits(:,i)=af~=a; a=af;
end
tga_read_header
function info = tga_read_header(fname)
% function for reading header of Truevision TARGA file, TGA, VDA, ICB VST
%
% info = tga_read_header(filename);
%
% examples:
% 1, info=tga_read_header()
% 2, info=tga_read_header('example.tga');%
% typedef struct
%
% byte identsize; // size of ID field that follows 18 byte header (0 usually)
% byte Colormaptype; // type of Color map 0=none, 1=has palette
% byte imagetype; // type of image 0=none,1=indexed,2=rgb,3=grey,+8=rle packed
%
% short Colormapstart; // first Color map entry in palette
% short Colormaplength; // number of Colors in palette
% byte Colormapbits; // number of bits per palette entry 15,16,24,32%
%
% short xstart; // image x origin
% short ystart; // image y origin
% short width; // image width in pixels
% short height; // image height in pixels
% byte bits; // image bits per pixel 8,16,24,32
% byte descriptor; // image descriptor bits (vh flip bits)
%
% // pixel data follows header
% 24 bits
% TGA_HEADERif(exist('fname','var')==0)[filename, pathname] = uigetfile('*.tga;*.vda;*.icb,*.vst', 'Read tga-file');fname = [pathname filename];
endf=fopen(fname,'rb','l');
if(f<0)fprintf('could not open file %s\n',fname);return
end
info.Filename=fname;% Footer Header
fseek(f,-26,'eof');
info.ExtensionArea=fread(f, 1, 'long');
info.DeveloperDirectory=fread(f, 1, 'long');
info.Signature=fread(f, 17, 'char=>char')';
if(strcmp(info.Signature,'TRUEVISION-XFILE.'))info.Version='new';
elseinfo.Version='old';
end% File Start Header
fseek(f,0,'bof');
info.IDlength = fread(f, 1, 'uint8');
info.ColorMapType = fread(f, 1, 'uint8');
info.ImageType =fread(f, 1, 'uint8');
switch(info.ImageType )case 0info.ImageTypeString='No Image Data';info.Rle=false;case 1info.ImageTypeString='Uncompressed, Color-mapped image'; info.Rle=false;case 2info.ImageTypeString='Uncompressed, True-color image';info.Rle=false;case 3info.ImageTypeString='Uncompressed, True-color image';info.Rle=false;case 9info.ImageTypeString='Run-length encoded Color-mapped Image'; info.Rle=true;case 10info.ImageTypeString='Run-length encoded True-color Image Image'; info.Rle=true;case 11info.ImageTypeString='Run-length encoded Black-and-white Image';info.Rle=true;otherwiseinfo.ImageTypeString='unknown';info.Rle=false;
end% Color Map Specification (offset 3)
info.ColorMapStart = fread(f, 1, 'short');
info.ColorMapLength = fread(f, 1, 'short');
info.ColorMapBits = fread(f, 1, 'uint8');
info.ColorMapStoreBits = 8*ceil(info.ColorMapBits/8);% Image specification offset 8
info.XOrigin = fread(f, 1, 'short');
info.YOrigin = fread(f, 1, 'short');
info.Width = fread(f, 1, 'short');
info.Height = fread(f, 1, 'short');
info.Depth = fread(f, 1, 'uint8');
b= bitget(fread(f, 1, 'uint8=>uint8'),1:8);
info.ImageDescriptor =b;
info.AlphaChannelBits=b(1)+b(2)*2+b(3)*4+b(4)*8;
if((b(6)==0)&&(b(5)==0)), info.ImageOrigin='bottom left'; end
if((b(6)==0)&&(b(5)==1)), info.ImageOrigin='bottom right'; end
if((b(6)==1)&&(b(5)==0)), info.ImageOrigin='top left'; end
if((b(6)==1)&&(b(5)==1)), info.ImageOrigin='top right'; end
info.ImageID= fread(f, info.IDlength, 'uint8');
if(info.ColorMapType==0)info.ColorMap=[];
elseColorMap = fread(f,info.ColorMapLength*(info.ColorMapStoreBits/8),'uint8=>uint8');switch(info.ColorMapStoreBits)case 16% BitsPerColor = min( info.bits/3, 8);%info.ColorMap=reshape(ColorMap,[info.ColorMapLength 2]);case 24info.ColorMap=reshape(ColorMap,[3 info.ColorMapLength])';info.ColorMap=info.ColorMap(:,3:-1:1);case 32info.ColorMap=reshape(ColorMap,[4 info.ColorMapLength])';info.ColorMap=info.ColorMap(:,[3 2 1 4]);endinfo.ColorMap=double(info.ColorMap)/255;
end% Get the header size
info.HeaderSize=ftell(f);% Get the file length
fseek(f,0,'eof');
info.FileSize = ftell(f); fclose(f);% Closing Header Contains :% Developer Fields
% Developer Directory
% Extension Size (offset =0) , short
% Author Name (offset = 2) , asci 41
% Author Comments ( offset = 43), asci 324
% Date Time, (offset=367), shorts 6
% Job Name/ID(offset=379) , 41 ascii
% Job Time (offset = 420) , 41 asci
% Software version (offset=467), 3 bytes
% Key color (offset=470), long
% Pixel Aspect Ratio (offset = 474) , 2shorts
% Gamma Value (offset = 478 ), 2 shorts
% Color Correction Offset (offset=482), long
% Postage Stamp Offset(offset=486), long
% Scan Line Offset ( offset=490), long
% Attributes Type (offset=494)
% Scan Line Table
% Postage Stamp Image
% Color Correction Table (1K shorts)
% Extentsion Area (offset=0), long
% Developer Directory (offset=4),long
% Signature (offset=8) , ASCI
tga格式转化为jpg格式相关推荐
- 如何将caj格式转化为word格式
如何将caj格式转化为word格式 caj文件的识别: 1)局部文字识别:直接使用caj浏览器的ocr 2)全文件识别:打印到Microsoft Office document Image Writ ...
- 批量将OFD格式转化为PDF格式文件(来源于chatgpt)
批量将OFD格式转化为PDF格式文件 一.pyofd库 预先下载pip install pyofd. import os from pyofd import OFDFile from pyofd.re ...
- 目标检测,将voc格式转化为coco格式详细过程
在目标检测方法研究中,数据集的格式至关重要,为了减小模型的训练时长,需要现在小数据集进行模型研究,从而需要将VOC数据集转化为coco格式.下面一步一步来看: 1. 下载VOC数据集 Pascal V ...
- 将搜狗词库.scel格式转化为.txt格式
[2020年5月28日更新:有一说一,这篇文章是我2017年底在新浪工作时处理家居.房产频道相关业务时的实践,代码是后来从自己代码库直接粘贴的,当然转码部分的代码是借鉴的,当时也是查阅了几种方法,一一 ...
- 将自己手动标注的数据集(PascalVOC格式)转化为.TFRecord格式
" 一个人如果不能学会遗忘,那将是很痛苦的事,别再自寻烦恼,快把痛苦的事给忘了吧!" 为了能够使用Object Detection API~ 需要将数据集格式转化为.TFRecor ...
- DGN格式转化为shp格式 【转】
其实本来,我就是需要把一个autocad的dwg/dgn格式的东西导入到google earth里面:但是首先我对dwg/dgn格式的东西根本就不熟:其次我拿到的dwg/dgn格式文件是用的HK80 ...
- DGN格式转化为shp格式
其实本来,我就是需要把一个autocad的dwg/dgn格式的东西导入到google earth里面:但是首先我对dwg/dgn格式的东西根本就不熟:其次我拿到的dwg/dgn格式文件是用的HK80 ...
- Json格式转化为string格式
今天在学习Jsonp的时候,想通过服务端返回一段json数据,因为使用的服务端是NodeJs,那么就需要在js文件中实现返回json.(这里不懂的同学,可以先了解一下NodeJs的基础概念,在这里,我 ...
- 利用python实现 CAD STEP格式转化为STL格式
由于STEP格式需要专门的制图工具才能打开,有一些客户会要求以格式更为简单的STL 显示,代码如下: 1:下载FreeCAD https://www.freecadweb.org/wiki/Down ...
- 利用python把dcm格式转化为jpg格式
dcm文件是DICOM(Digital Imaging and Communications in Medicine)即医学数字成像和通信中记录医学图像和相关信息的文件,在用于医学图像处理的时候我们需 ...
最新文章
- 值得分享!它们才是真正的宝藏网站,让人惊艳
- 深度学习多变量时间序列预测:Encoder-Decoder LSTM算法构建时间序列多变量模型预测交通流量+代码实战
- [原创]使用 NodeJS, MarkdownJS, PrettifyJS 打造个人博客写作平台 - 整体思路
- libevent 实现的socket 通信 server以及解决找不到动态库的方法
- 水池数目---深搜思想
- 写在2021: 值得关注/学习的前端框架和工具库
- 谈判失败:Oracle 杀死 Java EE
- 线阵相机工作模式解读
- 要使一个问题能够用计算机解决,如何正确并解决在使用计算机中的问题?
- JS的构造及其事件注意点总结
- 企业如何从大数据系统中获益
- openssl 升级
- python下载慢怎么办?
- Mac和Windows系统中ssh密钥的生成和共享
- 超级签补充-IOS描述文件mobileconfig的签名认证
- 怎样利用关键词查排名的工具找到行业相关网站
- [小说]魔王冢(63)正牌蚩尤
- 下载VirtualBox,创建虚拟机,安装Linux系统(Ubuntu版)
- 模具腐蚀皮纹工艺原理及其流程
- 去哪儿实习面经(拿到offer)
热门文章
- android游戏手柄怎么用,王者荣耀怎么用手柄玩?手柄游戏详细教程
- Java-买卖股票的最佳时机
- 检查日期是否为节假日api
- 【xctf之easyphp】
- Openwrt/Lede软路由设置为旁路由模式
- python矩阵运算算法_python 矩阵运算
- “ji32k7au4a83”是一个弱密码?
- 笔记本无法启用免费wifi
- Ubuntu18.04安装搜狗输入法无法切换中英文
- python与数据库实现报表的分组统计_报表排版与布局:数据分组和统计(RDL/页面报表)...