PNGPhys.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* PNGPhys.java --
  2. Copyright (C) 2006 Free Software Foundation
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package gnu.javax.imageio.png;
  32. /**
  33. * A PNG "pHYS" chunk - pixel physical dimensions
  34. */
  35. public class PNGPhys extends PNGChunk
  36. {
  37. long x, y;
  38. double ratio;
  39. boolean usesRatio;
  40. protected PNGPhys( int type, byte[] data, int crc ) throws PNGException
  41. {
  42. super( type, data, crc );
  43. if( data.length < 9 )
  44. throw new PNGException("Unexpected size of pHYS chunk.");
  45. x = ((data[0] & 0xFF) << 24) | ( (data[1] & 0xFF) << 16 ) |
  46. ((data[2] & 0xFF) << 8) | (data[3] & 0xFF);
  47. y = ((data[4] & 0xFF) << 24) | ( (data[5] & 0xFF) << 16 ) |
  48. ((data[6] & 0xFF) << 8) | (data[7] & 0xFF);
  49. if(data[8] == 0)
  50. {
  51. ratio = ((double)x)/((double)y);
  52. usesRatio = true;
  53. }
  54. }
  55. public PNGPhys( double ratio )
  56. {
  57. super( TYPE_PHYS );
  58. this.ratio = ratio;
  59. usesRatio = true;
  60. if( ratio < 1.0 )
  61. {
  62. y = 0xFFFFFFFF;
  63. x = (long)(0xFFFFFFFFL * ratio);
  64. }
  65. else
  66. {
  67. x = 0xFFFFFFFF;
  68. y = (long)(0xFFFFFFFFL * ratio);
  69. }
  70. makeData();
  71. }
  72. public PNGPhys( int x, int y )
  73. {
  74. super( TYPE_PHYS );
  75. usesRatio = false;
  76. this.x = x;
  77. this.y = y;
  78. makeData();
  79. }
  80. private void makeData()
  81. {
  82. data = new byte[ 9 ];
  83. byte[] a = getInt( (int)x );
  84. byte[] b = getInt( (int)y );
  85. data[0] = a[0]; data[1] = a[1]; data[2] = a[2]; data[3] = a[3];
  86. data[4] = b[0]; data[5] = b[1]; data[6] = b[2]; data[7] = b[3];
  87. data[7] = (usesRatio) ? 0 : (byte)0xFF;
  88. }
  89. public String toString()
  90. {
  91. String s = "PNG Physical pixel size chunk.";
  92. if( usesRatio )
  93. return s + " Aspect ratio (x/y): " + ratio;
  94. else
  95. return s + " " + x + " by " + y + " pixels per meter. (x, y).";
  96. }
  97. }