Advertisement
Guest User

DDSConverter.java

a guest
Jun 27th, 2013
2,257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.19 KB | None | 0 0
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package com.l2revive.antibot;
  6.  
  7. import java.awt.Font;
  8. import java.awt.FontMetrics;
  9. import java.awt.Graphics2D;
  10. import java.awt.RenderingHints;
  11. import java.awt.image.BufferedImage;
  12. import java.io.File;
  13. import java.io.IOException;
  14. import java.nio.ByteBuffer;
  15. import java.nio.ByteOrder;
  16. import java.util.Random;
  17. import java.util.logging.Logger;
  18. import javax.imageio.ImageIO;
  19. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  20. import net.sf.l2j.gameserver.network.serverpackets.PledgeCrest;
  21.  
  22. public class DDSConverter {
  23.  
  24. private static final Logger _log = Logger.getLogger(DDSConverter.class.getName());
  25.  
  26. protected static class Color {
  27.  
  28. @Override
  29. public boolean equals(Object obj) {
  30. if (this == obj) {
  31. return true;
  32. }
  33. if ((obj == null) || (getClass() != obj.getClass())) {
  34. return false;
  35. }
  36. Color color = (Color) obj;
  37. if (b != color.b) {
  38. return false;
  39. }
  40. if (g != color.g) {
  41. return false;
  42. }
  43. return r == color.r;
  44. }
  45.  
  46. @Override
  47. public int hashCode() {
  48. int i = r;
  49. i = (29 * i) + g;
  50. i = (29 * i) + b;
  51. return i;
  52. }
  53. protected int r;
  54. protected int g;
  55. protected int b;
  56.  
  57. public Color() {
  58. r = g = b = 0;
  59. }
  60.  
  61. public Color(int i, int j, int k) {
  62. r = i;
  63. g = j;
  64. b = k;
  65. }
  66. }
  67.  
  68. public static ByteBuffer convertToDDS(File file) throws IOException {
  69. if (file == null) {
  70. String s = "nullValue.FileIsNull";
  71. _log.severe(s);
  72. throw new IllegalArgumentException(s);
  73. }
  74. if (!file.exists() || !file.canRead()) {
  75. String s1 = "DDSConverter.NoFileOrNoPermission";
  76. _log.severe(s1);
  77. throw new IllegalArgumentException(s1);
  78. }
  79. BufferedImage bufferedimage = ImageIO.read(file);
  80. if (bufferedimage == null) {
  81. return null;
  82. }
  83. if (bufferedimage.getColorModel().hasAlpha()) {
  84. return convertToDxt3(bufferedimage);
  85. }
  86. return convertToDxt1NoTransparency(bufferedimage);
  87. }
  88.  
  89. public static ByteBuffer convertToDDS(BufferedImage bufferedimage) {
  90. if (bufferedimage == null) {
  91. return null;
  92. }
  93. if (bufferedimage.getColorModel().hasAlpha()) {
  94. return convertToDxt3(bufferedimage);
  95. }
  96. return convertToDxt1NoTransparency(bufferedimage);
  97. }
  98.  
  99. public static ByteBuffer convertToDxt1NoTransparency(BufferedImage bufferedimage) {
  100. if (bufferedimage == null) {
  101. return null;
  102. }
  103. int ai[] = new int[16];
  104. int i = 128 + ((bufferedimage.getWidth() * bufferedimage.getHeight()) / 2);
  105. ByteBuffer bytebuffer = ByteBuffer.allocate(i);
  106. bytebuffer.order(ByteOrder.LITTLE_ENDIAN);
  107. buildHeaderDxt1(bytebuffer, bufferedimage.getWidth(), bufferedimage.getHeight());
  108. int j = bufferedimage.getWidth() / 4;
  109. int k = bufferedimage.getHeight() / 4;
  110. for (int l = 0; l < k; l++) {
  111. for (int i1 = 0; i1 < j; i1++) {
  112. BufferedImage bufferedimage1 = bufferedimage.getSubimage(i1 * 4, l * 4, 4, 4);
  113. bufferedimage1.getRGB(0, 0, 4, 4, ai, 0, 4);
  114. Color acolor[] = getColors888(ai);
  115. for (int j1 = 0; j1 < ai.length; j1++) {
  116. ai[j1] = getPixel565(acolor[j1]);
  117. acolor[j1] = getColor565(ai[j1]);
  118. }
  119.  
  120. int ai1[] = determineExtremeColors(acolor);
  121. if (ai[ai1[0]] < ai[ai1[1]]) {
  122. int k1 = ai1[0];
  123. ai1[0] = ai1[1];
  124. ai1[1] = k1;
  125. }
  126. bytebuffer.putShort((short) ai[ai1[0]]);
  127. bytebuffer.putShort((short) ai[ai1[1]]);
  128. long l1 = computeBitMask(acolor, ai1);
  129. bytebuffer.putInt((int) l1);
  130. }
  131. }
  132. return bytebuffer;
  133. }
  134.  
  135. public static ByteBuffer convertToDxt3(BufferedImage bufferedimage) {
  136. if (bufferedimage == null) {
  137. return null;
  138. }
  139. if (!bufferedimage.getColorModel().hasAlpha()) {
  140. return convertToDxt1NoTransparency(bufferedimage);
  141. }
  142. int ai[] = new int[16];
  143. int i = 128 + (bufferedimage.getWidth() * bufferedimage.getHeight());
  144. ByteBuffer bytebuffer = ByteBuffer.allocate(i);
  145. bytebuffer.order(ByteOrder.LITTLE_ENDIAN);
  146. buildHeaderDxt3(bytebuffer, bufferedimage.getWidth(), bufferedimage.getHeight());
  147. int j = bufferedimage.getWidth() / 4;
  148. int k = bufferedimage.getHeight() / 4;
  149. for (int l = 0; l < k; l++) {
  150. for (int i1 = 0; i1 < j; i1++) {
  151. BufferedImage bufferedimage1 = bufferedimage.getSubimage(i1 * 4, l * 4, 4, 4);
  152. bufferedimage1.getRGB(0, 0, 4, 4, ai, 0, 4);
  153. Color acolor[] = getColors888(ai);
  154. for (int j1 = 0; j1 < ai.length; j1 += 2) {
  155. bytebuffer.put((byte) ((ai[j1] >>> 28) | (ai[j1 + 1] >>> 24)));
  156. }
  157.  
  158. for (int k1 = 0; k1 < ai.length; k1++) {
  159. ai[k1] = getPixel565(acolor[k1]);
  160. acolor[k1] = getColor565(ai[k1]);
  161. }
  162.  
  163. int ai1[] = determineExtremeColors(acolor);
  164. if (ai[ai1[0]] < ai[ai1[1]]) {
  165. int l1 = ai1[0];
  166. ai1[0] = ai1[1];
  167. ai1[1] = l1;
  168. }
  169. bytebuffer.putShort((short) ai[ai1[0]]);
  170. bytebuffer.putShort((short) ai[ai1[1]]);
  171. long l2 = computeBitMask(acolor, ai1);
  172. bytebuffer.putInt((int) l2);
  173. }
  174. }
  175. return bytebuffer;
  176. }
  177.  
  178. protected static void buildHeaderDxt1(ByteBuffer bytebuffer, int i, int j) {
  179. bytebuffer.rewind();
  180. bytebuffer.put((byte) 68);
  181. bytebuffer.put((byte) 68);
  182. bytebuffer.put((byte) 83);
  183. bytebuffer.put((byte) 32);
  184. bytebuffer.putInt(124);
  185. int k = 0xa1007;
  186. bytebuffer.putInt(k);
  187. bytebuffer.putInt(j);
  188. bytebuffer.putInt(i);
  189. bytebuffer.putInt((i * j) / 2);
  190. bytebuffer.putInt(0);
  191. bytebuffer.putInt(0);
  192. bytebuffer.position(bytebuffer.position() + 44);
  193. bytebuffer.putInt(32);
  194. bytebuffer.putInt(4);
  195. bytebuffer.put((byte) 68);
  196. bytebuffer.put((byte) 88);
  197. bytebuffer.put((byte) 84);
  198. bytebuffer.put((byte) 49);
  199. bytebuffer.putInt(0);
  200. bytebuffer.putInt(0);
  201. bytebuffer.putInt(0);
  202. bytebuffer.putInt(0);
  203. bytebuffer.putInt(0);
  204. bytebuffer.putInt(4096);
  205. bytebuffer.putInt(0);
  206. bytebuffer.position(bytebuffer.position() + 12);
  207. }
  208.  
  209. protected static void buildHeaderDxt3(ByteBuffer bytebuffer, int i, int j) {
  210. bytebuffer.rewind();
  211. bytebuffer.put((byte) 68);
  212. bytebuffer.put((byte) 68);
  213. bytebuffer.put((byte) 83);
  214. bytebuffer.put((byte) 32);
  215. bytebuffer.putInt(124);
  216. int k = 0xa1007;
  217. bytebuffer.putInt(k);
  218. bytebuffer.putInt(j);
  219. bytebuffer.putInt(i);
  220. bytebuffer.putInt(i * j);
  221. bytebuffer.putInt(0);
  222. bytebuffer.putInt(0);
  223. bytebuffer.position(bytebuffer.position() + 44);
  224. bytebuffer.putInt(32);
  225. bytebuffer.putInt(4);
  226. bytebuffer.put((byte) 68);
  227. bytebuffer.put((byte) 88);
  228. bytebuffer.put((byte) 84);
  229. bytebuffer.put((byte) 51);
  230. bytebuffer.putInt(0);
  231. bytebuffer.putInt(0);
  232. bytebuffer.putInt(0);
  233. bytebuffer.putInt(0);
  234. bytebuffer.putInt(0);
  235. bytebuffer.putInt(4096);
  236. bytebuffer.putInt(0);
  237. bytebuffer.position(bytebuffer.position() + 12);
  238. }
  239.  
  240. protected static int[] determineExtremeColors(Color acolor[]) {
  241. int i = 0x80000000;
  242. int ai[] = new int[2];
  243. for (int j = 0; j < (acolor.length - 1); j++) {
  244. for (int k = j + 1; k < acolor.length; k++) {
  245. int l = distance(acolor[j], acolor[k]);
  246. if (l > i) {
  247. i = l;
  248. ai[0] = j;
  249. ai[1] = k;
  250. }
  251. }
  252.  
  253. }
  254. return ai;
  255. }
  256.  
  257. protected static long computeBitMask(Color acolor[], int ai[]) {
  258. Color acolor1[] = {
  259. null,
  260. null,
  261. new Color(),
  262. new Color()
  263. };
  264. acolor1[0] = acolor[ai[0]];
  265. acolor1[1] = acolor[ai[1]];
  266. if (acolor1[0].equals(acolor1[1])) {
  267. return 0L;
  268. }
  269. acolor1[2].r = ((2 * acolor1[0].r) + acolor1[1].r + 1) / 3;
  270. acolor1[2].g = ((2 * acolor1[0].g) + acolor1[1].g + 1) / 3;
  271. acolor1[2].b = ((2 * acolor1[0].b) + acolor1[1].b + 1) / 3;
  272. acolor1[3].r = (acolor1[0].r + (2 * acolor1[1].r) + 1) / 3;
  273. acolor1[3].g = (acolor1[0].g + (2 * acolor1[1].g) + 1) / 3;
  274. acolor1[3].b = (acolor1[0].b + (2 * acolor1[1].b) + 1) / 3;
  275. long l = 0L;
  276. for (int i = 0; i < acolor.length; i++) {
  277. int j = 0x7fffffff;
  278. int k = 0;
  279. for (int i1 = 0; i1 < acolor1.length; i1++) {
  280. int j1 = distance(acolor[i], acolor1[i1]);
  281. if (j1 < j) {
  282. j = j1;
  283. k = i1;
  284. }
  285. }
  286.  
  287. l |= k << (i * 2);
  288. }
  289. return l;
  290. }
  291.  
  292. protected static int getPixel565(Color color) {
  293. int i = color.r >> 3;
  294. int j = color.g >> 2;
  295. int k = color.b >> 3;
  296. return (i << 11) | (j << 5) | k;
  297. }
  298.  
  299. protected static Color getColor565(int i) {
  300. Color color = new Color();
  301. color.r = (int) (i & 63488L) >> 11;
  302. color.g = (int) (i & 2016L) >> 5;
  303. color.b = (int) (i & 31L);
  304. return color;
  305. }
  306.  
  307. protected static Color[] getColors888(int ai[]) {
  308. Color acolor[] = new Color[ai.length];
  309. for (int i = 0; i < ai.length; i++) {
  310. acolor[i] = new Color();
  311. acolor[i].r = (int) (ai[i] & 0xff0000L) >> 16;
  312. acolor[i].g = (int) (ai[i] & 65280L) >> 8;
  313. acolor[i].b = (int) (ai[i] & 255L);
  314. }
  315. return acolor;
  316. }
  317.  
  318. protected static int distance(Color color, Color color1) {
  319. return ((color1.r - color.r) * (color1.r - color.r)) + ((color1.g - color.g) * (color1.g - color.g)) + ((color1.b - color.b) * (color1.b - color.b));
  320. }
  321.  
  322. public static BufferedImage scaleImage(BufferedImage img, int width, int height, java.awt.Color background) {
  323. int imgWidth = img.getWidth();
  324. int imgHeight = img.getHeight();
  325. if (imgWidth * height < imgHeight * width) {
  326. width = imgWidth * height / imgHeight;
  327. } else {
  328. height = imgHeight * width / imgWidth;
  329. }
  330. BufferedImage newImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  331. Graphics2D g = newImage.createGraphics();
  332. try {
  333. g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
  334. RenderingHints.VALUE_INTERPOLATION_BICUBIC);
  335. g.setBackground(background);
  336. g.clearRect(0, 0, width, height);
  337. g.drawImage(img, 0, 0, width, height, null);
  338. } finally {
  339. g.dispose();
  340. }
  341. return newImage;
  342. }
  343.  
  344. /*public static BufferedImage generateImage(String text) {
  345. BufferedImage img = new BufferedImage(128, 32, BufferedImage.TYPE_INT_RGB);
  346. java.awt.Color bgColor = java.awt.Color.GREEN;
  347. Graphics2D g2d = img.createGraphics();
  348. g2d.drawImage(img, 0, 0, null);
  349. g2d.setPaint(java.awt.Color.RED);
  350. g2d.setFont(new Font("Serif", Font.BOLD, 20));
  351. FontMetrics fm = g2d.getFontMetrics();
  352. int x = img.getWidth() - fm.stringWidth(text) - 50;
  353. int y = fm.getHeight() - 4;
  354. g2d.setBackground(bgColor);
  355. g2d.drawString(text, x, y);
  356. g2d.dispose();
  357. return img;
  358. }*/
  359. public static BufferedImage generateImage(String text) {
  360. BufferedImage img = new BufferedImage(128, 32, BufferedImage.TYPE_INT_RGB);
  361. java.awt.Color bgColor = new java.awt.Color(100, 100, 100);
  362. java.awt.Color textColor = new java.awt.Color(160, 160, 160);
  363. java.awt.Color noiseColor = new java.awt.Color(130, 130, 130);
  364. //===
  365. Random rnd = new Random();
  366. switch (rnd.nextInt(10)) {
  367. case 0:
  368. case 1://green
  369. bgColor = new java.awt.Color(0, 100, 0);
  370. textColor = new java.awt.Color(0, 150, 0);
  371. noiseColor = new java.awt.Color(0, 125, 0);
  372. break;
  373. case 2:
  374. case 3://blue
  375. bgColor = new java.awt.Color(0, 0, 100);
  376. textColor = new java.awt.Color(0, 0, 170);
  377. noiseColor = new java.awt.Color(0, 0, 125);
  378. break;
  379. case 4:
  380. case 5://pink
  381. bgColor = new java.awt.Color(100, 0, 100);
  382. textColor = new java.awt.Color(150, 0, 150);
  383. noiseColor = new java.awt.Color(125, 0, 125);
  384. break;
  385. case 6:
  386. case 7:
  387. case 8:
  388. case 9:
  389. }
  390. //===
  391. Graphics2D backColor = img.createGraphics();
  392. backColor.setPaint(bgColor);
  393. backColor.fillRect(0, 0, img.getWidth(), img.getHeight());
  394. backColor.dispose();
  395. //===
  396. for (int i = 0; i < 20; i++) {
  397. Graphics2D g = img.createGraphics();
  398. g.setColor(noiseColor);
  399. int radius = rnd.nextInt(10) + 4;
  400. int x = rnd.nextInt(128) + 1;
  401. int y = rnd.nextInt(32) + 1;
  402. g.fillOval(x, y, radius, radius);
  403. }
  404. //===
  405. Graphics2D g2d = img.createGraphics();
  406. g2d.drawImage(img, 0, 0, null);
  407. g2d.setPaint(textColor);
  408. int fontSize = rnd.nextInt(18) + 12;
  409. g2d.setFont(new Font("Serif", Font.BOLD, fontSize));
  410. FontMetrics fm = g2d.getFontMetrics();
  411. int x2 = img.getWidth() - fm.stringWidth(text) - rnd.nextInt(120 - fontSize);
  412. if (fontSize < 12) {
  413. fontSize = 12;
  414. }
  415. int y2 = fm.getHeight() - (fontSize / 4);//about 4
  416. g2d.drawString(text, x2, y2);
  417. //g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  418. g2d.dispose();
  419. return img;
  420. }
  421.  
  422. public static void sendImage(L2PcInstance player, int id, BufferedImage image) {
  423. try {
  424. PledgeCrest packet = new PledgeCrest(id, DDSConverter.convertToDDS(image).array());
  425. player.sendPacket(packet);
  426. } catch (Exception e) {
  427. }
  428. }
  429. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement