9210的Micaps第15类调色板数据

13 篇文章 0 订阅

Diamond15.java
001  /*******************************************************************************
002  *                                                                              *
003  *    FUNCTION: 处理Micaps的第15类数据文件(调色板数据)                          *
004  *     PACKAGE: cma.micaps.diamond                                              *
005  *    FILENAME: Diamond15.java                                                  *
006  *    LANGUAGE: Java2 v1.4                                                      *
007  *    ORIGINAL: c++                                                             *
008  * DESCRIPTION: Micaps diamond 15 data I/O                                      *
009  *      CREATE: 2001-07-18 2006-06-01 改写为 JAVA 版                            *
010  *      UPDATE: 2006-11-03                                                      *
011  *      AUTHOR: 刘泽军 (BJ0773@gmail.com)                                         *
012  *                                                                              *
013  *******************************************************************************/
014 
015  package  cma.micaps.diamond;
016 
017  import  java.io.*;
018  import  java.util.*;
019  import  java.lang.*;
020  import  java.awt.Color;
021  import  java.text.DecimalFormat;
022 
023  import  cma.common.atmos.*;
024  import  cma.micaps.diamond.datatype.*;
025 
026  public class  Diamond15  {
027 
028       public  static   String  SYMBOL  =  "diamond" ;     //文件标志
029       public  static   int      TYPE    =  15 ;            //文件类型
030       public  static   int      COUNT   =  512 ;           //Micaps系统自定义的最大颜色数
031 
032       public   String  title;       //文件说明
033       public   Color []  rgb;         //颜色值
034 
035       public   String  filename    =  "" ;
036 
037  /**
038    * 功能:
039    *      根据云图像素点的byte值获得对应的颜色索引
040    * 参数:
041    *      value   - 像素点值
042    * 返回值:
043    *          颜色索引
044    * 说明:
045    *
046    */
047       public static  int  getIndex ( byte  value ) { //云图像素点数据对应的颜色索引
048           int  index   =  ( new  Byte ( value )) .intValue () ;
049  // -128 <= index <= 127
050  //   0 - 127 =>   0 - 127
051  //-128 - -1  => 128 - 255
052           if index <  index = index +  256 ;
053           if index <  index =  0 ;
054           if index >  255  index =  255 ;
055           return ( index ) ;
056       }
057 
058  /**
059    * 功能:
060    *      获得指定索引号的颜色值
061    * 参数:
062    *      index   - 索引值
063    * 返回值:
064    *      对应RGB颜色值
065    */
066       public   Color   getColor ( int  index ) {
067           return ( rgb [ index ]) ;
068       }
069 
070  /**
071    * 功能:
072    *      获得指定索引号的颜色值的红、绿、蓝色分量
073    * 参数:
074    *      index   - 索引值
075    * 返回值:
076    *      对应的红、绿、蓝色分量
077    */
078       public   int  getRed ( int  index ) {
079           return ( rgb [ index ] .getRed ()) ;
080       }
081       public   int  getGreen ( int  index ) {
082           return ( rgb [ index ] .getGreen ()) ;
083       }
084       public   int  getBlue ( int  index ) {
085           return ( rgb [ index ] .getBlue ()) ;
086       }
087 
088  /**
089    * 功能:
090    *      重新设置成员数据为缺省值
091    * 参数:
092    *      无
093    * 返回值:
094    *      无
095    */
096       public   void  reset () {
097           title       =  "" ;        //文件说明
098           filename    =  "" ;        //文件名
099           rgb         =  null ;      //颜色值
100       }
101 
102  /**
103    * 功能:
104    *      类构造函数
105    * 参数:
106    *      无
107    * 返回值:
108    *      无
109    */
110       public  Diamond15 () {
111           reset () ;
112       }
113 
114  /**
115    * 功能:
116    *      类构造函数
117    * 参数:
118    *      fname   - Micaps调色板文件名
119    * 返回值:
120    *      无
121    */
122       public  Diamond15 ( String fname ) {
123           reset () ;
124           loadFromFile ( fname ) ;
125       }
126 
127  /**
128    * 功能:
129    *      读取Micaps调色板数据
130    * 参数:
131    *      fname   - Micaps调色板文件名
132    * 返回值:
133    *      是否读取成功
134    */
135       public  boolean  loadFromFile ( String fname ) {
136           if ! ( new  File ( fname )) .exists () ) {
137               System.out.println ( "Diamond15.java #137 : "  + fname +  " not found!" ) ;
138               return ( false ) ;
139           }
140           try  {
141               Vector  vectorData  =  new  Vector () ;
142               InputStreamReader   isReader    =  new  InputStreamReader ( new  FileInputStream ( fname ) "gb2312" ) ; //支持汉字
143               BufferedReader      bufReader   =  new  BufferedReader ( isReader ) ;
144 
145               String  lineString;
146               StringTokenizer st;
147               while null  !=  lineString = bufReader.readLine () ) ) {
148                   st  =  new  StringTokenizer ( lineString, " /r/n" ) ; //按空格、回车、换行符号来分隔文件内容
149                   while st.hasMoreTokens () ) {
150                       vectorData.add ( st.nextToken ()) ;
151                   }
152               }
153               isReader.close () ;
154 
155               title   = vectorData.size ()  >=  ( String ) vectorData.get ( 2 "" ; //文件说明
156               int  minLength   = Math.min ( vectorData.size () * COUNT +  3 ) ; //文件头长度为3项,文件内容为索引 红色分量 绿色分量 蓝色分量共4项
157               int  maxIndex    =  0 ;
158               int  index =  0 , rValue =  0 , gValue =  0 , bValue =  0 ;
159               for ( int  i= 3 ;i<minLength;i=i+ 4 ) {
160                   index       = Integer.parseInt (( String ) vectorData.get ( i+ 0 )) ;
161                   maxIndex    = Math.max ( maxIndex, index ) ;
162               }
163               if maxIndex ==  ) {
164                   return ( false ) ;
165               }
166               rgb =  new  Color [ maxIndex+ 1 ] ;
167               for ( int  i= 0 ;i<=maxIndex;i++ ) { //赋缺省的灰度值
168                   rgb [ i ]   new  Color ( i% 256 , i% 256 , i% 256 ) ;
169               }
170               for ( int  i= 3 ;i<minLength;i=i+ 4 ) {
171                   index       = Integer.parseInt (( String ) vectorData.get ( i+ 0 )) ;
172                   rValue      = Integer.parseInt (( String ) vectorData.get ( i+ 1 ))  256 ;
173                   gValue      = Integer.parseInt (( String ) vectorData.get ( i+ 2 ))  256 ;
174                   bValue      = Integer.parseInt (( String ) vectorData.get ( i+ 3 ))  256 ;
175                   rgb [ index ]   new  Color ( rValue, gValue, bValue ) ;
176               }
177               vectorData  =  null ;
178               filename    = fname;
179               return ( true ) ;
180           }
181           catch ( Exception ex ) {
182               System.out.println ( ex.getMessage ()) ;
183               ex.printStackTrace () ;
184               reset () ;
185               return ( false ) ;
186           }
187       }
188 
189  /**
190    * 功能:
191    *      获得Micaps调色板文件定义的颜色列表
192    * 参数:
193    *      fname   - 文件名
194    * 返回值:
195    *      RGB数组
196    */
197       public static  Color []  getColor ( String fname ) {
198           Diamond15   diamond15   =  new  Diamond15 () ;
199           boolean      enabled     = diamond15.loadFromFile ( fname ) ;
200           return enabled ? diamond15.rgb :  null  ) ;
201       }
202 
203  }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值