Exemplo n.º 1
0
  public static void main(String[] args) throws IOException {
    DesktopSetup.deploy();

    TextureSlicer.installComponent(new RedTextureSlicer());

    File input_folder = LocalFileSystem.ApplicationHome().child("input");
    File output_folder = LocalFileSystem.ApplicationHome().child("output");

    File input_file = input_folder.child("example.jpg");

    output_folder.makeFolder();
    output_folder.clearFolder();

    TextureSlicerSpecs specs = TextureSlicer.newDecompositionSpecs();
    specs.setInputFile(input_file);
    int tile_size = 256 * 2;
    int margin = TextureSlicerSpecs.MIN_TILE_SIZE / 2;
    specs.setTileWidth(tile_size - 2 * margin);
    specs.setTileHeight(tile_size - 2 * margin);
    specs.setMargin(margin);

    ID package_name = Names.newID("com.jfixby.tool.texture.slicer.example");

    specs.setNameSpacePrefix(package_name);

    specs.setOutputFolder(output_folder);

    TextureSlicingResult result = TextureSlicer.decompose(specs);
    { // Save resulting structure info
      L.d("output", result.getRasterID());
      SlicesCompositionInfo composition = result.getTilesComposition();
      SlicesCompositionsContainer container = new SlicesCompositionsContainer();
      container.content.addElement(composition);
      String data = Json.serializeToString(container).toString();

      ID sctruct_package_name = package_name.child(TextureSlicerSpecs.TILE_MAP_FILE_EXTENSION);
      String struct_pkg_name = sctruct_package_name.toString();
      File container_file = output_folder.child(struct_pkg_name);
      container_file.writeString(data);

      // com.badlogic.gdx.utils.Json j = new
      // com.badlogic.gdx.utils.Json();
      JsonValue gdx_json = new JsonReader().parse(data);
      L.d("structure:");
      L.d(gdx_json.toString());
    }
  }
  public static FolderSupportingIndex rebuild(final FolderSupportingIndexBuilderParams params)
      throws IOException {
    final File file = params.getTarget();

    if (!file.isFolder()) {
      Err.reportError("Is not folder " + file);
    }
    if (params.getDebug()) {
      L.d(file);
    }
    final ChildrenList children = file.listDirectChildren();
    // children.print("children");
    final FolderSupportingIndex desc = new FolderSupportingIndex();
    Collections.scanCollection(
        children,
        new CollectionScanner<File>() {
          @Override
          public void scanElement(final File e, final long i) {
            if (e.getName().startsWith(FolderSupportingIndex.FILE_NAME)) {
              return;
            }
            final FolderSupportingIndexEntry entry = new FolderSupportingIndexEntry();
            try {
              entry.name = e.getName();

              entry.is_file = e.isFile();
              entry.is_folder = e.isFolder();
              entry.lastModified = e.lastModified();
              entry.size = e.getSize();
              if (!params.ignoreHashSum() && e.isFile()) {
                entry.hash = e.calculateHash().getMD5HashHexString();
              }
              desc.entries.put(entry.name, entry);
              if (e.isFolder()) {
                final FolderSupportingIndexBuilderParams paramsNext = params.copy();
                paramsNext.setTarget(e);
                if (params.rebuidOnlyForRoot()) {
                  paramsNext.setNoOutput(true);
                } else {
                  paramsNext.setNoOutput(params.noOutput());
                  // paramsNext.noOutput = params.noOutput;
                }
                paramsNext.setIgnoreHashSum(params.ignoreHashSum());
                paramsNext.setDebug(params.getDebug());

                if (params.recoursive()) {
                  final FolderSupportingIndex sublevel = rebuild(paramsNext);
                  desc.children.put(entry.name, sublevel);
                }
              }
            } catch (final IOException e1) {
              Err.reportError(e1);
            }
          }
        });
    if (!params.noOutput()) {
      FolderSupportingIndex deckCheck = null;
      {
        final File desc_file = file.child(FolderSupportingIndex.FILE_NAME);
        L.d("writing", desc_file);

        final FileOutputStream os = desc_file.newOutputStream();
        os.open();
        HTTPOperator.encode(desc, os);
        os.close();

        final ByteArray dataCheck = desc_file.readBytes();
        deckCheck = HTTPOperator.decode(dataCheck);
      }
      {
        final File desc_file_json = file.child(FolderSupportingIndex.FILE_NAME + ".json");
        L.d("writing", desc_file_json);
        final JsonString stringData = Json.serializeToString(desc);
        final JsonString testStringData = Json.serializeToString(deckCheck);
        final String s1 = stringData.toString();
        final String s2 = testStringData.toString();
        if (!s1.equals(s2)) {

          L.e("    stringData " + s1.length());
          L.e("testStringData " + s2.length());
          L.e(desc_file_json);
          // for (int k = 0; k < s1.length(); k++) {
          // final char c1 = s1.charAt(k);
          // final char c2 = s2.charAt(k);
          // if (c1 != c2) {
          //// L.d_appendChars("#");L.d_appendChars("#");L.d_appendChars("#");
          //// L.d_appendChars("" + c1);
          //// L.d_appendChars("#");
          // } else {
          //// L.d_appendChars("" + c1);
          // }
          // }

          L.e("decoder fails");
        }
        final String data = stringData.toString();
        desc_file_json.writeString(data);
      }
    }
    return desc;
  }