/src/tool/TOOL/Image/RGBImage.java

https://github.com/nterman/nbites · Java · 107 lines · 71 code · 23 blank · 13 comment · 6 complexity · a90095a7f2b687946da7a429b518a6f2 MD5 · raw file

  1. package TOOL.Image;
  2. import java.awt.Color;
  3. import java.awt.image.BufferedImage;
  4. import java.awt.image.ColorModel;
  5. import java.io.DataInputStream;
  6. import java.io.EOFException;
  7. import java.io.IOException;
  8. import java.io.BufferedOutputStream;
  9. import java.io.FileOutputStream;
  10. import TOOL.TOOL;
  11. /**
  12. * Reads the Webots simulator's packed RGB format and converts into usable form
  13. * for us.
  14. * @author Northern Bites Team
  15. * @author http://robocup.bowdoin.edu/blog/history
  16. */
  17. public class RGBImage extends TOOLImage {
  18. public static final String extension = ".raw";
  19. public static final int RAW_HEADER_SIZE = 0;
  20. public static final int BYTES_PER_TWO_PIXELS = 6;
  21. public static final int COLOR_DEPTH = 3;
  22. public static final int Y1_OFFSET = 0;
  23. public static final int U_OFFSET = 1;
  24. public static final int Y2_OFFSET = 2;
  25. public static final int V_OFFSET = 3;
  26. public RGBImage(byte[] rawImage, int w, int h) {
  27. super(w, h);
  28. pixels = new byte[h][w][COLOR_DEPTH];
  29. readByteArray(rawImage);
  30. }
  31. public RGBImage(DataInputStream input, int width, int height)
  32. throws IOException {
  33. super(width, height);
  34. pixels = new byte[height][width][COLOR_DEPTH];
  35. readInputStream(input);
  36. }
  37. public int rawImageSize() {
  38. return getWidth() * getHeight() * COLOR_DEPTH;
  39. }
  40. // Color Format: RGB
  41. // Byte order: Interleaved, RGB...RGB
  42. //
  43. public void readByteArray(byte[] rawImage) {
  44. int i = 0;
  45. for (int r = 0; r < getHeight(); r++) {
  46. for (int c = 0; c < getWidth(); c++) {
  47. pixels[r][c][0] = rawImage[i++];
  48. pixels[r][c][1] = rawImage[i++];
  49. pixels[r][c][2] = rawImage[i++];
  50. }
  51. }
  52. }
  53. public void writeByteArray(byte[] rawImage) {
  54. int i = 0;
  55. for (int r = 0; r < getHeight(); r++) {
  56. for (int c = 0; c < getWidth(); c+=2, i+=3) {
  57. rawImage[i ] = pixels[r][c][0];
  58. rawImage[i+1] = pixels[r][c][1];
  59. rawImage[i+2] = pixels[r][c][2];
  60. }
  61. }
  62. }
  63. public void initImage(BufferedImage img) {
  64. ColorModel cm = img.getColorModel();
  65. int[] rgb = new int[3];
  66. for (int r = 0; r < getHeight(); r++) {
  67. for (int c = 0; c < getWidth(); c++) {
  68. // Fill the integer rgb array with byte values
  69. rgb[0] = pixels[r][c][0];
  70. rgb[1] = pixels[r][c][1];
  71. rgb[2] = pixels[r][c][2];
  72. // Set the BufferedImage pixel to the rgb value of this pixel
  73. // (use the ColorModel to convert from components to int)
  74. img.setRGB(c, r, cm.getDataElement(rgb, 0));
  75. }
  76. }
  77. }
  78. // Overridden getYCbCr() method, to convert values
  79. public int[] getYCbCr(int x, int y) {
  80. int[] yCbCr = new int[COLOR_DEPTH];
  81. YCbCr_CS.fromRGB(pixels[y][x], yCbCr);
  82. return yCbCr;
  83. }
  84. }