Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

Dither.java 6.6 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /**
  2. * libcaca Java bindings for libcaca
  3. * Copyright (c) 2009 Adrien Grand <jpountz@dinauz.org>
  4. *
  5. * This library is free software. It comes without any warranty, to
  6. * the extent permitted by applicable law. You can redistribute it
  7. * and/or modify it under the terms of the Do What the Fuck You Want
  8. * to Public License, Version 2, as published by Sam Hocevar. See
  9. * http://www.wtfpl.net/ for more details.
  10. */
  11. package org.zoy.caca;
  12. public class Dither extends NativeObject {
  13. static {
  14. Caca.load();
  15. }
  16. private native static long createDither(int bitmapDepth, int weight, int height,
  17. int pitch, int redMask, int greenMask, int blueMask, int alphaMask);
  18. public Dither(int bitmapDepth, int weight, int height, int pitch, int redMask,
  19. int greenMask, int blueMask, int alphaMask) {
  20. this.ptr = createDither(bitmapDepth, weight, height, pitch, redMask, greenMask,
  21. blueMask, alphaMask);
  22. }
  23. private static native void setDitherPalette(long ditherPtr, int[] red, int[] green,
  24. int[] blue, int[] alpha);
  25. public void setPalette(int[] red, int[] green, int[] blue, int[] alpha) {
  26. if (red.length != 256 ||
  27. green.length != 256 ||
  28. blue.length != 256 ||
  29. alpha.length !=256) {
  30. throw new IllegalArgumentException("Palette components must have 256 elements");
  31. }
  32. setDitherPalette(ptr, red, green, blue, alpha);
  33. }
  34. private static native void setDitherBrightness(long ditherPtr, float brightness);
  35. public void setBrightness(float brightness) {
  36. setDitherBrightness(ptr, brightness);
  37. }
  38. private static native float getDitherBrightness(long ditherPtr);
  39. public float getBrightness() {
  40. return getDitherBrightness(ptr);
  41. }
  42. private static native void setDitherGamma(long ditherPtr, float gama);
  43. public void setGamma(float gama) {
  44. setDitherGamma(ptr, gama);
  45. }
  46. private static native float getDitherGamma(long ditherPtr);
  47. public float getGamma() {
  48. return getDitherGamma(ptr);
  49. }
  50. private static native void setDitherContrast(long ditherPtr, float contrast);
  51. public void setContrast(float contrast) {
  52. setDitherContrast(ptr, contrast);
  53. }
  54. private static native float getDitherContrast(long ditherPtr);
  55. public float getContrast() {
  56. return getDitherContrast(ptr);
  57. }
  58. public static class AntiAliasing extends CacaObject {
  59. public AntiAliasing(String code, String desc) {
  60. super(code, desc);
  61. }
  62. public static AntiAliasing forCode(Dither dither, String code) {
  63. for (AntiAliasing aa : dither.getAntiAliasingList()) {
  64. if (aa.code.equals(code)) {
  65. return aa;
  66. }
  67. }
  68. return null;
  69. }
  70. }
  71. private static native String[] getDitherAntiAliasingList(long ditherPtr);
  72. public AntiAliasing[] getAntiAliasingList() {
  73. String[] tmp = getDitherAntiAliasingList(ptr);
  74. AntiAliasing[] aas;
  75. aas = new AntiAliasing[tmp.length / 2];
  76. for (int i = 0; 2*i < tmp.length; i++) {
  77. aas[i] = new AntiAliasing(tmp[2*i], tmp[2*i+1]);
  78. }
  79. return aas;
  80. }
  81. private static native void setDitherAntiAliasing(long ditherPtr, String antiAliasing);
  82. public void setAntiAliasing(AntiAliasing antiAliasing) {
  83. setDitherAntiAliasing(ptr, antiAliasing.code);
  84. }
  85. private static native String getDitherAntiAliasing(long ditherPtr);
  86. public AntiAliasing getAntiAliasing() {
  87. return AntiAliasing.forCode(this, getDitherAntiAliasing(ptr));
  88. }
  89. public static class Color extends CacaObject {
  90. public Color(String code, String desc) {
  91. super(code, desc);
  92. }
  93. public static Color forCode(Dither dither, String code) {
  94. for (Color color : dither.getColorList()) {
  95. if (color.code.equals(code)) {
  96. return color;
  97. }
  98. }
  99. return null;
  100. }
  101. }
  102. private static native String[] getDitherColorList(long ditherPtr);
  103. public Color[] getColorList() {
  104. String[] tmp = getDitherColorList(ptr);
  105. Color[] colors = new Color[tmp.length / 2];
  106. for (int i = 0; 2*i < tmp.length; i++) {
  107. colors[i] = new Color(tmp[2*i], tmp[2*i+1]);
  108. }
  109. return colors;
  110. }
  111. private static native void setDitherColor(long ditherPtr, String color);
  112. public void setColor(Color color) {
  113. setDitherColor(ptr, color.code);
  114. }
  115. private static native String getDitherColor(long ditherPtr);
  116. public Color getColor() {
  117. return Color.forCode(this, getDitherColor(ptr));
  118. }
  119. public static class Charset extends CacaObject {
  120. public Charset(String code, String desc) {
  121. super(code, desc);
  122. }
  123. public static Charset forCode(Dither dither, String code) {
  124. for (Charset charset : dither.getCharsetList()) {
  125. if (charset.code.equals(code)) {
  126. return charset;
  127. }
  128. }
  129. return null;
  130. }
  131. }
  132. private static native String[] getDitherCharsetList(long charsetPtr);
  133. public Charset[] getCharsetList() {
  134. String[] tmp = getDitherCharsetList(ptr);
  135. Charset[] charsets = new Charset[tmp.length / 2];
  136. for (int i = 0; 2*i < tmp.length; i++) {
  137. charsets[i] = new Charset(tmp[2*i], tmp[2*i+1]);
  138. }
  139. return charsets;
  140. }
  141. private static native void setDitherCharset(long ditherPtr, String charset);
  142. public void setCharset(Charset charset) {
  143. setDitherCharset(ptr, charset.code);
  144. }
  145. private static native String getDitherCharset(long ditherPtr);
  146. public Charset getCharset() {
  147. return Charset.forCode(this, getDitherCharset(ptr));
  148. }
  149. public static class Algorithm extends CacaObject {
  150. public Algorithm(String code, String desc) {
  151. super(code, desc);
  152. }
  153. public static Algorithm forCode(Dither dither, String code) {
  154. for (Algorithm algorithm : dither.getAlgorithmList()) {
  155. if (algorithm.code.equals(code)) {
  156. return algorithm;
  157. }
  158. }
  159. return null;
  160. }
  161. }
  162. private static native String[] getDitherAlgorithmList(long ditherPtr);
  163. public Algorithm[] getAlgorithmList() {
  164. String[] tmp = getDitherAlgorithmList(ptr);
  165. Algorithm[] result = new Algorithm[tmp.length / 2];
  166. for (int i = 0; 2*i < tmp.length; i++) {
  167. result[i] = new Algorithm(tmp[2*i], tmp[2*i+1]);
  168. }
  169. return result;
  170. }
  171. private static native void setDitherAlgorithm(long ditherPtr, String algorithm);
  172. public void setAlgorithm(Algorithm algorithm) {
  173. setDitherAlgorithm(ptr, algorithm.code);
  174. }
  175. private static native String getDitherAlgorithm(long ditherPtr);
  176. public Algorithm getAlgorithm() {
  177. return Algorithm.forCode(this, getDitherAlgorithm(ptr));
  178. }
  179. private static native void freeDither(long ditherPtr);
  180. @Override
  181. public void finalize() throws Throwable {
  182. freeDither(ptr);
  183. super.finalize();
  184. }
  185. }