source: libpipi/trunk/common.h @ 2228

Last change on this file since 2228 was 2228, checked in by Sam Hocevar, 15 years ago
  • Import image loading features from PWNtcha. As they are more than 4-year old, they will certainly require some cleanup, eg. pkg-config transitions.
File size: 2.1 KB
Line 
1/*
2 *  libpipi       Proper image processing implementation library
3 *  Copyright (c) 2004-2008 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id$
7 *
8 *  This library is free software. It comes without any warranty, to
9 *  the extent permitted by applicable law. You can redistribute it
10 *  and/or modify it under the terms of the Do What The Fuck You Want
11 *  To Public License, Version 2, as published by Sam Hocevar. See
12 *  http://sam.zoy.org/wtfpl/COPYING for more details.
13 */
14
15/*
16 *  This file contains replacements for commonly found object types and
17 *  function prototypes that are sometimes missing.
18 */
19
20/* C99 types */
21#if defined HAVE_INTTYPES_H && !defined __KERNEL__
22#   include <inttypes.h>
23#else
24typedef signed char int8_t;
25typedef signed short int16_t;
26typedef signed long int int32_t;
27
28typedef unsigned char uint8_t;
29typedef unsigned short uint16_t;
30typedef unsigned long int uint32_t;
31
32typedef long int intptr_t;
33typedef unsigned long int uintptr_t;
34#endif
35
36/* hton16() and hton32() */
37#if defined HAVE_HTONS
38#   if defined __KERNEL__
39        /* Nothing to do */
40#   elif defined HAVE_ARPA_INET_H
41#       include <arpa/inet.h>
42#   elif defined HAVE_NETINET_IN_H
43#       include <netinet/in.h>
44#   endif
45#   define hton16 htons
46#   define hton32 htonl
47#else
48#   if defined HAVE_ENDIAN_H
49#       include <endian.h>
50#   endif
51static inline uint16_t hton16(uint16_t x)
52{
53    /* This is compile-time optimised with at least -O1 or -Os */
54#if defined HAVE_ENDIAN_H
55    if(__BYTE_ORDER == __BIG_ENDIAN)
56#else
57    uint32_t const dummy = 0x12345678;
58    if(*(uint8_t const *)&dummy == 0x12)
59#endif
60        return x;
61    else
62        return (x >> 8) | (x << 8);
63}
64
65static inline uint32_t hton32(uint32_t x)
66{
67    /* This is compile-time optimised with at least -O1 or -Os */
68#if defined HAVE_ENDIAN_H
69    if(__BYTE_ORDER == __BIG_ENDIAN)
70#else
71    uint32_t const dummy = 0x12345678;
72    if(*(uint8_t const *)&dummy == 0x12)
73#endif
74        return x;
75    else
76        return (x >> 24) | ((x >> 8) & 0x0000ff00)
77                | ((x << 8) & 0x00ff0000) | (x << 24);
78}
79#endif
80
Note: See TracBrowser for help on using the repository browser.