| 1 |
/* lpicture.cxx |
| 2 |
$Id: lpicture.cxx,v 1.4 1998/10/15 04:11:51 elf Exp $ |
| 3 |
|
| 4 |
written by Marc Singer |
| 5 |
3 June 1997 |
| 6 |
|
| 7 |
This file is part of the project XO. See the file README for |
| 8 |
more information. |
| 9 |
|
| 10 |
Copyright (C) 1997 Marc Singer |
| 11 |
|
| 12 |
This program is free software; you can redistribute it and/or |
| 13 |
modify it under the terms of the GNU General Public License as |
| 14 |
published by the Free Software Foundation; either version 2 of the |
| 15 |
License, or (at your option) any later version. |
| 16 |
|
| 17 |
This program is distributed in the hope that it will be useful, but |
| 18 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 20 |
General Public License for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License |
| 23 |
in a file called COPYING along with this program; if not, write to |
| 24 |
the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA |
| 25 |
02139, USA. |
| 26 |
|
| 27 |
----------- |
| 28 |
DESCRIPTION |
| 29 |
----------- |
| 30 |
|
| 31 |
*/ |
| 32 |
|
| 33 |
#include "standard.h" |
| 34 |
#include <memory.h> |
| 35 |
|
| 36 |
#include <unistd.h> |
| 37 |
#include <fcntl.h> |
| 38 |
#include <sys/mman.h> |
| 39 |
#include <sys/stat.h> |
| 40 |
|
| 41 |
#include "lpicture.h" |
| 42 |
|
| 43 |
|
| 44 |
typedef struct { |
| 45 |
// INT16 bfType; //'B' 'M' |
| 46 |
unsigned32 bfSize; |
| 47 |
int16 bfReserved1; |
| 48 |
int16 bfReserved2; |
| 49 |
unsigned32 bfOffBits; |
| 50 |
} HDR_DIBFILE; |
| 51 |
|
| 52 |
typedef struct{ |
| 53 |
unsigned32 biSize; |
| 54 |
unsigned32 biWidth; |
| 55 |
unsigned32 biHeight; |
| 56 |
unsigned16 biPlanes; |
| 57 |
unsigned16 biBitCount; |
| 58 |
unsigned32 biCompression; |
| 59 |
unsigned32 biSizeImage; |
| 60 |
unsigned32 biXPelsPerMeter; |
| 61 |
unsigned32 biYPelsPerMeter; |
| 62 |
unsigned32 biClrUsed; |
| 63 |
unsigned32 biClrImportant; |
| 64 |
} HDR_DIB; |
| 65 |
|
| 66 |
|
| 67 |
bool LPicture::read (const char* szPath) |
| 68 |
{ |
| 69 |
m_fh = open (szPath, O_RDONLY); |
| 70 |
if (m_fh < 0) { |
| 71 |
m_fh = 0; |
| 72 |
return false; |
| 73 |
} |
| 74 |
|
| 75 |
struct stat stat; |
| 76 |
fstat (m_fh, &stat); |
| 77 |
|
| 78 |
if (!(m_pvMap = mmap (NULL, m_cbMap = stat.st_size, |
| 79 |
PROT_READ, MAP_SHARED, m_fh, 0)) |
| 80 |
|| ((char*)m_pvMap)[0] != 'B' |
| 81 |
|| ((char*)m_pvMap)[1] != 'M') { |
| 82 |
release_mmap (); |
| 83 |
return false; |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
HDR_DIBFILE& hdrFile = *(HDR_DIBFILE*)((unsigned8*) m_pvMap + 2); |
| 88 |
HDR_DIB& hdr = *(HDR_DIB*)((unsigned8*) m_pvMap |
| 89 |
+ 2 + sizeof (HDR_DIBFILE)); |
| 90 |
|
| 91 |
m_width = hdr.biWidth; |
| 92 |
m_height = hdr.biHeight; |
| 93 |
m_depth = hdr.biPlanes*hdr.biBitCount; |
| 94 |
_calc_stride (); |
| 95 |
m_fNonNormal = true; |
| 96 |
m_cColors = (hdrFile.bfOffBits - sizeof (hdrFile) - sizeof (hdr)) |
| 97 |
/sizeof (RGBQUAD); |
| 98 |
|
| 99 |
if (m_cColors) |
| 100 |
m_rgRGB = (RGBQUAD*) (&hdr + 1); |
| 101 |
m_pv = (unsigned8*) m_pvMap + hdrFile.bfOffBits; |
| 102 |
m_cb = m_cbStride*m_height; |
| 103 |
|
| 104 |
// This is catastrophic, but we cannot |
| 105 |
// do much about it unless we handle |
| 106 |
// reading very specially. |
| 107 |
// ** FIXME?? |
| 108 |
if ((unsigned8*) m_pv + m_cb > (unsigned8*) m_pvMap + m_cbMap) { |
| 109 |
release_mmap (); |
| 110 |
return false; |
| 111 |
} |
| 112 |
|
| 113 |
return true; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
void LPicture::release_mmap (void) |
| 118 |
{ |
| 119 |
if (!m_pvMap) |
| 120 |
return; |
| 121 |
|
| 122 |
if (m_rgRGB >= m_pvMap |
| 123 |
&& (unsigned8*) m_rgRGB < (unsigned8*) m_pvMap + m_cbMap) |
| 124 |
m_rgRGB = NULL; |
| 125 |
if (m_pv >= m_pvMap && m_pv < (unsigned8*) m_pvMap + m_cbMap) |
| 126 |
m_pv = NULL; |
| 127 |
|
| 128 |
if (m_pvMap) |
| 129 |
munmap ((caddr_t) m_pvMap, m_cbMap); |
| 130 |
m_pvMap = NULL; |
| 131 |
if (m_fh) { |
| 132 |
close (m_fh); |
| 133 |
m_fh = 0; |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
void LPicture::release_this (void) |
| 139 |
{ |
| 140 |
release_mmap (); |
| 141 |
if (m_pv) { |
| 142 |
free (m_pv); |
| 143 |
m_pv = NULL; |
| 144 |
m_cb = 0; |
| 145 |
} |
| 146 |
if (m_rgRGB) { |
| 147 |
free (m_rgRGB); |
| 148 |
m_rgRGB = 0; |
| 149 |
m_cColors = 0; |
| 150 |
} |
| 151 |
} |
| 152 |
|