FLTK 1.4.4: Fl_Image_Reader.h Source File

FLTK 1.4.4

Loading...
Searching...
No Matches
Fl_Image_Reader.h
1//
2// Internal (Image) Reader class for the Fast Light Tool Kit (FLTK).
3//
4// Copyright 2020-2021 by Bill Spitzak and others.
5//
6// This library is free software. Distribution and use rights are outlined in
7// the file "COPYING" which should have been included with this file. If this
8// file is missing or damaged, see the license at:
9//
10// https://www.fltk.org/COPYING.php
11//
12// Please see the following page on how to report bugs and issues:
13//
14// https://www.fltk.org/bugs.php
15//
16
17/*
18 This internal (undocumented) class reads data chunks from a file or from
19 memory in LSB-first byte order.
20
21 This class is used in Fl_GIF_Image and Fl_BMP_Image to avoid code
22 duplication and may be extended to be used in similar cases. Future
23 options might be to read data in MSB-first byte order or to add more
24 methods.
25*/
26
27#ifndef FL_IMAGE_READER_H
28#define FL_IMAGE_READER_H
29
30#include <stdio.h>
31
32 class Fl_Image_Reader {
33public:
34 // Create the reader.
35 Fl_Image_Reader()
36 : is_file_(0)
37 , is_data_(0)
38 , file_(0L)
39 , data_(0L)
40 , start_(0L)
41 , end_((const unsigned char *)(-1L))
42 , name_(0L)
43 , error_(0) {}
44
45 // Initialize the reader to access the file system, filename is copied
46 // and stored.
47 int open(const char *filename);
48
49 // Initialize the reader for memory access, name is copied and stored
50 int open(const char *imagename, const unsigned char *data, const size_t datasize);
51 // Deprecated, DO NOT USE!
52 int open(const char *imagename, const unsigned char *data);
53
54 // Close and destroy the reader
55 ~Fl_Image_Reader();
56
57 // Read a single byte from memory or a file
58 unsigned char read_byte();
59
60 // Read a 16-bit unsigned integer, LSB-first
61 unsigned short read_word();
62
63 // Read a 32-bit unsigned integer, LSB-first
64 unsigned int read_dword();
65
66 // Read a 32-bit signed integer, LSB-first
67 int read_long() { return (int)read_dword(); }
68
69 // Move the current read position to a byte offset from the beginning
70 // of the file or the original start address in memory
71 void seek(unsigned int n);
72
73 // Get the current file or memory offset from the beginning
74 // of the file or the original start address in memory
75 long tell() const;
76
77 // Get the current EOF or error status of the file or data block
78 int error() const { return error_; }
79
80 // return the name or filename for this reader
81 const char *name() const { return name_; }
82
83 // skip a given number of bytes
84 void skip(unsigned int n) { seek((unsigned int)tell() + n); }
85
86private:
87 // open() sets this if we read from a file
88 char is_file_;
89 // open() sets this if we read from memory
90 char is_data_;
91 // a pointer to the opened file
92 FILE *file_;
93 // a pointer to the current byte in memory
94 const unsigned char *data_;
95 // a pointer to the start of the image data
96 const unsigned char *start_;
97 // a pointer to the end of image data if reading from memory, otherwise undefined
98 // note: currently (const unsigned char *)(-1L) if end of memory is not available
99 // ... which means "unlimited"
100 const unsigned char *end_;
101 // a copy of the name associated with this reader
102 char *name_;
103 // a flag to store EOF or error status
104 int error_;
105};
106
107#endif // FL_IMAGE_READER_H