Improved Bitmap Converter

Libraries, utilities, bootloaders...

Improved Bitmap Converter

Postby AntonyPrince » Mon Aug 11, 2014 8:34 pm

I took the Processing code for the bitmap converter and made a few improvements.

- Now displays a file chooser to select the input file
- Will only allow *.bmp files to be selected
- Program exits if no file is chosen
- Added error message dialogs
- Added ability to repeat operation (for converting multiple bitmaps)
- Directory that bitmap is chosen from in the file chooser is remembered between operations
- Output files are named after the input file, "File1.bmp" yields "File1.txt"
- Changed from "unsigned char" to "byte" according to Arduino coding conventions
- Variable names in text file are named after bmp file, "File1.bmp" yields "static byte PROGMEM File1[] = "

Binaries: (require that Java is installed)
Windows 32 bit
MD5: 520be8ae6d4b4b3b0903271efea03ec3
SHA256: ad6dd984db3ca59d74ae2eeda6e109217b6683ea280b59ab0e809cce443540e8
Windows 64 bit
MD5: f958f846a4c78024dc0b6849155aea8d
SHA256: 08f91ad8844a22b5d69d51fe043795bef731d1eabb688d92c2f649e090b8c857
Linux x86
MD5: e44c8ec0522d88e7f300ae4b74789e44
SHA256: fc8e4927bc4fc60ef850414e3fd7d1df63c75733dd6c0e24723abc57c57024e5
Linux x64
MD5: f5a4477969f4f7f5901d5ae3018cff81
SHA256: 01694dad0b3fc4aa0633ef83d12f5851b0db757c50e9c3e54c165b265d0fb101
Source
MD5: 72580339f10c35293a78b2f3dfc2d518
SHA256: 6f7f7050a660e40b1d5a493b105a95070c82a5fa178808e243806992a91a5f03

Source:
Code: Select all
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.UIManager;

String fileName = "";
File chooserDirectory = null;

void setup() {
  size(84, 48);

  PImage img = getImage();
 
  if (img == null) {
    JOptionPane.showMessageDialog(null, "No input image selected", "No Image", JOptionPane.INFORMATION_MESSAGE);
    System.exit(0);
  }
 
  image(img, 0, 0);

  PrintWriter output;
  output = createWriter(fileName + ".txt");
  output.println("static byte PROGMEM " + fileName + "[] =");
  output.println("{");
  output.print("  ");
  output.print(img.width);
  output.print(",");
  output.print(img.height);
  output.println(", //width and height");

  img.loadPixels();

  for (int y = 0; y<img.height; y++) {
    output.print("  ");
    for (int x = 0; x<img.width; x+=8) {
      output.print("B");
      for (int b = 0; b<8; b++) {
        color thisColor = img.get(x+b, y);
        if (brightness(thisColor) > 100) {
          output.print("0");
          img.set(x+b, y, color(255));
        } else {
          output.print("1");
          img.set(x+b, y, color(0));
        }
      }
      output.print(", ");
    }
    output.println();
    if ((y%8)==7) {
      output.println();
    }
  }

  img.updatePixels();
  image(img, 0, 100);

  output.print("};");
  output.flush();
  output.close();
  int result = JOptionPane.showConfirmDialog (null, "Convert another bitmap?","Again?", JOptionPane.YES_NO_OPTION);
  if (result == JOptionPane.YES_OPTION) {
    fileName = "";
    setup();
  }
}

PImage getImage() {
  // set system look and feel
  try {
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  }
  catch (Exception e) {
    e.printStackTrace();
  }

  // create a file chooser
  final JFileChooser fc = new JFileChooser();
  FileNameExtensionFilter filter = new FileNameExtensionFilter("Windows bitmap", "bmp");
  fc.setFileFilter(filter);
  fc.setAcceptAllFileFilterUsed(false);
  if (chooserDirectory != null) {
    fc.setCurrentDirectory(chooserDirectory);
  }
  int returnVal = fc.showOpenDialog(this);
  if (returnVal == JFileChooser.APPROVE_OPTION) {
    File file = fc.getSelectedFile();
    chooserDirectory = fc.getCurrentDirectory();
    // see if it's an image
    // (better to write a function and check for all supported extensions)
    if (file.getName().endsWith(".bmp")) {
      // load the image using the given file path
      fileName = file.getName().substring(0, file.getName().lastIndexOf("."));
      PImage img = loadImage(file.getPath());
      return img;
    } else {
      JOptionPane.showMessageDialog(null, "Not a bitmap.", "Error", JOptionPane.ERROR_MESSAGE);
    }
  } else {
    JOptionPane.showMessageDialog(null, "Operation cancelled by user", "Cancelled", JOptionPane.INFORMATION_MESSAGE);
  }
  return null;
}
User avatar
AntonyPrince
 
Posts: 21
Joined: Thu Aug 07, 2014 1:07 am
Location: Virginia, United States

Re: Improved Bitmap Converter

Postby rodot » Mon Aug 11, 2014 9:04 pm

Mmmh, but the processing bitmap editor is outdated, the last version in made using native java and is on GitHub :/
User avatar
rodot
Site Admin
 
Posts: 1290
Joined: Mon Nov 19, 2012 11:54 pm
Location: France

Re: Improved Bitmap Converter

Postby AntonyPrince » Mon Aug 11, 2014 9:08 pm

rodot wrote:Mmmh, but the processing bitmap editor is outdated, the last version in made using native java and is on GitHub :/


Now I know. Haha! I just searched the forums yesterday for "bitmap converter" and it was the first thing I saw. Works fairly well despite using an external library. I'll have a look at the one on Github in a bit.
User avatar
AntonyPrince
 
Posts: 21
Joined: Thu Aug 07, 2014 1:07 am
Location: Virginia, United States


Return to Software Development

Who is online

Users browsing this forum: No registered users and 14 guests

cron