rgbtobgr565.con commit improve webp-convert.sh (b8ae487)
   1/* rgbtobgr565 - convert 24-bit RGB pixels to 16-bit BGR565 pixels
   2
   3  Written in 2016 by Glenn Randers-Pehrson <glennrp@users.sf.net>
   4
   5  To the extent possible under law, the author has dedicated all copyright
   6  and related and neighboring rights to this software to the public domain
   7  worldwide. This software is distributed without any warranty.
   8  See <http://creativecommons.org/publicdomain/zero/1.0/>. 
   9
  10  Use with ImageMagick or GraphicsMagick to convert 24-bit RGB pixels
  11  to 16-bit BGR565 pixels, e.g.,
  12
  13      magick file.png -depth 8 rgb:- | rgbtobgr565 > file.bgr565
  14
  15  Note that the 16-bit pixels are written in network byte order (most
  16  significant byte first), with blue in the most significant bits and
  17  red in the least significant bits.
  18
  19  ChangLog:
  20  Jan 2017: changed bgr565 from int to unsigned short (suggested by
  21             Steven Valsesia)
  22*/
  23
  24#include <stdio.h>
  25int main()
  26{
  27    int red,green,blue;
  28    unsigned short bgr565;
  29    while (1) {
  30        red=getchar(); if (red == EOF) return (0);
  31        green=getchar(); if (green == EOF) return (1);
  32        blue=getchar(); if (blue == EOF) return (1);
  33        bgr565 = (unsigned short)(red * 31.0 / 255.0) |
  34                 (unsigned short)(green * 63.0 / 255.0) << 5 |
  35                 (unsigned short)(blue * 31.0 / 255.0) << 11;
  36            putchar((bgr565 >> 8) & 0xFF);
  37            putchar(bgr565 & 0xFF);
  38        }
  39    }