Skip to content

Instantly share code, notes, and snippets.

@ssvb
Created March 3, 2015 23:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ssvb/ea382b2f9da241968373 to your computer and use it in GitHub Desktop.
Save ssvb/ea382b2f9da241968373 to your computer and use it in GitHub Desktop.
Conversion from 16bpp to 32bpp
/*
* Copyright (C) 2015 Siarhei Siamashka <siarhei.siamashka@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include <inttypes.h>
#include <stdio.h>
#include <string.h>
#define WIDTH 16
#define HEIGHT 10
uint16_t input_buf[WIDTH * HEIGHT];
uint32_t output_buf[WIDTH * HEIGHT];
#ifdef __arm__
/*
* This uses the ARM assembly function directly. Just include
* http://cgit.freedesktop.org/pixman/plain/pixman/pixman-arm-neon-asm.S
* http://cgit.freedesktop.org/pixman/plain/pixman/pixman-private.h
* http://cgit.freedesktop.org/pixman/plain/pixman/pixman-arm-neon-asm.h
* http://cgit.freedesktop.org/pixman/plain/pixman/pixman-arm-asm.h
* into your source tree (these files use MIT X11 license)
*
* Compile:
* gcc convert-16bpp-to-32bpp.c pixman-arm-neon-asm.S
*/
void pixman_composite_src_0565_8888_asm_neon(int width,
int height,
uint32_t *dst,
int dst_stride_pixels,
uint16_t *src,
int src_stride_pixels);
void convert_16bpp_to_32bpp(int width,
int height,
uint32_t *dst,
int dst_stride_pixels,
uint16_t *src,
int src_stride_pixels)
{
pixman_composite_src_0565_8888_asm_neon(width, height,
dst, dst_stride_pixels,
src, src_stride_pixels);
}
#else
/*
* This uses the standard pixman API and links with the pixman library.
*
* Compile:
* gcc `pkg-config --cflags --libs pixman-1` convert-16bpp-to-32bpp.c
*/
#include <pixman.h>
void convert_16bpp_to_32bpp(int width,
int height,
uint32_t *dst,
int dst_stride_pixels,
uint16_t *src,
int src_stride_pixels)
{
pixman_image_t *src_img, *dst_img;
/* Stride must be a multiple of 4 bytes */
if (src_stride_pixels % 2 != 0)
return;
/* Must be 4 bytes aligned */
if ((uintptr_t)src & 3 != 0)
return;
dst_img = pixman_image_create_bits(PIXMAN_a8r8g8b8, width, height,
dst,
dst_stride_pixels * sizeof(uint32_t));
src_img = pixman_image_create_bits(PIXMAN_r5g6b5, width, height,
(uint32_t *)src,
src_stride_pixels * sizeof(uint16_t));
pixman_image_composite (PIXMAN_OP_SRC, src_img, NULL, dst_img,
0, 0, 0, 0, 0, 0, width, height);
pixman_image_unref(src_img);
pixman_image_unref(dst_img);
}
#endif
int main(void)
{
int x, y;
memset(input_buf, 0xCC, sizeof(input_buf));
convert_16bpp_to_32bpp(WIDTH, HEIGHT, output_buf, WIDTH, input_buf, WIDTH);
for (y = 0; y < HEIGHT; y++) {
for (x = 0; x < WIDTH; x++)
printf("%08X ", output_buf[y * WIDTH + x]);
printf("\n");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment