| 1 | // Jomic - a viewer for comic book archives. |
| 2 | // Copyright (C) 2004-2008 Thomas Aglassinger |
| 3 | // |
| 4 | // This program is free software: you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU General Public License as published by |
| 6 | // the Free Software Foundation, either version 3 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // |
| 9 | // This program is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | // |
| 14 | // You should have received a copy of the GNU General Public License |
| 15 | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | package net.sf.jomic.tests; |
| 17 | |
| 18 | import java.awt.Color; |
| 19 | import java.awt.GradientPaint; |
| 20 | import java.awt.Graphics2D; |
| 21 | import java.awt.geom.Rectangle2D; |
| 22 | import java.awt.image.BufferedImage; |
| 23 | import java.awt.image.RenderedImage; |
| 24 | import java.io.File; |
| 25 | import java.io.FileInputStream; |
| 26 | import java.io.FileOutputStream; |
| 27 | import java.io.FileWriter; |
| 28 | import java.io.IOException; |
| 29 | import java.io.OutputStream; |
| 30 | import java.util.Enumeration; |
| 31 | import java.util.Iterator; |
| 32 | import java.util.LinkedList; |
| 33 | import java.util.List; |
| 34 | import java.util.Map; |
| 35 | import java.util.TreeMap; |
| 36 | import java.util.zip.ZipEntry; |
| 37 | import java.util.zip.ZipFile; |
| 38 | import java.util.zip.ZipOutputStream; |
| 39 | |
| 40 | import javax.imageio.ImageIO; |
| 41 | import javax.imageio.ImageWriteParam; |
| 42 | import javax.imageio.ImageWriter; |
| 43 | import javax.imageio.stream.ImageOutputStream; |
| 44 | |
| 45 | import net.sf.jomic.comic.Conversion; |
| 46 | import net.sf.jomic.comic.CreateComicTask; |
| 47 | import net.sf.jomic.jaiunit.Tools; |
| 48 | import net.sf.jomic.tools.FileTools; |
| 49 | import net.sf.jomic.tools.ImageTools; |
| 50 | import net.sf.jomic.tools.RegExFileFilter; |
| 51 | import net.sf.jomic.tools.StringTools; |
| 52 | import net.sf.jomic.tools.Task; |
| 53 | import net.sf.jomic.tools.TestTools; |
| 54 | import net.sf.wraplog.Logger; |
| 55 | |
| 56 | import org.pdfbox.exceptions.COSVisitorException; |
| 57 | import org.pdfbox.pdfwriter.COSWriter; |
| 58 | import org.pdfbox.pdmodel.PDDocument; |
| 59 | import org.pdfbox.pdmodel.PDPage; |
| 60 | |
| 61 | /** |
| 62 | * Tool to create test data, in particular images for comics. |
| 63 | * |
| 64 | * @author Thomas Aglassinger |
| 65 | */ |
| 66 | public class TestDataCreator |
| 67 | { |
| 68 | /** |
| 69 | * Prefix used for test files that are broken and cannot be opened. |
| 70 | */ |
| 71 | public static final String BROKEN_PREFIX = "broken_"; |
| 72 | |
| 73 | /** |
| 74 | * Prefix used for test comics that are broken and cannot be opened. |
| 75 | */ |
| 76 | public static final String BROKEN_ARCHIVE_PREFIX = BROKEN_PREFIX + "archive_"; |
| 77 | |
| 78 | /** |
| 79 | * Prefix used for test images that are broken and cannot be opened. |
| 80 | */ |
| 81 | public static final String BROKEN_IMAGE_PREFIX = BROKEN_PREFIX + "image_"; |
| 82 | |
| 83 | private static final int BUFFER_SIZE = 16384; |
| 84 | private static final double DIFFERENT_RATIO = 0.8; |
| 85 | |
| 86 | private static final int HEIGHT = 640; |
| 87 | |
| 88 | /** |
| 89 | * Default suffix used by generated test images. |
| 90 | */ |
| 91 | private static final String TEST_IMAGE_SUFFIX = "png"; |
| 92 | private static final int WIDTH = 480; |
| 93 | |
| 94 | private FileTools fileTools; |
| 95 | private ImageTools imageTools; |
| 96 | private Tools jaiUnitTools; |
| 97 | private Logger logger; |
| 98 | private String[] standardTestNames; |
| 99 | private StringTools stringTools; |
| 100 | private Map testComics; |
| 101 | private List testImageNames; |
| 102 | private TestTools testTools; |
| 103 | |
| 104 | TestDataCreator() { |
| 105 | logger = Logger.getLogger(TestDataCreator.class); |
| 106 | testTools = TestTools.instance(); |
| 107 | fileTools = FileTools.instance(); |
| 108 | stringTools = StringTools.instance(); |
| 109 | jaiUnitTools = Tools.instance(); |
| 110 | imageTools = ImageTools.instance(); |
| 111 | testImageNames = new LinkedList(); |
| 112 | testComics = new TreeMap(); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Create all test data. |
| 117 | */ |
| 118 | public static void main(String[] args) |
| 119 | throws Exception { |
| 120 | TestDataCreator creator = new TestDataCreator(); |
| 121 | |
| 122 | creator.create(); |
| 123 | } |
| 124 | |
| 125 | void create() |
| 126 | throws Exception { |
| 127 | createTestImages(); |
| 128 | createTestTexts(); |
| 129 | createTestComics(); |
| 130 | createBrokenTestComics(); |
| 131 | |
| 132 | Iterator comicRider = testComics.entrySet().iterator(); |
| 133 | |
| 134 | while (comicRider.hasNext()) { |
| 135 | Map.Entry entry = (Map.Entry) comicRider.next(); |
| 136 | File comicFile = (File) entry.getKey(); |
| 137 | String description = (String) entry.getValue(); |
| 138 | |
| 139 | // TODO: Write to HTML/XML file |
| 140 | logger.info("{}: {}", comicFile.getName(), description); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | private void addTestComic(File comicFile, String xhtmlDescription) { |
| 145 | assert comicFile != null; |
| 146 | assert xhtmlDescription != null; |
| 147 | testComics.put(comicFile, xhtmlDescription); |
| 148 | } |
| 149 | |
| 150 | private void assertCannotBeLoaded(File imageFile) { |
| 151 | assert imageFile != null; |
| 152 | try { |
| 153 | imageTools.readImage(imageFile); |
| 154 | // TODO: actually break the image: |
| 155 | // assert false |
| 156 | // : "image file " + stringTools.sourced(imageFile.getAbsolutePath() + " must be unparseable"); |
| 157 | } catch (Throwable expectedError) { |
| 158 | if (logger.isDebugEnabled()) { |
| 159 | logger.debug("ignoring expected error on unparseable image: " + expectedError); |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | private File copyTestFiles(String source, String target) |
| 165 | throws IOException { |
| 166 | assert source != null; |
| 167 | assert target != null; |
| 168 | File result = testTools.getTestGeneratedInputFile(target); |
| 169 | |
| 170 | fileTools.copyFile(testTools.getTestFile(source), result); |
| 171 | return result; |
| 172 | } |
| 173 | |
| 174 | private void createBrokenNoImagesPdf() |
| 175 | throws COSVisitorException, IOException { |
| 176 | File emptyPdf = testTools.getTestGeneratedInputFile(TestTools.BROKEN_NO_IMAGES_PDF); |
| 177 | PDDocument document = new PDDocument(); |
| 178 | |
| 179 | try { |
| 180 | //Every document requires at least one page, so we will add one |
| 181 | //blank page. |
| 182 | PDPage blankPage = new PDPage(); |
| 183 | |
| 184 | document.addPage(blankPage); |
| 185 | |
| 186 | FileOutputStream output = new FileOutputStream(emptyPdf); |
| 187 | COSWriter writer = new COSWriter(output); |
| 188 | |
| 189 | try { |
| 190 | writer.write(document.getDocument()); |
| 191 | } finally { |
| 192 | writer.close(); |
| 193 | } |
| 194 | } finally { |
| 195 | document.close(); |
| 196 | } |
| 197 | addTestComic(emptyPdf, "A (broken) PDF comic that does not contain any images, " |
| 198 | + "although it is a valid document."); |
| 199 | } |
| 200 | |
| 201 | private void createBrokenTestComics() |
| 202 | throws COSVisitorException, IOException { |
| 203 | File brokenImageIncompleteCbz = testTools.getTestGeneratedInputFile(TestTools.BROKEN_IMAGE_INCOMPLETE_CBZ); |
| 204 | |
| 205 | testTools.createTestZipArchive(brokenImageIncompleteCbz, |
| 206 | new String[]{"01.png", "incomplete.jpg", "03.png"}, null); |
| 207 | addTestComic(brokenImageIncompleteCbz, "A comic that contains a broken image."); |
| 208 | |
| 209 | File corruptedCbr = copyTestFiles(TestTools.TEST_TEXT_NAME, BROKEN_ARCHIVE_PREFIX + "corrupted.cbr"); |
| 210 | |
| 211 | addTestComic(corruptedCbr, "A broken CBR comic that is actually a text file."); |
| 212 | |
| 213 | File corruptedCbz = copyTestFiles(TestTools.TEST_TEXT_NAME, BROKEN_ARCHIVE_PREFIX + "corrupted.cbz"); |
| 214 | |
| 215 | addTestComic(corruptedCbz, "A broken CBZ comic that is actually a text file."); |
| 216 | |
| 217 | File corruptedPdf = copyTestFiles(TestTools.TEST_TEXT_NAME, BROKEN_ARCHIVE_PREFIX + "corrupted.pdf"); |
| 218 | |
| 219 | addTestComic(corruptedPdf, "A broken PDF comic that is actually a text file."); |
| 220 | |
| 221 | File brokenArchive0PagesCbz = testTools.getTestGeneratedInputFile(BROKEN_ARCHIVE_PREFIX + "0pages.cbz"); |
| 222 | |
| 223 | testTools.createTestZipArchive(brokenArchive0PagesCbz, new String[]{TestTools.TEST_TEXT_NAME}, null); |
| 224 | addTestComic(brokenArchive0PagesCbz, "A broken comic that does not contain any images, only a text file."); |
| 225 | |
| 226 | File brokenByCut = testTools.getTestFile(TestTools.TEST_IMAGE_BROKEN_BY_CUTTING_NAME); |
| 227 | File brokenByFill = testTools.getTestFile(TestTools.TEST_IMAGE_BROKEN_BY_FILLING_NAME); |
| 228 | |
| 229 | assertCannotBeLoaded(brokenByCut); |
| 230 | assertCannotBeLoaded(brokenByFill); |
| 231 | testTools.createTestZipArchive( |
| 232 | testTools.getTestGeneratedInputFile(BROKEN_ARCHIVE_PREFIX + "various.cbz"), |
| 233 | new String[]{ |
| 234 | "01." + TEST_IMAGE_SUFFIX, |
| 235 | "02." + TEST_IMAGE_SUFFIX, brokenByCut.getName(), |
| 236 | "03." + TEST_IMAGE_SUFFIX, brokenByFill.getName()}, null); |
| 237 | |
| 238 | createBrokenNoImagesPdf(); |
| 239 | } |
| 240 | |
| 241 | private File createHugeComic() |
| 242 | throws IOException { |
| 243 | File zipFile = testTools.getTestGeneratedInputFile(TestTools.HUGO_COMIC); |
| 244 | |
| 245 | if (logger.isInfoEnabled()) { |
| 246 | logger.info("create zip archive: " + zipFile); |
| 247 | } |
| 248 | byte[] buffer = new byte[BUFFER_SIZE]; |
| 249 | ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile)); |
| 250 | File imageFile = testTools.getTestImageFile(); |
| 251 | |
| 252 | try { |
| 253 | for (int page = 0; page < TestTools.HUGE_PAGE_COUNT; page += 1) { |
| 254 | |
| 255 | ZipEntry zipEntry = new ZipEntry(Integer.toString(page) + ".png"); |
| 256 | |
| 257 | // set constant dummy time so version control does not consider |
| 258 | // the archives changed |
| 259 | zipEntry.setTime(0); |
| 260 | out.putNextEntry(zipEntry); |
| 261 | |
| 262 | FileInputStream in = new FileInputStream(imageFile); |
| 263 | |
| 264 | try { |
| 265 | while (in.read(buffer) > 0) { |
| 266 | out.write(buffer); |
| 267 | } |
| 268 | } finally { |
| 269 | in.close(); |
| 270 | } |
| 271 | out.closeEntry(); |
| 272 | } |
| 273 | } finally { |
| 274 | out.close(); |
| 275 | } |
| 276 | return zipFile; |
| 277 | } |
| 278 | |
| 279 | private void createTestComicPdf() |
| 280 | throws Exception { |
| 281 | File testPdf = testTools.getTestGeneratedInputFile(TestTools.TEST_COMIC_PDF); |
| 282 | File sourceImageFolder = testTools.getTestImageFile().getParentFile(); |
| 283 | File[] allSourceImageFiles = sourceImageFolder.listFiles(new RegExFileFilter("\\d\\d(\\+\\d\\d)?\\.png")); |
| 284 | String[] sourceImageNames = fileTools.getRelativePaths(sourceImageFolder, allSourceImageFiles); |
| 285 | Conversion conversion = new Conversion(Conversion.COMIC_FORMAT_PDF); |
| 286 | Task createPdfTask = new CreateComicTask(sourceImageFolder, sourceImageNames, testPdf, conversion); |
| 287 | |
| 288 | createPdfTask.start(); |
| 289 | addTestComic(testPdf, "A simple comic in PDF containing a few pages. " |
| 290 | + "This is useful to test that a viewer can process PDF. " |
| 291 | + "No fancy PDF features are used, " |
| 292 | + "these are just simple pages containing a single image and nothing else."); |
| 293 | } |
| 294 | |
| 295 | private void createTestComics() |
| 296 | throws Exception { |
| 297 | assert standardTestNames != null; |
| 298 | assert standardTestNames.length > 0; |
| 299 | assert testTools.getTestInputFile(TestTools.TEST_COMIC_CBR).exists(); |
| 300 | createTestComicPdf(); |
| 301 | |
| 302 | File testCbz = testTools.getTestComicFile(); |
| 303 | |
| 304 | testTools.createTestZipArchive(testCbz, standardTestNames, null); |
| 305 | addTestComic(testCbz, "A collection of test pages that showcase various special cases " |
| 306 | + "the two page mode can face. Among others: " |
| 307 | + "different sizes and aspect ratios of facing paces, " |
| 308 | + "different color models of facing pages, " |
| 309 | + "landscape images."); |
| 310 | |
| 311 | File onePageCbz = testTools.getTestGeneratedInputFile(TestTools.SINGLE_PAGE_COMIC); |
| 312 | |
| 313 | testTools.createTestZipArchive(onePageCbz, |
| 314 | new String[]{"01." + TEST_IMAGE_SUFFIX}, null); |
| 315 | addTestComic(onePageCbz, "A comic with a single page. This is an interesting boundary case."); |
| 316 | |
| 317 | File onePageWithInfoCbz = testTools.getTestGeneratedInputFile("1page_with_info.cbz"); |
| 318 | |
| 319 | testTools.createTestZipArchive(onePageWithInfoCbz, |
| 320 | new String[]{"01." + TEST_IMAGE_SUFFIX, TestTools.TEST_TEXT_NAME}, null); |
| 321 | addTestComic(onePageWithInfoCbz, "A comic with one (single) page and a \"test.txt\" text file. " |
| 322 | + "A Viewer might choose to show the text file when opening the comic."); |
| 323 | |
| 324 | File twoPagesCbz = testTools.getTestGeneratedInputFile(TestTools.TWO_PAGE_COMIC); |
| 325 | |
| 326 | testTools.createTestZipArchive(twoPagesCbz, |
| 327 | new String[]{"01." + TEST_IMAGE_SUFFIX, "02." + TEST_IMAGE_SUFFIX}, null); |
| 328 | addTestComic(twoPagesCbz, "A comic with two (single) pages. " |
| 329 | + "This is an interesting boundary case, in particular for viewers that always " |
| 330 | + "show the title page seperately."); |
| 331 | |
| 332 | File threePagesCbz = testTools.getTestGeneratedInputFile(TestTools.THREE_PAGE_COMIC); |
| 333 | |
| 334 | testTools.createTestZipArchive(threePagesCbz, |
| 335 | new String[]{ |
| 336 | "01." + TEST_IMAGE_SUFFIX, |
| 337 | "02." + TEST_IMAGE_SUFFIX, |
| 338 | "03." + TEST_IMAGE_SUFFIX}, null); |
| 339 | addTestComic(threePagesCbz, "A comic with three (single) pages. " |
| 340 | + "This is an interesting boundary case."); |
| 341 | |
| 342 | File threePagesNotSortedCbz = testTools.getTestGeneratedInputFile("3pages_not_sorted.cbz"); |
| 343 | |
| 344 | testTools.createTestZipArchive(threePagesNotSortedCbz, |
| 345 | new String[]{ |
| 346 | "03." + TEST_IMAGE_SUFFIX, |
| 347 | "01." + TEST_IMAGE_SUFFIX, |
| 348 | "02." + TEST_IMAGE_SUFFIX}, null); |
| 349 | addTestComic(threePagesNotSortedCbz, "A comic with three (single) pages. " |
| 350 | + "In the archive, the images are stored in the order: 3, 1, 2. " |
| 351 | + "A viewer should be able to show them in the order 1, 2, 3."); |
| 352 | |
| 353 | File testMoronicNumberingCbz = testTools.getTestGeneratedInputFile(TestTools.TEST_MORONIC_NUMBERING_CBZ); |
| 354 | |
| 355 | testTools.createTestZipArchive(testMoronicNumberingCbz, |
| 356 | new String[]{ |
| 357 | "01." + TEST_IMAGE_SUFFIX, |
| 358 | "02." + TEST_IMAGE_SUFFIX, |
| 359 | "03." + TEST_IMAGE_SUFFIX, |
| 360 | "0405." + TEST_IMAGE_SUFFIX, |
| 361 | "06." + TEST_IMAGE_SUFFIX}, null); |
| 362 | addTestComic(testMoronicNumberingCbz, "In this comic, one file is named 0405.png, intending to " |
| 363 | + "contain pages 4 and 5. " |
| 364 | + "Many viewers treat the page number as 405, and will show it after page 6. " |
| 365 | + "This showcases \"moronic numbering\" as often used by inexperienced comic creators." |
| 366 | + "A proper naming for the image would have been \"04+05.png\"."); |
| 367 | |
| 368 | File logoTitleCbz = testTools.getTestGeneratedInputFile(TestTools.TEST_COMIC_WITH_LOGO_TITLE); |
| 369 | |
| 370 | testTools.createTestZipArchive(logoTitleCbz, |
| 371 | new String[]{"logo." + TEST_IMAGE_SUFFIX, "01." + TEST_IMAGE_SUFFIX, "02." + TEST_IMAGE_SUFFIX}, |
| 372 | new String[]{ |
| 373 | "logo." + TEST_IMAGE_SUFFIX, "xyz_01." + TEST_IMAGE_SUFFIX, |
| 374 | "xyz_02." + TEST_IMAGE_SUFFIX}); |
| 375 | addTestComic(logoTitleCbz, |
| 376 | "A comic with an ad as title page. This should still show the first comic page as preview image."); |
| 377 | |
| 378 | File disguisedZipCbr = copyTestFiles(TestTools.SINGLE_PAGE_COMIC, TestTools.DISGUISED_ZIP); |
| 379 | |
| 380 | addTestComic(disguisedZipCbr, "A ZIP comic that is improperly named as CBR. " |
| 381 | + "A viewer should still be able to properly identify the archive by looking at " |
| 382 | + "the \"magic bytes\" in the file header."); |
| 383 | |
| 384 | File disguisedRarCbz = copyTestFiles(TestTools.TEST_COMIC_CBR, TestTools.DISGUISED_RAR); |
| 385 | |
| 386 | addTestComic(disguisedRarCbz, "A RAR comic that is improperly named as CBZ. " |
| 387 | + "A viewer should still be able to properly identify the archive by looking at " |
| 388 | + "the \"magic bytes\" in the file header."); |
| 389 | |
| 390 | File comicsInArchiveZip = testTools.getTestGeneratedInputFile("special_comics_in_archive.cbz"); |
| 391 | |
| 392 | testTools.createTestZipArchive(comicsInArchiveZip, |
| 393 | new String[]{ |
| 394 | "1page.cbz", |
| 395 | "2pages.cbz", |
| 396 | TestTools.TEST_TEXT_NAME, |
| 397 | "test.cbz"}, null); |
| 398 | addTestComic(comicsInArchiveZip, "An archive that a user might think is a comic, but actually is " |
| 399 | + "a collection of comics. As it does not contain images directly, a viewer cannot show it. " |
| 400 | + "However, a viewer can explain the situation and offer to extract the archive in order to " |
| 401 | + "show the comics inside it."); |
| 402 | |
| 403 | File hugeComic = createHugeComic(); |
| 404 | |
| 405 | addTestComic(hugeComic, "An archive with many pages. Just a simple stress test."); |
| 406 | |
| 407 | File disguiseImagesZip = testTools.getTestGeneratedInputFile( |
| 408 | TestTools.IMAGES_WITH_WRONG_SUFFIX_COMIC); |
| 409 | |
| 410 | testTools.createTestZipArchive(disguiseImagesZip, |
| 411 | new String[]{TestTools.TEST_IMAGE_PNG_NAME, TestTools.TEST_IMAGE_PNG_NAME}, |
| 412 | new String[]{"01.jpg", "02.png"}); |
| 413 | addTestComic(comicsInArchiveZip, "An archive with a JPEG image having a \".png\" suffix ." |
| 414 | + " a PNG image having a \".jpg\" suffix. A viewer should still be able to view it " |
| 415 | + " without any problems. A PDF converter should detect the PNG image properly " |
| 416 | + " and convert it to a JPEG before including it in the PDF."); |
| 417 | |
| 418 | File landscapeOnlyComic = testTools.getTestGeneratedInputFile( |
| 419 | TestTools.TEST_COMIC_LANDSCAPE_ONLY); |
| 420 | |
| 421 | testTools.createTestZipArchive(landscapeOnlyComic, |
| 422 | new String[]{TestTools.TEST_IMAGE_LANDSCAPE_NAME, TestTools.TEST_IMAGE_LANDSCAPE_NAME}, |
| 423 | new String[]{"01.jpg", "02.png"}); |
| 424 | addTestComic(comicsInArchiveZip, "An which contains only landscape images. A viewer should" |
| 425 | + " be able to handle a landscape title page correctly, in particular the page" |
| 426 | + " numbers of the title and the following page must be correct (1+2 and 3+4)."); |
| 427 | |
| 428 | assert hasInternalError(testTools.getTestFile(TestTools.TEST_COMIC_INTERNAL_ERROR)); |
| 429 | } |
| 430 | |
| 431 | private void createTestImages() |
| 432 | throws IOException { |
| 433 | writeTestImage(WIDTH, HEIGHT, "logo", "some cool logo"); |
| 434 | writeTestImage(WIDTH, HEIGHT, "01", "front single"); |
| 435 | writeTestImage(WIDTH, HEIGHT, "02", "single 1"); |
| 436 | writeTestImage(WIDTH, HEIGHT, "03", "single 2"); |
| 437 | writeTestImage(2 * WIDTH, HEIGHT, "04+05", "double"); |
| 438 | writeTestImage(WIDTH, HEIGHT, "06", "single 1 after double"); |
| 439 | writeTestImage(WIDTH, (int) (DIFFERENT_RATIO * HEIGHT), "07", "single 2 after double"); |
| 440 | writeTestImage(WIDTH, HEIGHT, "08", "lone single before double"); |
| 441 | writeTestFiller("09+10", "double after lone single"); |
| 442 | writeTestImage(WIDTH, HEIGHT, "11", "same ratio, normal"); |
| 443 | writeTestImage(2 * WIDTH, 2 * HEIGHT, "12", "same ratio, big"); |
| 444 | writeTestImage(2 * WIDTH, 2 * HEIGHT, "13", "same ratio, big"); |
| 445 | writeTestImage(WIDTH, HEIGHT, "14", "same ratio, normal"); |
| 446 | writeTestImage(WIDTH, HEIGHT, "15", "different ratio, normal"); |
| 447 | writeTestImage(WIDTH, (int) (DIFFERENT_RATIO * HEIGHT), "16", "different ratio, normal"); |
| 448 | writeTestImage(2 * WIDTH, HEIGHT, BufferedImage.TYPE_BYTE_GRAY, "17+18", "gray"); |
| 449 | writeTestImage(WIDTH, HEIGHT, BufferedImage.TYPE_BYTE_GRAY, "19", "gray"); |
| 450 | writeTestImage(WIDTH, HEIGHT, "20", "color"); |
| 451 | writeTestImage(WIDTH, HEIGHT, "21", "color"); |
| 452 | writeTestImage(WIDTH, HEIGHT, BufferedImage.TYPE_BYTE_GRAY, "22", "gray"); |
| 453 | writeTestImage(WIDTH, HEIGHT, BufferedImage.TYPE_BYTE_GRAY, "23", "gray, different ratio, normal"); |
| 454 | writeTestImage(WIDTH, (int) (DIFFERENT_RATIO * HEIGHT), |
| 455 | BufferedImage.TYPE_BYTE_GRAY, "24", "gray, different ratio, normal"); |
| 456 | writeTestImage(12 * WIDTH / 10, HEIGHT, "25", "same hight, wide"); |
| 457 | writeTestImage(8 * WIDTH / 10, HEIGHT, "26", "same hight, narrow"); |
| 458 | |
| 459 | writeTestImage(WIDTH, HEIGHT, "27.jpg", "JFIF/JPEG"); |
| 460 | // TODO: generate these files using a proper ImageWriter |
| 461 | testTools.copyTestFile("01.jp2", "28.jp2"); |
| 462 | testTools.copyTestFile("01.87a.gif", "29.gif"); |
| 463 | testTools.copyTestFile("01.89a.gif", "30.gif"); |
| 464 | testTools.copyTestFile("01.mac.tiff", "31.tiff"); |
| 465 | testTools.copyTestFile("01.ibm.tiff", "32.tiff"); |
| 466 | |
| 467 | // TODO: create images with 1, 4, and 6 bits depth |
| 468 | // TODO: create incomplete images with different formats |
| 469 | |
| 470 | writeTestFiller("97+98", "double before single at end"); |
| 471 | writeTestImage(WIDTH, HEIGHT, "99", "single at end"); |
| 472 | standardTestNames = (String[]) testImageNames.toArray(new String[0]); |
| 473 | testTools.copyTestFile("04+05." + TEST_IMAGE_SUFFIX, "0405." + TEST_IMAGE_SUFFIX); |
| 474 | testTools.copyTestFile(TestTools.TEST_IMAGE_PNG_NAME, TestTools.TEST_IMAGE_DISGUISED_PNG); |
| 475 | testTools.copyTestFile(TestTools.TEST_IMAGE_JPG_NAME, TestTools.TEST_IMAGE_DISGUISED_JPG); |
| 476 | |
| 477 | writeHugeImage(); |
| 478 | writeEmptyFile(TestTools.TEST_IMAGE_BROKEN_EMPTY_NAME); |
| 479 | } |
| 480 | |
| 481 | private void createTestTexts() |
| 482 | throws IOException { |
| 483 | File testTxtFile = testTools.getTestGeneratedInputFile(TestTools.TEST_TEXT_NAME); |
| 484 | FileWriter writer = new FileWriter(testTxtFile); |
| 485 | |
| 486 | try { |
| 487 | writer.write("dummy description of a comic\n"); |
| 488 | } finally { |
| 489 | writer.close(); |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | private boolean hasInternalError(File brokenZipFile) |
| 494 | throws IOException { |
| 495 | boolean result = false; |
| 496 | ZipFile zipFile = null; |
| 497 | |
| 498 | try { |
| 499 | zipFile = new ZipFile(brokenZipFile, ZipFile.OPEN_READ); |
| 500 | |
| 501 | Enumeration zipEntries = zipFile.entries(); |
| 502 | |
| 503 | if (zipEntries.hasMoreElements()) { |
| 504 | zipEntries.nextElement(); |
| 505 | } |
| 506 | } catch (InternalError bug) { |
| 507 | result = true; |
| 508 | } catch (Exception error) { |
| 509 | if (logger.isDebugEnabled()) { |
| 510 | logger.debug("ignoring broken archive while checking for internal error", error); |
| 511 | } |
| 512 | } finally { |
| 513 | if (zipFile != null) { |
| 514 | zipFile.close(); |
| 515 | } |
| 516 | } |
| 517 | return result; |
| 518 | } |
| 519 | |
| 520 | private void writeEmptyFile(String name) |
| 521 | throws IOException { |
| 522 | File targetFile = testTools.getTestGeneratedInputFile(name); |
| 523 | OutputStream out = new FileOutputStream(targetFile); |
| 524 | |
| 525 | out.close(); |
| 526 | } |
| 527 | |
| 528 | private void writeHugeImage() |
| 529 | throws IOException { |
| 530 | String suffix = "jpg"; |
| 531 | BufferedImage image = new BufferedImage(3 * WIDTH, 3 * HEIGHT, BufferedImage.TYPE_INT_ARGB); |
| 532 | Graphics2D g2d = image.createGraphics(); |
| 533 | |
| 534 | try { |
| 535 | GradientPaint gradient = new GradientPaint(0, 0, Color.BLUE, |
| 536 | image.getWidth() / 6, image.getHeight() / 6, Color.RED, true); |
| 537 | |
| 538 | //g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 0.0f)); |
| 539 | g2d.setPaint(gradient); |
| 540 | g2d.fill(new Rectangle2D.Double(image.getWidth() / 4, image.getHeight() / 4, |
| 541 | image.getWidth() / 2, image.getHeight() / 2)); |
| 542 | } finally { |
| 543 | g2d.dispose(); |
| 544 | } |
| 545 | |
| 546 | |
| 547 | File file = testTools.getTestGeneratedInputFile("huge." + suffix); |
| 548 | Iterator writers = ImageIO.getImageWritersByFormatName(suffix); |
| 549 | ImageWriter writer = (ImageWriter) writers.next(); |
| 550 | ImageWriteParam writeParameters = writer.getDefaultWriteParam(); |
| 551 | |
| 552 | writeParameters.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); |
| 553 | writeParameters.setCompressionQuality(1); |
| 554 | if (logger.isInfoEnabled()) { |
| 555 | logger.info("write test image " + stringTools.sourced(file.getAbsolutePath())); |
| 556 | } |
| 557 | ImageOutputStream ios = ImageIO.createImageOutputStream(file); |
| 558 | |
| 559 | try { |
| 560 | writer.setOutput(ios); |
| 561 | writer.write(image); |
| 562 | } finally { |
| 563 | ios.close(); |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | private void writeTestFiller(String baseFileName, String description) |
| 568 | throws IOException { |
| 569 | writeTestImage(2 * WIDTH, HEIGHT, baseFileName, "filler: " + description); |
| 570 | } |
| 571 | |
| 572 | private void writeTestImage(int width, int height, String baseFileName, String details) |
| 573 | throws IOException { |
| 574 | writeTestImage(width, height, BufferedImage.TYPE_INT_RGB, baseFileName, details); |
| 575 | } |
| 576 | |
| 577 | private void writeTestImage(int width, int height, int imageType, String baseFileName, String details) |
| 578 | throws IOException { |
| 579 | assert baseFileName != null; |
| 580 | assert width > 0; |
| 581 | assert height > 0; |
| 582 | String suffix = fileTools.getSuffix(baseFileName); |
| 583 | String label; |
| 584 | |
| 585 | if (suffix.length() == 0) { |
| 586 | label = baseFileName; |
| 587 | suffix = TEST_IMAGE_SUFFIX; |
| 588 | baseFileName += "." + suffix; |
| 589 | } else { |
| 590 | label = baseFileName.substring(0, baseFileName.length() - suffix.length() - 1); |
| 591 | } |
| 592 | if (logger.isDebugEnabled()) { |
| 593 | logger.debug("label=" + stringTools.sourced(label)); |
| 594 | } |
| 595 | RenderedImage image = jaiUnitTools.createTestImage(width, height, imageType, label, details); |
| 596 | Iterator writers = ImageIO.getImageWritersByFormatName(suffix); |
| 597 | ImageWriter writer = (ImageWriter) writers.next(); |
| 598 | File file = testTools.getTestGeneratedInputFile(baseFileName); |
| 599 | |
| 600 | if (logger.isInfoEnabled()) { |
| 601 | logger.info("write test image " + stringTools.sourced(file.getAbsolutePath())); |
| 602 | } |
| 603 | ImageOutputStream ios = ImageIO.createImageOutputStream(file); |
| 604 | |
| 605 | try { |
| 606 | writer.setOutput(ios); |
| 607 | writer.write(image); |
| 608 | } finally { |
| 609 | ios.close(); |
| 610 | } |
| 611 | testImageNames.add(baseFileName); |
| 612 | } |
| 613 | } |