Last active
January 28, 2018 14:42
-
-
Save ka-ka-xyz/232bc0368ebf3c44bb46a7e1cb03810b to your computer and use it in GitHub Desktop.
aparapi sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package example; | |
import java.awt.image.BufferedImage; | |
import java.io.File; | |
import java.io.IOException; | |
import javax.imageio.ImageIO; | |
import com.aparapi.Kernel; | |
import com.aparapi.Range; | |
/** | |
* Required System Properties | |
* | |
*-Dexample.useGPU: true of false. | |
*-Dexample.inputFilePath: input file path str. | |
*-Dexample.outputFilePath: output file path str. | |
*-Djava.library.path: path to directory of aparapi_x86_64.dll | |
*-Dcom.aparapi.enableShowGeneratedOpenCL: when you want detailed info, true. | |
*-Dcom.aparapi.enableExecutionModeReporting: when you want detailed info, true. | |
* */ | |
public class Main { | |
private int width; | |
private int height; | |
public static void main(String[] args) throws IOException { | |
boolean useGPU = Boolean.getBoolean("example.useGPU"); | |
String inputFilePath = System.getProperty("example.inputFilePath"); | |
String outputFilePath = System.getProperty("example.outputFilePath"); | |
Main main = new Main(); | |
int[] pixels = main.loadFile(new File(inputFilePath)); | |
long start = System.currentTimeMillis(); | |
if (useGPU) { | |
main.convertToGrayScaleInGpu(pixels); | |
} else { | |
main.convertToGrayScaleInJvm(pixels); | |
} | |
System.out.println("Convert Finished:" + (System.currentTimeMillis() - start) + "ms"); | |
File outFile = new File(outputFilePath); | |
main.saveFile(outFile, pixels); | |
} | |
private void convertToGrayScaleInJvm(final int[] pixels) { | |
for (int i = 0; i < pixels.length; i++) { | |
int pixel = pixels[i]; | |
int alpha = pixel >> 24 & 0xFF; | |
int red = pixel >> 16 & 0xFF; | |
int green = pixel >> 8 & 0xFF; | |
int blue = pixel & 0xFF; | |
int y = (int)(0.298912 * red + 0.586611 * green + 0.114478 * blue); | |
pixels[i] = alpha << 24 | y << 16 | y << 8 | y; | |
} | |
} | |
private void convertToGrayScaleInGpu(final int[] pixels) { | |
Kernel kernel = new Kernel() { | |
@Override | |
public void run() { | |
//GPU world | |
int i = getGlobalId(); | |
int pixel = pixels[i]; | |
int alpha = pixel >> 24 & 0xFF; | |
int red = pixel >> 16 & 0xFF; | |
int green = pixel >> 8 & 0xFF; | |
int blue = pixel & 0xFF; | |
int y = (int)(0.298912 * red + 0.586611 * green + 0.114478 * blue); | |
pixels[i] = alpha << 24 | y << 16 | y << 8 | y; | |
}}; | |
int size = pixels.length; | |
kernel.execute(Range.create(size)); | |
} | |
private int[] loadFile(File file) throws IOException { | |
BufferedImage img = ImageIO.read(file); | |
this.width = img.getWidth(); | |
this.height = img.getHeight(); | |
int[] pixels = new int[width*height]; | |
img.getRGB(0, 0, width, height, pixels, 0, width); | |
return pixels; | |
} | |
private void saveFile(File file, int[] px) throws IOException { | |
BufferedImage out = new BufferedImage(this.width, this.height, BufferedImage.TYPE_INT_RGB); | |
out.setRGB(0, 0, this.width, this.height, px, 0, this.width); | |
ImageIO.write(out, "jpg", file); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>ka_ka_xyz</groupId> | |
<artifactId>example.gpu</artifactId> | |
<version>0.0.1-SNAPSHOT</version> | |
<name>gpu_example</name> | |
<dependencies> | |
<dependency> | |
<groupId>com.aparapi</groupId> | |
<artifactId>aparapi</artifactId> | |
<version>1.4.1</version> | |
</dependency> | |
</dependencies> | |
</project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment