Java Code Examples for java.awt.image.BufferedImage#TYPE_BYTE_GRAY

The following examples show how to use java.awt.image.BufferedImage#TYPE_BYTE_GRAY . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OpCompatibleImageTest.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
private static String describeType(int type) {
    switch(type) {
    case BufferedImage.TYPE_3BYTE_BGR:
        return "TYPE_3BYTE_BGR";
    case BufferedImage.TYPE_4BYTE_ABGR:
        return "TYPE_4BYTE_ABGR";
    case BufferedImage.TYPE_BYTE_GRAY:
        return "TYPE_BYTE_GRAY";
    case BufferedImage.TYPE_INT_ARGB:
        return "TYPE_INT_ARGB";
    case BufferedImage.TYPE_INT_BGR:
        return  "TYPE_INT_BGR";
    case BufferedImage.TYPE_INT_RGB:
        return "TYPE_INT_RGB";
    case BufferedImage.TYPE_BYTE_INDEXED:
        return "TYPE_BYTE_INDEXED";
    default:
        throw new RuntimeException("Test FAILED: unknown type " + type);
    }
}
 
Example 2
Source File: EmbeddedFormatTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
static String getImageTypeName(int type) {
    switch(type) {
        case BufferedImage.TYPE_INT_RGB:
            return "TYPE_INT_RGB";
        case BufferedImage.TYPE_3BYTE_BGR:
            return "TYPE_3BYTE_BGR";
        case BufferedImage.TYPE_USHORT_555_RGB:
            return "TYPE_USHORT_555_RGB";
        case BufferedImage.TYPE_BYTE_GRAY:
            return "TYPE_BYTE_GRAY";
        case BufferedImage.TYPE_BYTE_BINARY:
            return "TYPE_BYTE_BINARY";
        default:
            return "TBD";
    }
}
 
Example 3
Source File: EffectUtils.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * <p>Writes a rectangular area of pixels in the destination <code>BufferedImage</code>. Calling this method on an
 * image of type different from <code>BufferedImage.TYPE_INT_ARGB</code> and <code>BufferedImage.TYPE_INT_RGB</code>
 * will unmanage the image.</p>
 *
 * @param img    the destination image
 * @param x      the x location at which to start storing pixels
 * @param y      the y location at which to start storing pixels
 * @param w      the width of the rectangle of pixels to store
 * @param h      the height of the rectangle of pixels to store
 * @param pixels an array of pixels, stored as integers
 * @throws IllegalArgumentException is <code>pixels</code> is non-null and of length &lt; w*h
 */
static void setPixels(BufferedImage img,
                             int x, int y, int w, int h, byte[] pixels) {
    if (pixels == null || w == 0 || h == 0) {
        return;
    } else if (pixels.length < w * h) {
        throw new IllegalArgumentException("pixels array must have a length >= w*h");
    }
    int imageType = img.getType();
    if (imageType == BufferedImage.TYPE_BYTE_GRAY) {
        WritableRaster raster = img.getRaster();
        raster.setDataElements(x, y, w, h, pixels);
    } else {
        throw new IllegalArgumentException("Only type BYTE_GRAY is supported");
    }
}
 
Example 4
Source File: OpCompatibleImageTest.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
private static String describeType(int type) {
    switch(type) {
    case BufferedImage.TYPE_3BYTE_BGR:
        return "TYPE_3BYTE_BGR";
    case BufferedImage.TYPE_4BYTE_ABGR:
        return "TYPE_4BYTE_ABGR";
    case BufferedImage.TYPE_BYTE_GRAY:
        return "TYPE_BYTE_GRAY";
    case BufferedImage.TYPE_INT_ARGB:
        return "TYPE_INT_ARGB";
    case BufferedImage.TYPE_INT_BGR:
        return  "TYPE_INT_BGR";
    case BufferedImage.TYPE_INT_RGB:
        return "TYPE_INT_RGB";
    case BufferedImage.TYPE_BYTE_INDEXED:
        return "TYPE_BYTE_INDEXED";
    default:
        throw new RuntimeException("Test FAILED: unknown type " + type);
    }
}
 
Example 5
Source File: EffectUtils.java    From Java8CN with Apache License 2.0 6 votes vote down vote up
/**
 * <p>Returns an array of pixels, stored as integers, from a <code>BufferedImage</code>. The pixels are grabbed from
 * a rectangular area defined by a location and two dimensions. Calling this method on an image of type different
 * from <code>BufferedImage.TYPE_INT_ARGB</code> and <code>BufferedImage.TYPE_INT_RGB</code> will unmanage the
 * image.</p>
 *
 * @param img    the source image
 * @param x      the x location at which to start grabbing pixels
 * @param y      the y location at which to start grabbing pixels
 * @param w      the width of the rectangle of pixels to grab
 * @param h      the height of the rectangle of pixels to grab
 * @param pixels a pre-allocated array of pixels of size w*h; can be null
 * @return <code>pixels</code> if non-null, a new array of integers otherwise
 * @throws IllegalArgumentException is <code>pixels</code> is non-null and of length &lt; w*h
 */
static byte[] getPixels(BufferedImage img,
                               int x, int y, int w, int h, byte[] pixels) {
    if (w == 0 || h == 0) {
        return new byte[0];
    }

    if (pixels == null) {
        pixels = new byte[w * h];
    } else if (pixels.length < w * h) {
        throw new IllegalArgumentException("pixels array must have a length >= w*h");
    }

    int imageType = img.getType();
    if (imageType == BufferedImage.TYPE_BYTE_GRAY) {
        Raster raster = img.getRaster();
        return (byte[]) raster.getDataElements(x, y, w, h, pixels);
    } else {
        throw new IllegalArgumentException("Only type BYTE_GRAY is supported");
    }
}
 
Example 6
Source File: BufferedImageLuminanceSource.java    From bicycleSharingServer with MIT License 6 votes vote down vote up
public BufferedImageLuminanceSource(BufferedImage image, int left,
        int top, int width, int height) {
    super(width, height);

    int sourceWidth = image.getWidth();
    int sourceHeight = image.getHeight();
    if (left + width > sourceWidth || top + height > sourceHeight) {
        throw new IllegalArgumentException(
                "Crop rectangle does not fit within image data.");
    }

    for (int y = top; y < top + height; y++) {
        for (int x = left; x < left + width; x++) {
            if ((image.getRGB(x, y) & 0xFF000000) == 0) {
                image.setRGB(x, y, 0xFFFFFFFF); // = white
            }
        }
    }

    this.image = new BufferedImage(sourceWidth, sourceHeight,
            BufferedImage.TYPE_BYTE_GRAY);
    this.image.getGraphics().drawImage(image, 0, 0, null);
    this.left = left;
    this.top = top;
}
 
Example 7
Source File: JPEGFactory.java    From sambox with Apache License 2.0 6 votes vote down vote up
private static BufferedImage getAlphaImage(BufferedImage image)
{
    if (!image.getColorModel().hasAlpha())
    {
        return null;
    }
    if (image.getTransparency() == Transparency.BITMASK)
    {
        throw new UnsupportedOperationException("BITMASK Transparency JPEG compression is not"
                + " useful, use LosslessImageFactory instead");
    }
    WritableRaster alphaRaster = image.getAlphaRaster();
    if (alphaRaster == null)
    {
        // happens sometimes (PDFBOX-2654) despite colormodel claiming to have alpha
        return null;
    }
    BufferedImage alphaImage = new BufferedImage(image.getWidth(), image.getHeight(),
            BufferedImage.TYPE_BYTE_GRAY);
    alphaImage.setData(alphaRaster);
    return alphaImage;
}
 
