Skip to content

Commit b724b57

Browse files
committed
Introduce simple API to save ImageData to a file and support .jpg
1 parent d1859ce commit b724b57

File tree

3 files changed

+74
-21
lines changed

3 files changed

+74
-21
lines changed

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/ImageData.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515

1616

1717
import java.io.*;
18+
import java.nio.file.Path;
1819

1920
import org.eclipse.swt.*;
2021
import org.eclipse.swt.internal.*;
22+
import org.eclipse.swt.internal.image.*;
2123

2224
/**
2325
* Instances of this class are device-independent descriptions
@@ -1599,6 +1601,48 @@ public void setPixels(int x, int y, int putWidth, int[] pixels, int startIndex)
15991601
SWT.error(SWT.ERROR_UNSUPPORTED_DEPTH);
16001602
}
16011603

1604+
/**
1605+
* Saves this image to the specified file.
1606+
* The image format is derived from the file's extension and the following formats are supported:
1607+
* <dl>
1608+
* <dt>{@link SWT#IMAGE_BMP .bmp}</dt>
1609+
* <dd>Windows BMP file format, no compression</dd>
1610+
* <dt>{@link SWT#IMAGE_GIF .gif}</dt>
1611+
* <dd>GIF file format</dd>
1612+
* <dt>{@link SWT#IMAGE_ICO .ico}</dt>
1613+
* <dd>Windows ICO file format</dd>
1614+
* <dt>{@link SWT#IMAGE_JPEG .jpg or .jpeg}</dt>
1615+
* <dd>JPEG file format</dd>
1616+
* <dt>{@link SWT#IMAGE_PNG .png}</dt>
1617+
* <dd>PNG file format</dd>
1618+
* <dt>{@link SWT#IMAGE_TIFF .tiff}</dt>
1619+
* <dd>TIFF file format</dd>
1620+
* </dl>
1621+
*
1622+
* @param file the path to the file where this image is to be saved
1623+
*
1624+
* @exception IllegalArgumentException <ul>
1625+
* <li>ERROR_NULL_ARGUMENT - if the file name is null</li>
1626+
* </ul>
1627+
* @exception SWTException <ul>
1628+
* <li>ERROR_IO - if an IO error occurs while writing to the file</li>
1629+
* <li>ERROR_INVALID_IMAGE - if this image data contains invalid data</li>
1630+
* <li>ERROR_UNSUPPORTED_FORMAT - if the image data cannot be saved to the requested format</li>
1631+
* </ul>
1632+
* @since 3.130
1633+
*/
1634+
public void save(Path file) {
1635+
if (file == null) {
1636+
SWT.error(SWT.ERROR_NULL_ARGUMENT);
1637+
}
1638+
String filename = file.getFileName().toString();
1639+
String extension = filename.substring(filename.lastIndexOf('.') + 1);
1640+
int imageFormat = FileFormat.getImageFormatFromFileExtension(extension);
1641+
ImageLoader loader = new ImageLoader();
1642+
loader.data = new ImageData[] { this };
1643+
loader.save(file.toString(), imageFormat);
1644+
}
1645+
16021646
/**
16031647
* Returns a palette with 2 colors: black & white.
16041648
*/

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/image/FileFormat.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,34 @@ public abstract class FileFormat {
5858
} catch (NoClassDefFoundError e) { } // ignore format
5959
}
6060

61+
public static int getImageFormatFromFileExtension(String nameStr) {
62+
return switch (nameStr) {
63+
case "bmp" -> SWT.IMAGE_BMP;
64+
case "gif" -> SWT.IMAGE_GIF;
65+
case "ico" -> SWT.IMAGE_ICO;
66+
case "jpg", "jpeg" -> SWT.IMAGE_JPEG;
67+
case "png" -> SWT.IMAGE_PNG;
68+
case "tiff" -> SWT.IMAGE_TIFF;
69+
case "svg" -> SWT.IMAGE_SVG;
70+
default -> SWT.IMAGE_UNDEFINED;
71+
};
72+
}
73+
74+
public static String getImageFileExtensionFromFormat(int format) {
75+
String typeStr = switch (format) {
76+
case SWT.IMAGE_BMP_RLE -> "bmp";
77+
case SWT.IMAGE_BMP -> "bmp";
78+
case SWT.IMAGE_GIF -> "gif";
79+
case SWT.IMAGE_ICO -> "ico";
80+
case SWT.IMAGE_JPEG -> "jpeg";
81+
case SWT.IMAGE_PNG -> "png";
82+
case SWT.IMAGE_TIFF -> "tiff";
83+
case SWT.IMAGE_SVG -> "svg";
84+
default -> "";
85+
};
86+
return typeStr;
87+
}
88+
6189
public static final int DEFAULT_ZOOM = 100;
6290

6391
private static Optional<FileFormat> determineFileFormat(LEDataInputStream stream) {

bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/internal/NativeImageLoader.java

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -180,16 +180,7 @@ private static int getImageFormat(long loader) {
180180
long name = GDK.gdk_pixbuf_format_get_name(format);
181181
String nameStr = Converter.cCharPtrToJavaString(name, false);
182182
OS.g_free(name);
183-
return switch (nameStr) {
184-
case "bmp" -> SWT.IMAGE_BMP;
185-
case "gif" -> SWT.IMAGE_GIF;
186-
case "ico" -> SWT.IMAGE_ICO;
187-
case "jpeg" -> SWT.IMAGE_JPEG;
188-
case "png" -> SWT.IMAGE_PNG;
189-
case "tiff" -> SWT.IMAGE_TIFF;
190-
case "svg" -> SWT.IMAGE_SVG;
191-
default -> SWT.IMAGE_UNDEFINED;
192-
};
183+
return FileFormat.getImageFormatFromFileExtension(nameStr);
193184
}
194185

195186
/**
@@ -351,17 +342,7 @@ public static void save(OutputStream stream, int format, ImageLoader imageLoader
351342
}
352343

353344
// Write pixbuf to byte array and then to OutputStream
354-
String typeStr = switch (format) {
355-
case SWT.IMAGE_BMP_RLE -> "bmp";
356-
case SWT.IMAGE_BMP -> "bmp";
357-
case SWT.IMAGE_GIF -> "gif";
358-
case SWT.IMAGE_ICO -> "ico";
359-
case SWT.IMAGE_JPEG -> "jpeg";
360-
case SWT.IMAGE_PNG -> "png";
361-
case SWT.IMAGE_TIFF -> "tiff";
362-
case SWT.IMAGE_SVG -> "svg";
363-
default -> "";
364-
};
345+
String typeStr = FileFormat.getImageFileExtensionFromFormat(format);
365346
byte[] type = Converter.wcsToMbcs(typeStr, true);
366347

367348
long[] buffer = new long[1];

0 commit comments

Comments
 (0)