Read 2D Image from vrml-file img1(Nx, Ny) : R color image img2(Nx, Ny) : G color image img3(Nx, Ny) : B color image Texture2 { image 400 400 3 0x000603 0x010603 0x030703 0x060704 0x060503 0x080703 0x070502 0x070502 0x090504 0x090404 0x090305 0x090307 0x080307 0x070206 0x090307 0x0b0408 } imageデータの並び :[ Nx Ny ] jx ; X-index 左から右 (from Left to Right) jy : Y-index 下から上 (from Down to Up) for jy=1:Ny for jx=1:Nx image(jx,jy) end end 1つのデータ '0x0b0307' が、1ピクセルのRGB(3つの16進数)を表す RGB = [0b 03 07] Copyright (C) 2011, ATR All Rights Reserved. License : New BSD License(see VBMEG_LICENSE.txt)
0001 function [img1,img2,img3] = vb_read_vrml_image(fname) 0002 % Read 2D Image from vrml-file 0003 % 0004 % img1(Nx, Ny) : R color image 0005 % img2(Nx, Ny) : G color image 0006 % img3(Nx, Ny) : B color image 0007 % 0008 % Texture2 { 0009 % image 400 400 3 0010 % 0x000603 0x010603 0x030703 0x060704 0x060503 0x080703 0x070502 0x070502 0011 % 0x090504 0x090404 0x090305 0x090307 0x080307 0x070206 0x090307 0x0b0408 0012 % } 0013 % 0014 % imageデータの並び :[ Nx Ny ] 0015 % jx ; X-index 左から右 (from Left to Right) 0016 % jy : Y-index 下から上 (from Down to Up) 0017 % 0018 % for jy=1:Ny 0019 % for jx=1:Nx 0020 % image(jx,jy) 0021 % end 0022 % end 0023 % 0024 % 1つのデータ '0x0b0307' が、1ピクセルのRGB(3つの16進数)を表す 0025 % RGB = [0b 03 07] 0026 % 0027 % Copyright (C) 2011, ATR All Rights Reserved. 0028 % License : New BSD License(see VBMEG_LICENSE.txt) 0029 0030 key.start = 'Texture2'; 0031 key.start2 = 'image'; 0032 key.end = '}'; 0033 key.braket = '{'; 0034 0035 [Nskip, Nline, Mmax] = vb_read_vrml_line(fname,key); 0036 0037 fid=fopen(fname); 0038 0039 % Skip lines until data 0040 for n=1:Nskip 0041 next_line = fgetl(fid); 0042 end 0043 0044 ix = strfind(next_line,'image'); 0045 0046 if isempty(ix), 0047 % No image data 0048 img1=[];img2=[];img3=[]; 0049 return; 0050 end; 0051 0052 dim = next_line( (ix+5):end ); 0053 Ndim = sscanf(dim,'%d %d %d'); 0054 0055 Nx = Ndim(1); 0056 Ny = Ndim(2); 0057 Nbyte = Ndim(3); 0058 0059 fprintf('NX = %d , NY = %d\n', Nx,Ny); 0060 0061 % Start read data 0062 img = zeros(Nx*Ny,Nbyte); 0063 n2 = 0; 0064 0065 % Read data 0066 for n=1:Nline 0067 next_line = fgetl(fid); 0068 0069 % one line consists of 8 strings 0070 % each string '0x0b0307' represents three 2-char Hex integer 0071 % '0x0b0307' -> '0b' , '03' , '07' 0072 0073 str = strrep(next_line,'0x',''); % eliminate '0x' 0074 x = sscanf( str,'%2x'); % read 2-char Hex integer 0075 M = length(x)/Nbyte; 0076 0077 n1 = n2 + 1; 0078 n2 = n2 + M; 0079 img(n1:n2,:) = reshape(x,[Nbyte M])'; 0080 0081 end 0082 0083 fclose(fid); 0084 0085 if n2 ~= (Nx*Ny), 0086 error('Read format error'); 0087 end 0088 0089 % Reshape 2D-matrix and Scale value 0090 img1 = reshape(img(:,1), [Nx Ny])/256; 0091 img2 = reshape(img(:,2), [Nx Ny])/256; 0092 img3 = reshape(img(:,3), [Nx Ny])/256; 0093