title: Image Compression Algorithm - Breaking an Image Into Squares By Color
body: <p>I'm trying to develop a mobile application, and I'm wondering the easiest way to convert an image into a text file, and then be able to recreate it later in memory said text.  The image(s) in question will contain no more than 16 or so colors, so it would work out fine.</p>
  
  <p>Basically, brute-forcing this solution would require me saving each individual's pixel color data into a file.  However, this would result in a HUGE file.  <strong>I know there's a better way - like, if there's a huge portion of the image that consists of the same color, breaking up the area into smaller squares and rectangles and saving their coordinates and size to file.</strong></p>
  
  <p>Here's an example.  The image is supposed to be just black/white. The big color boxes represent theoretical 'data points' in the outputted text file.  These boxes would really state their origin, size, and what color they should be.</p>
  
  <p>E.g., top box has an origin of 0,0, a size of 359,48, and it represents the color black.
  Saved in a text file, the data would be 0,0,359,48,0.</p>
  
  <p><img src="https://i.stack.imgur.com/Y3wRM.png" alt="sample algorithm output"></p>
  
  <p>What kind of algorithm would this be?</p>
  
  <p>NOTE: The SDK that I am using cannot return a pixel's color from an X,Y coordinate.  However, I can load external information into the program from a text file and manipulate it that way.  This data that I need to export to a text file will be from a different utility that will have the capability to get a pixel's color from X,Y coordinates. </p>
  
  <p>EDIT: Added a picture
  EDIT2: Added constraints</p>