Beispiel #1
0
uint16_t Sound::swapends(uint16_t u)
{
    if (!am_big_endian()) return u;
    uint16_t v;
    swab(&u, &v, 2);
    return v;
}
/* pass in something at least 20 bytes long */
void ip_from_bytes(uint32_t addr, char *buf, int nbo){
    unsigned char *caddr = (unsigned char*) &addr;
    if (am_big_endian() || nbo){
        sprintf(buf,"%d.%d.%d.%d",caddr[0],caddr[1],caddr[2],caddr[3]);
    } else {
        sprintf(buf,"%d.%d.%d.%d",caddr[3],caddr[2],caddr[1],caddr[0]);
    }
    return;
}
Beispiel #3
0
uint32_t Sound::swapends(uint32_t u)
{
    if (!am_big_endian()) return u;
    unsigned char byte[4];
    byte[0] = (u << 24) >> 24;
    byte[1] = (u << 16) >> 24;
    byte[2] = (u << 8) >> 24;
    byte[3] = (u << 0) >> 24;
    return (byte[0]<<24)+(byte[1]<<16)+(byte[2]<<8)+byte[3];
}
static uint32_t be32_to_cpu (uint32_t cpu)
{
  int i;
  uint32_t result;

  if (am_big_endian ())
    return cpu;

  for (i = 0; i < sizeof(uint32_t); i++)
    ((char *)&result)[i] = ((char *)&cpu)[sizeof(uint32_t) - i - 1];

  return result;
}
Beispiel #5
0
ALuint Sound::dataToBuffer(char* data, BasicWAVEHeader header)
{
    int errno;
    ALuint buffer = 0;

    ALuint format = 0;
    switch (header.bitsPerSample){
      case 8:
        format = (header.channels == 1) ? AL_FORMAT_MONO8 : AL_FORMAT_STEREO8;
        break;
      case 16:
        format = (header.channels == 1) ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16;
        break;
      default:
        return 0;
    }

    alGetError();
    alGenBuffers(1, &buffer);
    if ((errno = alGetError()) != AL_NO_ERROR)
    {
        DisplayALError("alGenBuffers: ", errno);
        return 0;
    }
    if (am_big_endian())
    {
        char *b = (char*) malloc(header.dataSize);
        for (unsigned int i=0; i < header.dataSize; i+=2) {
            swab(data+i, b+i, 2);
        }
        memcpy(data, b, header.dataSize);
        free(b);
    }
    alBufferData(buffer, format, data, header.dataSize, header.samplesPerSec);
    return buffer;
}
static float *read_flt_file(
    FILE *in_flt_file, int nrows, int ncols,
    float nodata, int big_endian, int skipbytes, int rowpad,
    int *has_nulls, int *all_ints )
{
    union {
        float f;
        char c[4];
    } pun;

    float *data;
    float *ptr;
    int i, j;
    int count;
    int error;
    char c;
    int reverse_bytes = ( am_big_endian() != big_endian );
    char temp;
    
    *has_nulls = 0;
    *all_ints  = 1;

    // Read data from .flt file:

    data = (float *)malloc( (LONG)nrows * (LONG)ncols * sizeof( float ) );

    if (!data) {
        error_exit( "Insufficient memory for input .flt data." );
    }

    error = fseek( in_flt_file, skipbytes, SEEK_CUR );
    if (error) {
        error_exit( "Read error occurred on input .flt file." );
    }

    for (i=0, ptr=data; i<nrows; ++i, ptr+=ncols) {
        count = fread( ptr, sizeof( float ), ncols, in_flt_file );
        if (count < ncols) {
            if (feof( in_flt_file )) {
                error_exit( "Input .flt file size too small - does not match .hdr info." );
            } else {
                error_exit( "Read error occurred on input .flt file." );
            }
        }
        
        if (reverse_bytes) {
            for (j=0; j<ncols; ++j) {
                pun.f = ptr[j];
                temp = pun.c[0];
                pun.c[0] = pun.c[3];
                pun.c[3] = temp;
                temp = pun.c[1];
                pun.c[1] = pun.c[2];
                pun.c[2] = temp;
                ptr[j] = pun.f;
            }
        }

        for (j=0; j<ncols; ++j) {
            if (flt_isnan( ptr[j] )) {
                prefix_error();
                fprintf( stderr, "Input .flt file contains NaNs - probably bad data" );
                fprintf( stderr, "(or wrong .hdr file).\n" );
                exit( EXIT_FAILURE );
            }
            if (ptr[j] == nodata || ptr[j] < -1.0e+38) {
                ptr[j] = 0.0;
                *has_nulls = 1;
            } else if (*all_ints && ptr[j] != floor( ptr[j] )) {
                *all_ints = 0;
            }
        }

        error = fseek( in_flt_file, rowpad, SEEK_CUR );
        if (error) {
            error_exit( "Read error occurred on input .flt file." );
        }
    }
    
    fread( &c, 1, 1, in_flt_file );
    if (!feof( in_flt_file )) {
        fprintf( stderr, "*** WARNING: " );
        fprintf( stderr, "Input .flt file size too large - does not match .hdr info.\n" );
    }
    
    return data;
}
Beispiel #7
0
int main()
{
  printf("Test Endianness: this system is %s endian.\n", (am_big_endian() ? "BIG" : "LITTLE"));
  printf("htons(1) returns %d.\n", htons(1));
  return 0;
}
static void write_hdr_file(
    FILE *out_hdr_file, int nrows, int ncols,
    double xmin, double xmax, double ymin, double ymax,
    float nodata, float min_value, float max_value,
    int flt_data_type )