Example 8
Source File: Wiz4Image.java    From DiskBrowser with GNU General Public License v3.0 6 votes vote down vote up
public Wiz4Image (String name, byte[] buffer, int rows, int cols)   // 5, 6
// ---------------------------------------------------------------------------------//
{
  super (name, buffer);

  image = new BufferedImage (cols * 7, rows * 8, BufferedImage.TYPE_BYTE_GRAY);
  DataBuffer db = image.getRaster ().getDataBuffer ();
  int element = 0;

  int rowSize = cols * 8;
  for (int row = 0; row < rows; row++)
    for (int line = 0; line < 8; line++)
      for (int col = 0; col < cols; col++)
      {
        byte b = buffer[row * rowSize + col * 8 + line];
        for (int bit = 0; bit < 7; bit++)
        {
          if ((b & 0x01) == 0x01)
            db.setElem (element, 255);
          b >>>= 1;
          element++;
        }
      }
}
 
Example 9
Source File: EffectUtils.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * <p>Writes a rectangular area of pixels in the destination <code>BufferedImage</code>. Calling this method on an
 * image of type different from <code>BufferedImage.TYPE_INT_ARGB</code> and <code>BufferedImage.TYPE_INT_RGB</code>
 * will unmanage the image.</p>
 *
 * @param img    the destination image
 * @param x      the x location at which to start storing pixels
 * @param y      the y location at which to start storing pixels
 * @param w      the width of the rectangle of pixels to store
 * @param h      the height of the rectangle of pixels to store
 * @param pixels an array of pixels, stored as integers
 * @throws IllegalArgumentException is <code>pixels</code> is non-null and of length &lt; w*h
 */
static void setPixels(BufferedImage img,
                             int x, int y, int w, int h, byte[] pixels) {
    if (pixels == null || w == 0 || h == 0) {
        return;
    } else if (pixels.length < w * h) {
        throw new IllegalArgumentException("pixels array must have a length >= w*h");
    }
    int imageType = img.getType();
    if (imageType == BufferedImage.TYPE_BYTE_GRAY) {
        WritableRaster raster = img.getRaster();
        raster.setDataElements(x, y, w, h, pixels);
    } else {
        throw new IllegalArgumentException("Only type BYTE_GRAY is supported");
    }
}
 
Example 10
Source File: EffectUtils.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * <p>Returns an array of pixels, stored as integers, from a <code>BufferedImage</code>. The pixels are grabbed from
 * a rectangular area defined by a location and two dimensions. Calling this method on an image of type different
 * from <code>BufferedImage.TYPE_INT_ARGB</code> and <code>BufferedImage.TYPE_INT_RGB</code> will unmanage the
 * image.</p>
 *
 * @param img    the source image
 * @param x      the x location at which to start grabbing pixels
 * @param y      the y location at which to start grabbing pixels
 * @param w      the width of the rectangle of pixels to grab
 * @param h      the height of the rectangle of pixels to grab
 * @param pixels a pre-allocated array of pixels of size w*h; can be null
 * @return <code>pixels</code> if non-null, a new array of integers otherwise
 * @throws IllegalArgumentException is <code>pixels</code> is non-null and of length &lt; w*h
 */
static byte[] getPixels(BufferedImage img,
                               int x, int y, int w, int h, byte[] pixels) {
    if (w == 0 || h == 0) {
        return new byte[0];
    }

    if (pixels == null) {
        pixels = new byte[w * h];
    } else if (pixels.length < w * h) {
        throw new IllegalArgumentException("pixels array must have a length >= w*h");
    }

    int imageType = img.getType();
    if (imageType == BufferedImage.TYPE_BYTE_GRAY) {
        Raster raster = img.getRaster();
        return (byte[]) raster.getDataElements(x, y, w, h, pixels);
    } else {
        throw new IllegalArgumentException("Only type BYTE_GRAY is supported");
    }
}
 
Example 11
Source File: TestWarp.java    From libreveris with GNU Lesser General Public License v3.0 6 votes vote down vote up
private BufferedImage buildPattern (int cols,
                                    int rows,
                                    int dx,
                                    int dy)
{
    int           width = cols * dx;
    int           height = rows * dy;
    BufferedImage img = new BufferedImage(
        width,
        height,
        BufferedImage.TYPE_BYTE_GRAY);
    Graphics2D    g = (Graphics2D) img.getGraphics();
    g.setColor(Color.BLACK);

    for (int ir = 0; ir < rows; ir++) {
        int y = ir * dy;

        for (int ic = 0; ic < cols; ic++) {
            int x = ic * dx;
            g.setColor((((ir + ic) % 2) == 0) ? Color.GRAY : Color.WHITE);
            g.fillRect(x, y, dx, dy);
        }
    }

    return img;
}
 
Example 12
Source File: TextView.java    From DroidUIBuilder with Apache License 2.0 5 votes vote down vote up
public TextView(String str)
{
	super(TAG_NAME);

	text = new StringProperty("Text", "android:text", "");
	if (str != null)
	{
		text.setStringValue(str);
	}
	hint = new StringProperty("Default Text", "android:hint", "");
	fontSz = new StringProperty("Font Size", "android:textSize", fontSize
			+ "sp");
	face = new SelectProperty("Font Face", "android:typeface",
			new String[] { "normal", "sans", "serif", "monospace" }, 0);
	style = new SelectProperty("Font Style", "android:textStyle",
			new String[] { "normal", "bold", "italic", "bold|italic" }, 0);
	textColor = new ColorProperty("Text Color", "android:textColor", null);
	align = new SelectProperty("Text Alignment", "android:gravity",
			new String[] { "left", "center", "right" }, 2);
	props.add(text);
	props.add(hint);
	props.add(fontSz);
	props.add(face);
	props.add(style);
	props.add(textColor);
	props.add(align);

	osx = (System.getProperty("os.name").toLowerCase().contains("mac os x"));
	buildFont();

	_tempUsedForMessureStringWidth = new BufferedImage(1, 1, BufferedImage.TYPE_BYTE_GRAY);
	bgInTools = WidgetDefaultNPIconFactory.getInstance().getTextView_normal();
	
	apply();
}
 
Example 13
Source File: Destinations.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
public static void init() {
    destroot = new Group.EnableSet(TestEnvironment.globaloptroot,
                                   "dest", "Output Destination Options");

    new Screen();
    new OffScreen();

    if (GraphicsTests.hasGraphics2D) {
        if (ImageTests.hasCompatImage) {
            compatimgdestroot =
                new Group.EnableSet(destroot, "compatimg",
                                    "Compatible Image Destinations");
            compatimgdestroot.setHorizontal();

            new CompatImg();
            new CompatImg(Transparency.OPAQUE);
            new CompatImg(Transparency.BITMASK);
            new CompatImg(Transparency.TRANSLUCENT);
        }

        if (ImageTests.hasVolatileImage) {
            new VolatileImg();
        }

        bufimgdestroot = new Group.EnableSet(destroot, "bufimg",
                                             "BufferedImage Destinations");

        new BufImg(BufferedImage.TYPE_INT_RGB);
        new BufImg(BufferedImage.TYPE_INT_ARGB);
        new BufImg(BufferedImage.TYPE_INT_ARGB_PRE);
        new BufImg(BufferedImage.TYPE_3BYTE_BGR);
        new BufImg(BufferedImage.TYPE_BYTE_INDEXED);
        new BufImg(BufferedImage.TYPE_BYTE_GRAY);
        new CustomImg();
    }
}
 
Example 14
Source File: CvHelper.java    From opentest with MIT License 5 votes vote down vote up
public static BufferedImage convertToBufferedImage(Mat mat) {
    byte[] data = new byte[mat.cols() * mat.rows() * (int) mat.elemSize()];
    mat.get(0, 0, data);

    BufferedImage image = new BufferedImage(
            mat.cols(),
            mat.rows(),
            mat.channels() == 1
            ? BufferedImage.TYPE_BYTE_GRAY
            : BufferedImage.TYPE_3BYTE_BGR);
    image.getRaster().setDataElements(0, 0, mat.cols(), mat.rows(), data);
    return image;
}
 
Example 15
Source File: UserFaceDetector.java    From server_face_recognition with GNU General Public License v3.0 5 votes vote down vote up
private Mat bufferedImageToMat(BufferedImage bi) {
    Mat mat;

    if (bi.getType() == BufferedImage.TYPE_BYTE_GRAY) {
        mat = new Mat(bi.getHeight(), bi.getWidth(), CV_8UC1);
    } else {
        mat = new Mat(bi.getHeight(), bi.getWidth(), CV_8UC3);
    }

    byte[] data = ((DataBufferByte) bi.getRaster().getDataBuffer()).getData();
    mat.data().put(data);

    return mat;
}
 
Example 16
Source File: FSLoader.java    From TrakEM2 with GNU General Public License v3.0 5 votes vote down vote up
boolean save(final BufferedImage bi, final String path, final float quality, final boolean as_grey) {
	switch (bi.getType()) {
		case BufferedImage.TYPE_BYTE_GRAY:
			return save(new ByteProcessor(bi), path, quality, false);
		default:
			if (as_grey) return save(new ByteProcessor(bi), path, quality, false);
			return save(new ColorProcessor(bi), path, quality, false);
	}
}
 
Example 17
Source File: MomentsExtractorTest.java    From audiveris with GNU Affero General Public License v3.0 5 votes vote down vote up
private void reconstruct (Shape shape,
                          MomentsExtractor<D> extractor)
{
    int size = 200;
    BufferedImage img = new BufferedImage(size, size, BufferedImage.TYPE_BYTE_GRAY);
    WritableRaster raster = img.getRaster();

    extractor.reconstruct(raster);

    try {
        ImageIO.write(img, "png", new File(temp, shape + ".png"));
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
 
Example 18
Source File: ColConvTest.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
static String getImageTypeName(int type) {
    switch(type) {
        case BufferedImage.TYPE_INT_ARGB:
            return "TYPE_INT_ARGB";
        case BufferedImage.TYPE_INT_RGB:
            return "TYPE_INT_RGB";
        case BufferedImage.TYPE_INT_BGR:
            return "TYPE_INT_BGR";
        case BufferedImage.TYPE_INT_ARGB_PRE:
            return "TYPE_INT_ARGB_PRE";
        case BufferedImage.TYPE_3BYTE_BGR:
            return "TYPE_3BYTE_BGR";
        case BufferedImage.TYPE_4BYTE_ABGR:
            return "TYPE_4BYTE_ABGR";
        case BufferedImage.TYPE_4BYTE_ABGR_PRE:
            return "TYPE_4BYTE_ABGR_PRE";
        case BufferedImage.TYPE_BYTE_BINARY:
            return "TYPE_BYTE_BINARY";
        case BufferedImage.TYPE_BYTE_GRAY:
            return "TYPE_BYTE_GRAY";
        case BufferedImage.TYPE_BYTE_INDEXED:
            return "TYPE_BYTE_INDEXED";
        case BufferedImage.TYPE_USHORT_555_RGB:
            return "TYPE_USHORT_555_RGB";
        case BufferedImage.TYPE_USHORT_565_RGB:
            return "TYPE_USHORT_565_RGB";
        case BufferedImage.TYPE_USHORT_GRAY:
            return "TYPE_USHORT_GRAY";
    }
    return "UNKNOWN";
}
 
Example 19
Source File: PixelTests.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void init() {
    pixelroot = new Group("pixel", "Pixel Access Benchmarks");

    pixeloptroot = new Group(pixelroot, "opts", "Pixel Access Options");
    doRenderTo = new Option.Toggle(pixeloptroot, "renderto",
                                   "Render to Image before test",
                                   Option.Toggle.Off);
    doRenderFrom = new Option.Toggle(pixeloptroot, "renderfrom",
                                     "Render from Image before test",
                                     Option.Toggle.Off);

    // BufferedImage Sources
    bufimgsrcroot = new Group.EnableSet(pixelroot, "src",
                                        "BufferedImage Sources");
    new BufImg(BufferedImage.TYPE_BYTE_BINARY, 1);
    new BufImg(BufferedImage.TYPE_BYTE_BINARY, 2);
    new BufImg(BufferedImage.TYPE_BYTE_BINARY, 4);
    new BufImg(BufferedImage.TYPE_BYTE_INDEXED);
    new BufImg(BufferedImage.TYPE_BYTE_GRAY);
    new BufImg(BufferedImage.TYPE_USHORT_555_RGB);
    new BufImg(BufferedImage.TYPE_USHORT_565_RGB);
    new BufImg(BufferedImage.TYPE_USHORT_GRAY);
    new BufImg(BufferedImage.TYPE_3BYTE_BGR);
    new BufImg(BufferedImage.TYPE_4BYTE_ABGR);
    new BufImg(BufferedImage.TYPE_INT_RGB);
    new BufImg(BufferedImage.TYPE_INT_BGR);
    new BufImg(BufferedImage.TYPE_INT_ARGB);

    // BufferedImage Tests
    bufimgtestroot = new Group(pixelroot, "bimgtests",
                               "BufferedImage Tests");
    new BufImgTest.GetRGB();
    new BufImgTest.SetRGB();

    // Raster Tests
    rastertestroot = new Group(pixelroot, "rastests",
                               "Raster Tests");
    new RasTest.GetDataElements();
    new RasTest.SetDataElements();
    new RasTest.GetPixel();
    new RasTest.SetPixel();

    // DataBuffer Tests
    dbtestroot = new Group(pixelroot, "dbtests",
                           "DataBuffer Tests");
    new DataBufTest.GetElem();
    new DataBufTest.SetElem();
}
 
Example 20
Source File: DossierManagementImpl.java    From opencps-v2 with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public Response getDossierBarcode(HttpServletRequest request, HttpHeaders header, Company company, Locale locale,
		User user, ServiceContext serviceContext, String id) {
	long groupId = GetterUtil.getLong(header.getHeaderString("groupId"));
	
	try {
		long dossierId = GetterUtil.getLong(id);
		
		Dossier dossier = DossierLocalServiceUtil.fetchDossier(dossierId);
		
		if (dossier == null) {
			dossier = DossierLocalServiceUtil.getByRef(groupId, id);
		}
		Code128 barcode = new Code128();
		barcode.setFontName("Monospaced");
		barcode.setFontSize(16);
		barcode.setModuleWidth(2);
		barcode.setBarHeight(50);
		barcode.setHumanReadableLocation(HumanReadableLocation.BOTTOM);
		barcode.setContent(dossier.getDossierNo());

		int width = barcode.getWidth();
		int height = barcode.getHeight();

		BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
		Graphics2D g2d = image.createGraphics();
		Java2DRenderer renderer = new Java2DRenderer(g2d, 1, Color.WHITE, Color.BLACK);
		renderer.render(barcode);
		File destDir = new File("barcode");
		if (!destDir.exists()) {
			destDir.mkdir();
		}
		File file = new File("barcode/" + dossier.getDossierId() + ".png");
		if (!file.exists()) {
			file.createNewFile();				
		}

		if (file.exists()) {
			ImageIO.write(image, "png", file);
//			String fileType = Files.probeContentType(file.toPath());
			ResponseBuilder responseBuilder = Response.ok((Object) file);

			responseBuilder.header("Content-Disposition",
					"attachment; filename=\"" + file.getName() + "\"");
			responseBuilder.header("Content-Type", "image/png");

			return responseBuilder.build();
		} else {
			return Response.status(HttpURLConnection.HTTP_NO_CONTENT).build();
		}

	} catch (Exception e) {
		return BusinessExceptionImpl.processException(e);
	}
}