Java - JMF Simple Filter


/ Published in: Java



Copy this code and paste it in your HTML
  1. import java.awt.Dimension;
  2.  
  3. import javax.media.Buffer;
  4. import javax.media.Effect;
  5. import javax.media.Format;
  6. import javax.media.ResourceUnavailableException;
  7. import javax.media.format.RGBFormat;
  8.  
  9. public class SimpleFilter implements Effect
  10. {
  11. protected Format inputFormat = null;
  12. protected Format outputFormat = null;
  13.  
  14. protected Format[] inputFormats = null;
  15. protected Format[] outputFormats = null;
  16.  
  17. public AngelMotionCodec()
  18. {
  19. inputFormats = new Format[]{ new RGBFormat(null, Format.NOT_SPECIFIED, Format.byteArray, Format.NOT_SPECIFIED, 24, 3, 2, 1, 3, Format.NOT_SPECIFIED, Format.TRUE, Format.NOT_SPECIFIED) };
  20. outputFormats = new Format[]{ new RGBFormat(null, Format.NOT_SPECIFIED, Format.byteArray, Format.NOT_SPECIFIED, 24, 3, 2, 1, 3, Format.NOT_SPECIFIED, Format.TRUE, Format.NOT_SPECIFIED) };
  21. }
  22.  
  23. /****** Codec ******/
  24. public Format[] getSupportedInputFormats()
  25. {
  26. return inputFormats;
  27. }
  28.  
  29. public Format[] getSupportedOutputFormats(Format input)
  30. {
  31. if(input != null)
  32. {
  33. if(matches(input, inputFormats) != null)
  34. return new Format[]{ outputFormats[0].intersects(input) };
  35. else
  36. return new Format[0];
  37. }
  38.  
  39. return outputFormats;
  40. }
  41.  
  42. public int process(Buffer input, Buffer output)
  43. {
  44. // Swap tra input & output
  45. Object tmp = input.getData();
  46.  
  47. input.setData(output.getData());
  48. output.setData(tmp);
  49.  
  50. return BUFFER_PROCESSED_OK;
  51. }
  52.  
  53. public Format setInputFormat(Format input)
  54. {
  55. inputFormat = input;
  56.  
  57. return input;
  58. }
  59.  
  60. public Format setOutputFormat(Format output)
  61. {
  62. if(output != null || matches(output, outputFormats) != null)
  63. {
  64. RGBFormat inRGB = (RGBFormat) output;
  65.  
  66. Dimension size = inRGB.getSize();
  67. int maxDataLength = inRGB.getMaxDataLength();
  68. int lineStride = inRGB.getLineStride();
  69. int flipped = inRGB.getFlipped();
  70.  
  71. if(size == null)
  72. return null;
  73.  
  74. if(maxDataLength < size.width*size.height*3)
  75. maxDataLength = size.width*size.height*3;
  76.  
  77. if(lineStride < size.width*3)
  78. lineStride = size.width*3;
  79.  
  80. if(flipped != Format.FALSE)
  81. flipped = Format.FALSE;
  82.  
  83. outputFormat = outputFormats[0].intersects(new RGBFormat(size, maxDataLength, inRGB.getDataType(), inRGB.getFrameRate(), inRGB.getBitsPerPixel(), inRGB.getRedMask(), inRGB.getGreenMask(), inRGB.getBlueMask(), inRGB.getPixelStride(), lineStride, flipped, inRGB.getEndian()));
  84.  
  85. return outputFormat;
  86. }
  87.  
  88. return null;
  89. }
  90. /****** Codec ******/
  91.  
  92. /****** PlugIn ******/
  93. public void close()
  94. {
  95.  
  96. }
  97.  
  98. public String getName()
  99. {
  100. return "Simple-Filter";
  101. }
  102.  
  103. public void open() throws ResourceUnavailableException
  104. {
  105.  
  106. }
  107.  
  108. public void reset()
  109. {
  110.  
  111. }
  112. /****** PlugIn ******/
  113.  
  114. /****** Controls ******/
  115. public Object getControl(String controlType)
  116. {
  117. return null;
  118. }
  119.  
  120. public Object[] getControls()
  121. {
  122. return null;
  123. }
  124. /****** Controls ******/
  125.  
  126. /****** Utility ******/
  127. private Format matches(Format in, Format[] out)
  128. {
  129. if(in != null && out != null)
  130. {
  131. for(int i=0, cnt=out.length; i<cnt; i++)
  132. {
  133. if(in.matches(out[i]))
  134. return out[i];
  135. }
  136. }
  137.  
  138. return null;
  139. }
  140. /****** Utility ******/
  141. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.