1 /* bzflag
2  * Copyright (c) 1993-2021 Tim Riker
3  *
4  * This package is free software;  you can redistribute it and/or
5  * modify it under the terms of the license found in the file
6  * named COPYING that should have accompanied this file.
7  *
8  * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
9  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
10  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
11  */
12 
13 #ifndef BZF_MEDIA_FILE_H
14 #define BZF_MEDIA_FILE_H
15 
16 #include "common.h"
17 
18 /* system interface headers */
19 #include <string>
20 
21 /* common interface headers */
22 #include "bzfio.h"
23 
24 
25 // if HALF_RATE_AUDIO defined then use half the normal audio sample
26 // rate (and downsample the audio files to match).  this reduces the
27 // demands on the system.
28 // #define HALF_RATE_AUDIO
29 
30 
31 /** This class is a base class for media files, which can be image files or
32     audio files. */
33 class MediaFile
34 {
35 public:
36     /** Close the media file.  This does *not* destroy the stream. */
37     virtual ~MediaFile();
38 
39     /** Read an image file.  Use delete[] to release the returned
40         image.  Returns NULL on failure.  Images are stored RGBA,
41         left to right, bottom to top. */
42     static unsigned char* readImage(std::string filename,
43                                     int* width, int* height);
44 
45     // read a sound file.  use delete[] to release the returned
46     // audio.  returns NULL on failure.  sounds are stored
47     // left/right.
48     //    static float*       readSound(const std::string& filename,
49     //                            int* numFrames, int* rate);
50 
51 protected:
52     MediaFile(std::istream*);
53 
54     /** Get the stream. */
getStream()55     std::istream* getStream() const
56     {
57         return stream;
58     }
59 
60     /** Return true if the stream is in a readable state. */
61     bool isOkay() const;
62 
63     /** Utility method to read raw data. */
64     void readRaw(void* buffer, uint32_t bytes);
65 
66     /** Utility method to skip data. */
67     void skip(uint32_t bytes);
68 
69     /** Utility method to read a 2 byte little-endian number into host byte
70         order. */
71     uint16_t read16LE();
72     /** Utility method to read a 2 byte big-endian number into host byte
73         order. */
74     uint16_t read16BE();
75     /** Utility method to read a 4 byte little-endian number into host byte
76         order. */
77     uint32_t read32LE();
78     /** Utility method to read a 4 byte big-endian number into host byte
79         order. */
80     uint32_t read32BE();
81 
82     /** Utility method to byte swap a little-endian 2 byte number in place
83         into host byte order. Returns the byte swapped data. */
84     static uint16_t swap16LE(uint16_t*);
85 
86     /** Utility method to byte swap a big-endian 2 byte number in place into
87         host byte order. Returns the byte swapped data. */
88     static uint16_t swap16BE(uint16_t*);
89 
90     /** Utility method to byte swap a little-endian 4 byte number in place
91         into host byte order. Returns the byte swapped data. */
92     static uint32_t swap32LE(uint32_t*);
93 
94     /** Utility method to byte swap a big-endian 4 byte number in place into
95         host byte order. Returns the byte swapped data. */
96     static uint32_t swap32BE(uint32_t*);
97 
98 private:
99     std::istream*         stream;
100 };
101 
102 #endif
103 
104 // Local Variables: ***
105 // mode: C++ ***
106 // tab-width: 4 ***
107 // c-basic-offset: 4 ***
108 // indent-tabs-mode: nil ***
109 // End: ***
110 // ex: shiftwidth=4 tabstop=4
111