/**
   * Wraps a BufferedImage inside a Photoshop _8BIM
   *
   * @param thumbnail input thumbnail image
   * @return a Photoshop _8BMI
   * @throws IOException
   */
  public static _8BIM createThumbnail8BIM(Bitmap thumbnail) throws IOException {
    // Create memory buffer to write data
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    // Compress the thumbnail
    try {
      thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, bout);
    } catch (Exception e) {
      e.printStackTrace();
    }
    byte[] data = bout.toByteArray();
    bout.reset();
    // Write thumbnail format
    IOUtils.writeIntMM(bout, 1); // 1 = kJpegRGB. We are going to write JPEG format thumbnail
    // Write thumbnail dimension
    int width = thumbnail.getWidth();
    int height = thumbnail.getHeight();
    IOUtils.writeIntMM(bout, width);
    IOUtils.writeIntMM(bout, height);
    // Padded row bytes = (width * bits per pixel + 31) / 32 * 4.
    int bitsPerPixel = 24;
    int planes = 1;
    int widthBytes = (width * bitsPerPixel + 31) / 32 * 4;
    IOUtils.writeIntMM(bout, widthBytes);
    // Total size = widthbytes * height * planes
    IOUtils.writeIntMM(bout, widthBytes * height * planes);
    // Size after compression. Used for consistency check.
    IOUtils.writeIntMM(bout, data.length);
    IOUtils.writeShortMM(bout, bitsPerPixel);
    IOUtils.writeShortMM(bout, planes);
    bout.write(data);
    // Create 8BIM
    _8BIM bim = new _8BIM(ImageResourceID.THUMBNAIL_RESOURCE_PS5, "thumbnail", bout.toByteArray());

    return bim;
  }