/* NOTE: it overwrites the output file without warning if it exists!  */
unsigned decodeBMP(std::vector< unsigned char > &image, unsigned &w, unsigned &h, const std::vector< unsigned char > &bmp)
/* NOTE: it overwrites the output file without warning if it exists!  */
unsigned decodeBMP(std::vector< unsigned char > &image, unsigned &w, unsigned &h, const std::vector< unsigned char > &bmp)
{
    unsigned bmpos = pixeloffset + (h - y - 1) * scanlineBytes + numChannels * x;
    unsigned newpos = 4 * y * w + 4 * x;
    if(numChannels == 3) {
      image[newpos + 0] = bmp[bmpos + 2];
      image[newpos + 1] = bmp[bmpos + 1];
      image[newpos + 2] = bmp[bmpos + 0];
      image[newpos + 3] = 255;
    } else {
      image[newpos + 0] = bmp[bmpos + 2];
      image[newpos + 1] = bmp[bmpos + 1];
      image[newpos + 2] = bmp[bmpos + 0];
      image[newpos + 3] = bmp[bmpos + 3];
    }
  }
  return 0;
}