Пример #1
0
        def _optimize_image(self, src_path, json_path, js_path):
            """Begins tasks to identify, optimize, and write metadata for a single
      input image.

      Args:
        src_path: Source path to an image file.
        json_path: JSON file output path.
        js_path: JS file output path.

      Returns:
        A list of Deferreds for all image tasks.
      """
            ds = []

            rel_src_path = anvil.util.strip_build_paths(
                os.path.relpath(src_path, self.build_env.root_path))
            rel_json_path = anvil.util.strip_build_paths(
                os.path.relpath(json_path, self.build_env.root_path))

            # TODO(benvanik): want to split this into tasks:
            # - IdentifyImageTask: read metadata, decide what to do
            # - *ExecutionTask[]: various conversions (if required)
            # - Gather back, write metadata

            # TODO(benvanik): identify in a task
            (width, height, channels) = self._identify_image(src_path)

            # TODO(benvanik): encode? drop invalid name things? (whitespace/etc)
            class_name = os.path.splitext(os.path.basename(src_path))[0]

            # TODO(benvanik): proper image info construction
            class Image(object):
                pass

            image = Image()
            image.class_name = '%s.%s' % (self.rule.namespace, class_name)
            image.friendly_name = class_name
            image.src_path = rel_src_path
            image.base_path = ensure_forwardslashes(
                os.path.dirname(rel_src_path))
            image.json_path = ensure_forwardslashes(rel_json_path)
            image.width = width
            image.height = height
            image.channels = channels
            image.slot_size = \
                self.rule.slot_size.split('x') if self.rule.slot_size else None

            # TODO(benvanik) proper mime type
            mime_type = {
                '.png': 'image/png',
                '.jpg': 'image/jpeg',
                '.gif': 'image/gif',
                '.webp': 'image/webp',
            }[os.path.splitext(src_path)[1]]

            class ImageLod(object):
                pass

            lod0 = ImageLod()
            lod0.type = mime_type
            lod0.path = ensure_forwardslashes(os.path.basename(src_path))
            lod0.size = os.path.getsize(src_path)
            image.lod_list = [
                [
                    lod0,
                ],
            ]

            # TODO(benvanik): optimize, instead of just copying source
            self._append_output_paths([src_path])

            (json_template, js_template) = _get_template_paths()
            # Generate JSON
            ds.append(
                self._run_task_async(
                    MakoTemplateTask(self.build_env, json_path, json_template,
                                     {
                                         'image': image,
                                     })))
            # Generate JS
            ds.append(
                self._run_task_async(
                    MakoTemplateTask(self.build_env, js_path, js_template, {
                        'image': image,
                    })))
            return ds
Пример #2
0
    def _optimize_image(self, src_path, json_path, js_path):
      """Begins tasks to identify, optimize, and write metadata for a single
      input image.

      Args:
        src_path: Source path to an image file.
        json_path: JSON file output path.
        js_path: JS file output path.

      Returns:
        A list of Deferreds for all image tasks.
      """
      ds = []

      rel_src_path = anvil.util.strip_build_paths(
          os.path.relpath(src_path, self.build_env.root_path))
      rel_json_path = anvil.util.strip_build_paths(
          os.path.relpath(json_path, self.build_env.root_path))

      # TODO(benvanik): want to split this into tasks:
      # - IdentifyImageTask: read metadata, decide what to do
      # - *ExecutionTask[]: various conversions (if required)
      # - Gather back, write metadata

      # TODO(benvanik): identify in a task
      (width, height, channels) = self._identify_image(src_path)

      # TODO(benvanik): encode? drop invalid name things? (whitespace/etc)
      class_name = os.path.splitext(os.path.basename(src_path))[0]

      # TODO(benvanik): proper image info construction
      class Image(object):
        pass
      image = Image()
      image.class_name = '%s.%s' % (self.rule.namespace, class_name)
      image.friendly_name = class_name
      image.src_path = rel_src_path
      image.base_path = ensure_forwardslashes(os.path.dirname(rel_src_path))
      image.json_path = ensure_forwardslashes(rel_json_path)
      image.width = width
      image.height = height
      image.channels = channels
      image.slot_size = \
          self.rule.slot_size.split('x') if self.rule.slot_size else None

      # TODO(benvanik) proper mime type
      mime_type = {
          '.png': 'image/png',
          '.jpg': 'image/jpeg',
          '.gif': 'image/gif',
          '.webp': 'image/webp',
          }[os.path.splitext(src_path)[1]]
      class ImageLod(object):
        pass
      lod0 = ImageLod()
      lod0.type = mime_type
      lod0.path = ensure_forwardslashes(os.path.basename(src_path))
      lod0.size = os.path.getsize(src_path)
      image.lod_list = [
          [lod0,],
          ]

      # TODO(benvanik): optimize, instead of just copying source
      self._append_output_paths([src_path])

      (json_template, js_template) = _get_template_paths()
      # Generate JSON
      ds.append(self._run_task_async(MakoTemplateTask(
          self.build_env, json_path, json_template, {
              'image': image,
              })))
      # Generate JS
      ds.append(self._run_task_async(MakoTemplateTask(
          self.build_env, js_path, js_template, {
              'image': image,
              })))
      return ds