📄 xpm.java
字号:
"DeepPink1","DeepPink2","DeepPink3","DeepPink4","HotPink1","HotPink2","HotPink3","HotPink4","pink1","pink2","pink3","pink4","LightPink1","LightPink2","LightPink3","LightPink4","PaleVioletRed1","PaleVioletRed2","PaleVioletRed3","PaleVioletRed4","maroon1","maroon2","maroon3","maroon4","VioletRed1","VioletRed2","VioletRed3","VioletRed4","magenta1","magenta2","magenta3","magenta4","orchid1","orchid2","orchid3","orchid4","plum1","plum2","plum3","plum4","MediumOrchid1","MediumOrchid2","MediumOrchid3","MediumOrchid4","DarkOrchid1","DarkOrchid2","DarkOrchid3","DarkOrchid4","purple1","purple2","purple3","purple4","MediumPurple1","MediumPurple2","MediumPurple3","MediumPurple4","thistle1","thistle2","thistle3","thistle4","gray0","grey0","gray1","grey1","gray2","grey2","gray3","grey3","gray4","grey4","gray5","grey5","gray6","grey6","gray7","grey7","gray8","grey8","gray9","grey9","gray10","grey10","gray11","grey11","gray12","grey12","gray13","grey13","gray14","grey14","gray15","grey15","gray16","grey16","gray17","grey17","gray18","grey18","gray19","grey19","gray20","grey20","gray21","grey21","gray22","grey22","gray23","grey23","gray24","grey24","gray25","grey25","gray26","grey26","gray27","grey27","gray28","grey28","gray29","grey29","gray30","grey30","gray31","grey31","gray32","grey32","gray33","grey33","gray34","grey34","gray35","grey35","gray36","grey36","gray37","grey37","gray38","grey38","gray39","grey39","gray40","grey40","gray41","grey41","gray42","grey42","gray43","grey43","gray44","grey44","gray45","grey45","gray46","grey46","gray47","grey47","gray48","grey48","gray49","grey49","gray50","grey50","gray51","grey51","gray52","grey52","gray53","grey53","gray54","grey54","gray55","grey55","gray56","grey56","gray57","grey57","gray58","grey58","gray59","grey59","gray60","grey60","gray61","grey61","gray62","grey62","gray63","grey63","gray64","grey64","gray65","grey65","gray66","grey66","gray67","grey67","gray68","grey68","gray69","grey69","gray70","grey70","gray71","grey71","gray72","grey72","gray73","grey73","gray74","grey74","gray75","grey75","gray76","grey76","gray77","grey77","gray78","grey78","gray79","grey79","gray80","grey80","gray81","grey81","gray82","grey82","gray83","grey83","gray84","grey84","gray85","grey85","gray86","grey86","gray87","grey87","gray88","grey88","gray89","grey89","gray90","grey90","gray91","grey91","gray92","grey92","gray93","grey93","gray94","grey94","gray95","grey95","gray96","grey96","gray97","grey97","gray98","grey98","gray99","grey99","gray100","grey100","dark grey","DarkGrey","dark gray","DarkGray","dark blue","DarkBlue","dark cyan","DarkCyan","dark magenta","DarkMagenta","dark red","DarkRed","light" }; // rgbents, rgbnames public static boolean debugflag=false; static void debug(String msg) { if(debugflag) System.out.println(msg); } // look up string name of color. return int[3] of rgb, or // null if color not found public static int[] NameToRGB3(String str) { int rsize=rgbnames.length; for(int count=0;count<rsize; count++) { if(str.equalsIgnoreCase(rgbnames[count])) { return rgbents[count]; } } return null; } // This returns an int. If the int were represented as // 0xffffffff, then the format would match data as // 0x00rrggbb //BUT.. returns 0xffffffff if color not found public static int NameToRGB(String str) { int rsize=rgbnames.length; if(str.charAt(0)=='#'){ // Have to deal with rrggbb, OR rrrrggggbbbb // Erm.. except this only deals with // #rrggbb, it looks like... ?!! String Substr=str.substring(1,7); int rgb=Integer.parseInt(Substr,16); rgb|=0xff000000; return rgb; } for(int count=0;count<rsize; count++) { if(str.equalsIgnoreCase(rgbnames[count])) { int ret; ret = 0xff000000 | (rgbents[count][0]<<16) | (rgbents[count][1]<<8) | rgbents[count][2]; return ret; } } if(!str.equalsIgnoreCase("None")) debug("NameToRGB: Could not find match for color "+str); return 0x00000000; } public static String RGB3ToName(int val[]) { if(val.length != 3) return null; int rsize=rgbnames.length; for(int count=0; count<rsize; count++) { if(val[0] ==rgbents[count][0]) if(val[1] ==rgbents[count][1]) if(val[2] ==rgbents[count][2]) return rgbnames[count]; } return null; } /************************************************************ * This part implements reading in xpm data, and doing something * USEFUL with it. This is the only public routine that people * will probably care about. * xpm is possibly copyright/trademarked by Arnaud LE HORS, * BULL Research, France. lehors@sophia.inria.fr ************************************************************/ // we dont care/try to deal with mono conversion public static Image XpmToImage(String xpm) { debug(xpm); /* general rules I'm going to follow: * * skip all (* xxxx*) but possibly insist on initial * (* XPM *) * skip static char ..... * read "<width> <height> <colors> <charsperpixel>" <hotx,hoty>? * Then in main reading; * take c FIRST. fall back to s. fall back to g. fall back to g4. * fall back to m. * * */ if (xpm == null) { debug("XpmToImage : Provided xpm is null!"); return null; } int parse, parseend; int width, height,colcount,charsperpixel; Hashtable colorlookup = new Hashtable(); // add default val for "transparent" // the initial 0xff should mean it should not show up if(! xpm.startsWith("/* XPM */")) { debug("xpm data doesn't start with XPM magic. exit"); debug(xpm.substring(0,10)); return null; } /********************************************* * Do initial width/size,etc parsing **********************************************/ parse = xpm.indexOf('"', 9); parse+=1; parseend=xpm.indexOf(' ', parse); width=Integer.parseInt(xpm.substring(parse,parseend)); parse=parseend+1; parseend=xpm.indexOf(' ', parse); height=Integer.parseInt(xpm.substring(parse,parseend)); parse=parseend+1; parseend=xpm.indexOf(' ', parse); colcount=Integer.parseInt(xpm.substring(parse,parseend)); parse=parseend+1; parseend=xpm.indexOf('"',parse); if(parseend==-1){ return null; } charsperpixel=Integer.parseInt(xpm.substring(parse,parseend)); debug("width="+width+",height="+height+ ",colcount="+colcount+",cpp="+charsperpixel); if(charsperpixel==1){ colorlookup.put(new Integer(' '), new Integer(0x00000000)); } else { int tmpchar=(' ' &0xff) <<8; tmpchar |= (' '&0xff); colorlookup.put(new Integer((tmpchar&0xffff)), new Integer(0x00000000)); } /************************************************** * Now do parsing of color naming/indexing **************************************************/ Image image; int imageb[]; imageb = new int[width * height]; int bytecount=0; // counting bytes into image array parse=xpm.indexOf('"', parseend+1)+1; while(colcount-->0) { if(parse==0){ debug("ERROR: expecting color def"); return null; } Integer colref; Integer rgb; parseend=xpm.indexOf('"', parse+1); String colorname = getColorName(xpm.substring(parse, parseend)); if(debugflag){ debug("colorname on line is "+colorname); debug("now parsing "+ xpm.substring(parse,parse+charsperpixel)); } if(charsperpixel==1){ colref=new Integer(xpm.charAt(parse++)); } else{ int tmpchar=xpm.charAt(parse++); tmpchar = (tmpchar&0xff)<<8; tmpchar |= xpm.charAt(parse++) & 0xff; if(debugflag){ debug("two charsperpixel: substre==" + xpm.substring(parse-2,parse)+ " which generates char "+ (tmpchar&0xffff)); } colref=new Integer(tmpchar&0xffff); } rgb = new Integer(NameToRGB(colorname)); if(debugflag){ debug("Color num parsed for \"" +colorname+"\"("+ Integer.toHexString(colref.intValue())+ ") is #"+ Integer.toHexString(rgb.intValue()) ); } colorlookup.put(colref, rgb); parse=xpm.indexOf('"', parseend+1)+1; } debug("Done with color defs"); /**************************************************** * Finally, now that we have all the data, * fully interpret the actual image ***************************************************/ parse = xpm.indexOf("\n\"", parseend+1); if(parse==-1) { debug("ERROR; incomplete Xpm data"); return null; } parse+=1; debug("Xpm starting image parse"); while(xpm.charAt(parse) =='"') { parse++; parseend = xpm.indexOf('"', parse); debug("Xpm; parsing \""+xpm.substring(parse, parseend)+"\""); for(int pix=parse; pix<parseend;pix++) { int tmpchar; Integer pixchar; Integer pixval; if(charsperpixel==1){ tmpchar=xpm.charAt(pix); } else{ tmpchar=xpm.charAt(pix++); tmpchar = (tmpchar&0xff)<<8; tmpchar |= xpm.charAt(pix) & 0xff; } pixchar=new Integer(tmpchar&0xffff); pixval = (Integer)colorlookup.get(pixchar); if(pixval==null){ if(debugflag){ debug("HEY MORON: no value stored "+ " for int "+ Integer.toHexString(tmpchar)+ ", char substring "+ xpm.substring(pix-charsperpixel, pix)); } } imageb[bytecount++]=pixval.intValue(); } parse=xpm.indexOf('"', parseend+1); if(parse==-1) break; } if(bytecount<(width * height -1)) { debug("Warning.. pixmap truncated!"); } //printbytes(imageb); image=BytesToImage(imageb, width, height); return image; } static Image BytesToImage(int bytes[], int width, int height) { Image myimage; Toolkit tk = Toolkit.getDefaultToolkit(); myimage=tk.createImage(new MemoryImageSource(width, height, bytes, 0, width)); return myimage; } // debug routine static void printbytes(int b[]) { int index; for(index=0; index<b.length; index++) { System.out.print(" "+Integer.toHexString(b[index])); } System.out.println(" "); } // This just parses out the string that defines the colorname // we do NOT interpret. we just split it out from the rest of // the line. Call NameToRGB() on the result. // We make the assumption that our string does NOT // have a trailing '"' static String getColorName(String xpmline) { //debug("getColorName passed: "+xpmline); int parsemid, parsemidend; parsemid = xpmline.indexOf("\tc"); if(parsemid <=0) parsemid = xpmline.indexOf(" c"); if(parsemid <=0) { debug("Xpm: oops. no c found, in: "+xpmline); return null; } parsemid+=3; parsemidend=xpmline.indexOf('\t',parsemid); if(parsemidend==-1) parsemidend=xpmline.length(); return xpmline.substring(parsemid, parsemidend); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -