Search in sources :

Example 36 with ImageData

use of org.eclipse.swt.graphics.ImageData in project eclipse.platform.swt by eclipse.

the class ImageAnalyzer method menuReopen.

void menuReopen() {
    if (currentName == null)
        return;
    // stop any animation in progress
    animate = false;
    Cursor waitCursor = display.getSystemCursor(SWT.CURSOR_WAIT);
    shell.setCursor(waitCursor);
    imageCanvas.setCursor(waitCursor);
    try {
        loader = new ImageLoader();
        ImageData[] newImageData;
        if (fileName == null) {
            URL url = new URL(currentName);
            try (InputStream stream = url.openStream()) {
                long startTime = System.currentTimeMillis();
                newImageData = loader.load(stream);
                loadTime = System.currentTimeMillis() - startTime;
            }
        } else {
            long startTime = System.currentTimeMillis();
            newImageData = loader.load(fileName);
            loadTime = System.currentTimeMillis() - startTime;
        }
        imageDataIndex = 0;
        displayImage(newImageData[imageDataIndex]);
    } catch (Exception e) {
        showErrorDialog(bundle.getString("Reloading_lc"), currentName, e);
    } catch (OutOfMemoryError e) {
        showErrorDialog(bundle.getString("Reloading_lc"), currentName, e);
    } finally {
        shell.setCursor(null);
        imageCanvas.setCursor(crossCursor);
    }
}
Also used : ImageData(org.eclipse.swt.graphics.ImageData) InputStream(java.io.InputStream) Cursor(org.eclipse.swt.graphics.Cursor) ImageLoader(org.eclipse.swt.graphics.ImageLoader) URL(java.net.URL) SWTException(org.eclipse.swt.SWTException)

Example 37 with ImageData

use of org.eclipse.swt.graphics.ImageData in project eclipse.platform.swt by eclipse.

the class ImageAnalyzer method menuSaveAs.

void menuSaveAs() {
    if (image == null)
        return;
    // stop any animation in progress
    animate = false;
    // Get the user to choose a file name and type to save.
    FileDialog fileChooser = new FileDialog(shell, SWT.SAVE);
    fileChooser.setFilterPath(lastPath);
    if (fileName != null) {
        String name = fileName;
        int nameStart = name.lastIndexOf(java.io.File.separatorChar);
        if (nameStart > -1) {
            name = name.substring(nameStart + 1);
        }
        fileChooser.setFileName(name.substring(0, name.indexOf(".")) + "." + imageTypeCombo.getText().toLowerCase());
    }
    fileChooser.setFilterExtensions(SAVE_FILTER_EXTENSIONS);
    fileChooser.setFilterNames(SAVE_FILTER_NAMES);
    switch(imageTypeCombo.getSelectionIndex()) {
        case 0:
            fileChooser.setFilterIndex(4);
            break;
        case 1:
            fileChooser.setFilterIndex(5);
            break;
        case 2:
            fileChooser.setFilterIndex(2);
            break;
        case 3:
            fileChooser.setFilterIndex(3);
            break;
        case 4:
            fileChooser.setFilterIndex(6);
            break;
        case 5:
            fileChooser.setFilterIndex(0);
            break;
    }
    String filename = fileChooser.open();
    lastPath = fileChooser.getFilterPath();
    if (filename == null)
        return;
    // Figure out what file type the user wants saved.
    int filetype = fileChooser.getFilterIndex();
    if (filetype == -1) {
        /* The platform file dialog does not support user-selectable file filters.
			 * Determine the desired type by looking at the file extension.
			 */
        filetype = determineFileType(filename);
        if (filetype == SWT.IMAGE_UNDEFINED) {
            MessageBox box = new MessageBox(shell, SWT.ICON_ERROR);
            box.setMessage(createMsg(bundle.getString("Unknown_extension"), filename.substring(filename.lastIndexOf('.') + 1)));
            box.open();
            return;
        }
    }
    if (new java.io.File(filename).exists()) {
        MessageBox box = new MessageBox(shell, SWT.ICON_QUESTION | SWT.OK | SWT.CANCEL);
        box.setMessage(createMsg(bundle.getString("Overwrite"), filename));
        if (box.open() == SWT.CANCEL)
            return;
    }
    Cursor waitCursor = display.getSystemCursor(SWT.CURSOR_WAIT);
    shell.setCursor(waitCursor);
    imageCanvas.setCursor(waitCursor);
    try {
        // Save the current image to the specified file.
        boolean multi = false;
        if (loader.data.length > 1) {
            MessageBox box = new MessageBox(shell, SWT.ICON_QUESTION | SWT.YES | SWT.NO | SWT.CANCEL);
            box.setMessage(createMsg(bundle.getString("Save_all"), Integer.valueOf(loader.data.length)));
            int result = box.open();
            if (result == SWT.CANCEL)
                return;
            if (result == SWT.YES)
                multi = true;
        }
        /* If the image has transparency but the user has transparency turned off,
			 * turn it off in the saved image. */
        int transparentPixel = imageData.transparentPixel;
        if (!multi && transparentPixel != -1 && !transparent) {
            imageData.transparentPixel = -1;
        }
        if (!multi)
            loader.data = new ImageData[] { imageData };
        loader.compression = compressionCombo.indexOf(compressionCombo.getText());
        loader.save(filename, filetype);
        /* Restore the previous transparency setting. */
        if (!multi && transparentPixel != -1 && !transparent) {
            imageData.transparentPixel = transparentPixel;
        }
        // Update the shell title and file type label,
        // and use the new file.
        fileName = filename;
        shell.setText(createMsg(bundle.getString("Analyzer_on"), filename));
        typeLabel.setText(createMsg(bundle.getString("Type_string"), fileTypeString(filetype)));
    } catch (SWTException e) {
        showErrorDialog(bundle.getString("Saving_lc"), filename, e);
    } catch (SWTError e) {
        showErrorDialog(bundle.getString("Saving_lc"), filename, e);
    } finally {
        shell.setCursor(null);
        imageCanvas.setCursor(crossCursor);
    }
}
Also used : SWTError(org.eclipse.swt.SWTError) SWTException(org.eclipse.swt.SWTException) ImageData(org.eclipse.swt.graphics.ImageData) Cursor(org.eclipse.swt.graphics.Cursor) FileDialog(org.eclipse.swt.widgets.FileDialog) Point(org.eclipse.swt.graphics.Point) MessageBox(org.eclipse.swt.widgets.MessageBox)