// If flt_data_type = 0, writes header for file of 16-bit unsigned ints in BSQ format;
// nodata, min_value, and max_value are assumed to be integers in the range 0 to 65535.
// If flt_data_type = 1, writes header for file of 32-fit floats in BIL format.
{
    // Write .hdr file:
    
    double xdim = (xmax - xmin) / (double)ncols;
    double ydim = (ymax - ymin) / (double)nrows;
    
    int error = 0;
    
    int nbits;
    const char *layout;
    const char *pixeltype;

    if (flt_data_type) {
        nbits = 32;
        layout = "BIL";
        pixeltype = "FLOAT";
    } else {
        nbits = 16;
        layout = "BSQ";
        pixeltype = "UNSIGNEDINT";
    }

    error = error || 0 > fprintf( out_hdr_file, "%-13s %d\r\n", "ncols", ncols );
    error = error || 0 > fprintf( out_hdr_file, "%-13s %d\r\n", "nrows", nrows );
    error = error || 0 > fprintf( out_hdr_file, "%-13s %.14g\r\n", "xllcorner", xmin );
    error = error || 0 > fprintf( out_hdr_file, "%-13s %.14g\r\n", "yllcorner", ymin );
    if  (fabs( (xmax - xmin) / ydim - ncols ) < 0.25 &&
         fabs( (ymax - ymin) / xdim - nrows ) < 0.25)
    {
        // xdim == ydim
        double cellsize = 2.0 * xdim * ydim / (xdim + ydim);    // harmonic mean
        error = error || 0 > fprintf( out_hdr_file, "%-13s %.14g\r\n", "cellsize", cellsize );
    } else {
        // xdim != ydim
        // warning message here?
        error = error || 0 > fprintf( out_hdr_file, "%-13s %.14g\r\n", "xdim", xdim );
        error = error || 0 > fprintf( out_hdr_file, "%-13s %.14g\r\n", "ydim", ydim );
    }
    error = error || 0 > fprintf( out_hdr_file, "%-13s %.6g\r\n", "NODATA_value", nodata );
    if (am_big_endian()) {
        error = error || 0 > fprintf( out_hdr_file, "%-13s %s\r\n", "byteorder", "MSBFIRST" );
    } else {
        error = error || 0 > fprintf( out_hdr_file, "%-13s %s\r\n", "byteorder", "LSBFIRST" );
    }

    error = error || 0 > fprintf( out_hdr_file, "%-13s %s\r\n", "layout", layout );
    error = error || 0 > fprintf( out_hdr_file, "%-13s %d\r\n", "nbands", 1 );
    error = error || 0 > fprintf( out_hdr_file, "%-13s %d\r\n", "nbits", nbits );
    error = error || 0 > fprintf( out_hdr_file, "%-13s %s\r\n", "pixeltype", pixeltype );

    if (flt_data_type) {
        // warning here if these both small relative to precision printed?
        error = error || 0 > fprintf( out_hdr_file, "%-13s %.1f\r\n", "min_value", min_value );
        error = error || 0 > fprintf( out_hdr_file, "%-13s %.1f\r\n", "max_value", max_value );
    } else {
        // write min_value and max_value as integers
        error = error || 0 > fprintf( out_hdr_file, "%-13s %.0f\r\n", "min_value", min_value );
        error = error || 0 > fprintf( out_hdr_file, "%-13s %.0f\r\n", "max_value", max_value );
    }
    
    error = error || fflush( out_hdr_file );
    
    if (error) {
        error_exit( "Write error occurred on output .hdr file." );
    }
}