Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Automatically restrict shader colours (#207)
Adapted generate-and-run so that it no longer focuses on reference+variant, but just processes a stream of variants. Furthermore, changed generation so that fragment shader colours are restricted to a small palette, so that a test oracle can automatically look for deviations from this palette.
- Loading branch information
1 parent
9081181
commit 0b902b2
Showing
9 changed files
with
632 additions
and
127 deletions.
There are no files selected for viewing
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
107 changes: 107 additions & 0 deletions
107
common/src/main/java/com/graphicsfuzz/common/util/ImageColorComponents.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
* Copyright 2019 The GraphicsFuzz Project Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.graphicsfuzz.common.util; | ||
|
||
import java.awt.image.BufferedImage; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class ImageColorComponents { | ||
|
||
private static final int R_OFFSET = 16; | ||
private static final int G_OFFSET = 8; | ||
private static final int B_OFFSET = 0; | ||
private static final int A_OFFSET = 24; | ||
|
||
/** | ||
* Determines whether the given buffered image comprises pixels whose components come only from | ||
* the given component values. | ||
* @param image The image to be considered. | ||
* @param allowedComponentValues The allowed component values. | ||
* @return True if and only if the pixels in the image only use the given components. | ||
*/ | ||
public static boolean containsOnlyGivenComponentValues(BufferedImage image, | ||
List<Integer> allowedComponentValues) { | ||
final int[] colors = getRgb(image); | ||
for (int color : colors) { | ||
for (int componentValue : Arrays.asList(getComponentR(color), getComponentG(color), | ||
getComponentB(color), getComponentA(color))) { | ||
if (!allowedComponentValues.contains(componentValue)) { | ||
return false; | ||
} | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
* Gets the R component from a pixel. | ||
* @param pixel A pixel. | ||
* @return The pixel's R component. | ||
*/ | ||
public static int getComponentR(int pixel) { | ||
return getComponent(pixel, R_OFFSET); | ||
} | ||
|
||
/** | ||
* Gets the G component from a pixel. | ||
* @param pixel A pixel. | ||
* @return The pixel's G component. | ||
*/ | ||
public static int getComponentG(int pixel) { | ||
return getComponent(pixel, G_OFFSET); | ||
} | ||
|
||
/** | ||
* Gets the B component from a pixel. | ||
* @param pixel A pixel. | ||
* @return The pixel's B component. | ||
*/ | ||
public static int getComponentB(int pixel) { | ||
return getComponent(pixel, B_OFFSET); | ||
} | ||
|
||
/** | ||
* Gets the A component from a pixel. | ||
* @param pixel A pixel. | ||
* @return The pixel's A component. | ||
*/ | ||
public static int getComponentA(int pixel) { | ||
return getComponent(pixel, A_OFFSET); | ||
} | ||
|
||
/** | ||
* Returns an array of pixel values for the given image. | ||
* @param image Image for which pixel values are required. | ||
* @return Pixel values for the image. | ||
*/ | ||
public static int[] getRgb(BufferedImage image) { | ||
return image.getRGB( | ||
0, | ||
0, | ||
image.getWidth(), | ||
image.getHeight(), | ||
null, | ||
0, | ||
image.getWidth()); | ||
} | ||
|
||
private static int getComponent(int pixel, int componentBitOffset) { | ||
return (pixel >> componentBitOffset) & 0xff; | ||
} | ||
|
||
} |
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
62 changes: 62 additions & 0 deletions
62
common/src/main/java/com/graphicsfuzz/imagetools/CheckColorComponents.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright 2019 The GraphicsFuzz Project Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.graphicsfuzz.imagetools; | ||
|
||
import com.graphicsfuzz.common.util.ImageColorComponents; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.List; | ||
import javax.imageio.ImageIO; | ||
import net.sourceforge.argparse4j.ArgumentParsers; | ||
import net.sourceforge.argparse4j.inf.ArgumentParser; | ||
import net.sourceforge.argparse4j.inf.ArgumentParserException; | ||
import net.sourceforge.argparse4j.inf.Namespace; | ||
|
||
public class CheckColorComponents { | ||
|
||
public static void main(String[] args) throws ArgumentParserException, IOException { | ||
|
||
final ArgumentParser parser = ArgumentParsers.newArgumentParser("CheckColorComponents") | ||
.defaultHelp(true) | ||
.description("Exits with code 0 if and only if the given image uses only the given color " | ||
+ "components as the RGBA values of its pixels."); | ||
|
||
// Required arguments | ||
parser.addArgument("image") | ||
.help("Path to PNG image") | ||
.type(File.class); | ||
parser.addArgument("components") | ||
.type(Integer.class) | ||
.nargs("+") | ||
.help("Allowed components, each in range 0..255."); | ||
|
||
final Namespace ns = parser.parseArgs(args); | ||
|
||
final File image = ns.get("image"); | ||
final List<Integer> components = ns.get("components"); | ||
|
||
if (components.stream().anyMatch(item -> item < 0 || item > 255)) { | ||
System.err.println("Error: given component list " + components + " includes elements not " | ||
+ "in range 0..255."); | ||
} | ||
|
||
if (!ImageColorComponents.containsOnlyGivenComponentValues(ImageIO.read(image), components)) { | ||
System.exit(1); | ||
} | ||
} | ||
|
||
} |
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
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
Oops, something went wrong.