Example 38 with ImageData

use of org.eclipse.swt.graphics.ImageData in project eclipse.platform.swt by eclipse.

the class IconCache method getIconFromProgram.

/**
 * Gets an image for a file associated with a given program
 *
 * @param program the Program
 */
public Image getIconFromProgram(Program program) {
    Image image = iconCache.get(program);
    if (image == null) {
        ImageData imageData = program.getImageData();
        if (imageData != null) {
            image = new Image(null, imageData, imageData.getTransparencyMask());
            iconCache.put(program, image);
        } else {
            image = stockImages[iconFile];
        }
    }
    return image;
}
Also used : ImageData(org.eclipse.swt.graphics.ImageData) Image(org.eclipse.swt.graphics.Image)

Example 39 with ImageData

use of org.eclipse.swt.graphics.ImageData in project eclipse.platform.swt by eclipse.

the class Test_org_eclipse_swt_graphics_GC method test_copyAreaLorg_eclipse_swt_graphics_ImageII.

@Test
public void test_copyAreaLorg_eclipse_swt_graphics_ImageII() {
    Color white = display.getSystemColor(SWT.COLOR_WHITE);
    Color blue = display.getSystemColor(SWT.COLOR_BLUE);
    RGB whiteRGB = getRealRGB(white);
    RGB blueRGB = getRealRGB(blue);
    gc.setBackground(white);
    gc.fillRectangle(image.getBounds());
    gc.setBackground(blue);
    gc.fillRectangle(5, 0, 6, 1);
    Image image = new Image(display, 12, 12);
    gc.copyArea(image, 0, 0);
    ImageData imageData = image.getImageData();
    PaletteData palette = imageData.palette;
    if (DPIUtil.getDeviceZoom() != 100) {
        // TODO Fix non integer scaling factors.
        if (SwtTestUtil.verbose) {
            System.out.println("Excluded test_copyAreaLorg_eclipse_swt_graphics_ImageII(org.eclipse.swt.tests.junit.Test_org_eclipse_swt_graphics_GC)");
        }
        image.dispose();
        return;
    }
    int pixel = imageData.getPixel(4, 0);
    assertEquals(":a:", whiteRGB, palette.getRGB(pixel));
    pixel = imageData.getPixel(5, 0);
    assertEquals(":b:", blueRGB, palette.getRGB(pixel));
    pixel = imageData.getPixel(10, 0);
    assertEquals(":c:", blueRGB, palette.getRGB(pixel));
    pixel = imageData.getPixel(11, 0);
    assertEquals(":d:", whiteRGB, palette.getRGB(pixel));
    image.dispose();
}
Also used : PaletteData(org.eclipse.swt.graphics.PaletteData) ImageData(org.eclipse.swt.graphics.ImageData) Color(org.eclipse.swt.graphics.Color) RGB(org.eclipse.swt.graphics.RGB) Image(org.eclipse.swt.graphics.Image) Point(org.eclipse.swt.graphics.Point) Test(org.junit.Test)

Example 40 with ImageData

use of org.eclipse.swt.graphics.ImageData in project eclipse.platform.swt by eclipse.

the class Test_org_eclipse_swt_graphics_GC method test_drawImageLorg_eclipse_swt_graphics_ImageIIIIIIII.

@Test
public void test_drawImageLorg_eclipse_swt_graphics_ImageIIIIIIII() {
    Color c1 = new Color(display, 255, 0, 0);
    Color c2 = new Color(display, 0, 0, 0);
    Color c3 = new Color(display, 255, 255, 0);
    PaletteData paletteData = new PaletteData(c1.getRGB(), c2.getRGB(), c3.getRGB());
    ImageData data = new ImageData(30, 30, 8, paletteData);
    for (int y = 0; y < data.height; y++) {
        for (int x = 0; x < data.width; x++) {
            if (x > y)
                data.setPixel(x, y, paletteData.getPixel(c1.getRGB()));
            else if (x < y)
                data.setPixel(x, y, paletteData.getPixel(c2.getRGB()));
            else
                data.setPixel(x, y, paletteData.getPixel(c3.getRGB()));
        }
    }
    Image image = new Image(display, data);
    data = image.getImageData();
    data.transparentPixel = paletteData.getPixel(c1.getRGB());
    Image imageTransparent = new Image(display, data);
    data.transparentPixel = -1;
    for (int y = 0; y < data.height; y++) {
        for (int x = 0; x < data.width; x++) {
            data.setAlpha(x, y, 127);
        }
    }
    Image imageAlpha = new Image(display, data);
    gc.drawImage(image, 10, 5, 20, 15, 100, 120, 50, 60);
    gc.drawImage(imageTransparent, 10, 5, 20, 15, 100, 120, 10, 10);
    gc.drawImage(imageAlpha, 10, 5, 20, 15, 100, 120, 20, 15);
    try {
        gc.drawImage(null, 10, 5, 20, 15, 100, 120, 50, 60);
        // should never get here
        fail("No exception thrown");
    } catch (IllegalArgumentException e) {
    }
    image.dispose();
    imageAlpha.dispose();
    imageTransparent.dispose();
    c1.dispose();
    c2.dispose();
    c3.dispose();
}
Also used : PaletteData(org.eclipse.swt.graphics.PaletteData) ImageData(org.eclipse.swt.graphics.ImageData) Color(org.eclipse.swt.graphics.Color) Image(org.eclipse.swt.graphics.Image) Point(org.eclipse.swt.graphics.Point) Test(org.junit.Test)

Aggregations

ImageData (org.eclipse.swt.graphics.ImageData)132 Image (org.eclipse.swt.graphics.Image)78 PaletteData (org.eclipse.swt.graphics.PaletteData)38 RGB (org.eclipse.swt.graphics.RGB)33 Point (org.eclipse.swt.graphics.Point)32 Test (org.junit.Test)26 InputStream (java.io.InputStream)24 GC (org.eclipse.swt.graphics.GC)19 IOException (java.io.IOException)18 SWTException (org.eclipse.swt.SWTException)14 ImageDescriptor (org.eclipse.jface.resource.ImageDescriptor)13 Rectangle (org.eclipse.swt.graphics.Rectangle)12 Color (org.eclipse.swt.graphics.Color)11 ImageLoader (org.eclipse.swt.graphics.ImageLoader)11 Display (org.eclipse.swt.widgets.Display)11 ByteArrayInputStream (java.io.ByteArrayInputStream)7 Cursor (org.eclipse.swt.graphics.Cursor)7 Composite (org.eclipse.swt.widgets.Composite)7 GradientPaint (java.awt.GradientPaint)5 URL (java.net.URL)5