00001
00020 #include "mfcpch.h"
00021 #ifdef __MSW32__
00022 #include <io.h>
00023 #else
00024 #include <unistd.h>
00025 #endif
00026 #include <stdio.h>
00027 #include "img.h"
00028 #include "imgbmp.h"
00029
00034 typedef struct
00035 {
00036 char bfType1;
00037 char bfType2;
00038 } BMPHEADER0;
00039
00044 typedef struct
00045 {
00046 UINT32 bfSize;
00047 UINT16 bfReserved1;
00048 UINT16 bfReserved2;
00049 UINT32 bfOffBits;
00050 } BMPHEADER;
00051
00056 typedef struct
00057 {
00058 UINT32 biSize;
00059 INT32 biWidth;
00060 INT32 biHeight;
00061 UINT16 biPlanes;
00062 UINT16 biBitCount;
00063 UINT32 biCompression;
00064 UINT32 biSizeImage;
00065 INT32 biXPelsPerMeter;
00066 INT32 biYPelsPerMeter;
00067 UINT32 biClrUsed;
00068 UINT32 biClrImportant;
00069 } BMPHEADER2;
00070
00075 typedef struct
00076 {
00077 UINT8 rgbBlue;
00078 UINT8 rgbGreen;
00079 UINT8 rgbRed;
00080 UINT8 rgbReserved;
00081 } WIN32_RGBQUAD;
00082
00086 INT8 open_bmp_image(
00087 int fd,
00088 INT32 *xsize,
00089 INT32 *ysize,
00090 INT8 *bpp,
00091 INT8 *photo,
00092 INT32 *res
00093 ) {
00094 UINT32 nread;
00095 BMPHEADER0 head0;
00096 BMPHEADER head1;
00097 BMPHEADER2 head2;
00098
00099 *photo = 1;
00100 nread = read (fd, &head0, sizeof (head0));
00101 if (nread != sizeof (head0))
00102 return -1;
00103 nread = read (fd, &head1, sizeof (head1));
00104 if (nread != sizeof (head1))
00105 return -1;
00106 nread = read (fd, &head2, sizeof (head2));
00107 if (nread != sizeof (head2))
00108 return -1;
00109
00110 if (head0.bfType1 != 'B')
00111 return -1;
00112 if (head0.bfType2 != 'M')
00113 return -1;
00114 lseek (fd, head1.bfOffBits, SEEK_SET);
00115 *bpp = head2.biBitCount;
00116 *xsize = head2.biWidth;
00117 *ysize = head2.biHeight;
00118 *res = 300;
00119 return -2;
00120 }
00121
00122
00126 INT8 read_bmp_image(
00127 int fd,
00128 UINT8 *pixels,
00129 INT32 xsize,
00130 INT32 ysize,
00131 INT8 bpp,
00132 INT32
00133 ) {
00134 UINT32 bpl;
00135 UINT32 wpl;
00136 UINT32 nread;
00137 INT32 index;
00138
00139 bpl = (xsize * bpp + 7) / 8;
00140 wpl = (bpl + 3) / 4;
00141 wpl *= 4;
00142 for (index = 0; index < ysize; index++) {
00143 nread = read (fd, pixels + bpl * (ysize - 1 - index), bpl);
00144 if (nread != bpl)
00145 return -1;
00146 if (wpl != bpl)
00147 lseek (fd, wpl - bpl, SEEK_CUR);
00148 }
00149 return 0;
00150 }
00151
00152
00156 INT8 write_bmp_image(
00157 int fd,
00158 UINT8 *pixels,
00159 INT32 xsize,
00160 INT32 ysize,
00161 INT8 bpp,
00162 INT8,
00163 INT32 res
00164 ) {
00165 UINT32 bpl;
00166 UINT32 wpl;
00167 UINT32 nread;
00168 INT32 cols;
00169 INT32 index;
00170 BMPHEADER0 head0;
00171 BMPHEADER head1;
00172 BMPHEADER2 head2;
00173 WIN32_RGBQUAD coltab[256];
00174
00175 if (bpp == 24)
00176 cols = 0;
00177 else
00178 cols = 1 << bpp;
00179 bpl = (xsize * bpp + 7) / 8;
00180 wpl = (bpl + 3) / 4;
00181
00182 head2.biSize = sizeof (head2);
00183 head2.biWidth = xsize;
00184 head2.biHeight = ysize;
00185 head2.biPlanes = 1;
00186 head2.biBitCount = bpp;
00187 head2.biCompression = 0;
00188
00189 head2.biSizeImage = wpl * 4 * ysize;
00190
00191 head2.biXPelsPerMeter = (UINT32) (res * 39.37);
00192 head2.biYPelsPerMeter = (UINT32) (res * 39.37);
00193 head2.biClrUsed = cols;
00194 head2.biClrImportant = 0;
00195
00196 head0.bfType1 = 'B';
00197 head0.bfType2 = 'M';
00198 head1.bfReserved1 = 0;
00199 head1.bfReserved2 = 0;
00200
00201 head1.bfOffBits = sizeof (head0) + sizeof (head1) +
00202 sizeof (head2) + sizeof (WIN32_RGBQUAD) * cols;
00203
00204 head1.bfSize = head1.bfOffBits + head2.biSizeImage;
00205
00206 for (index = 0; index < cols; index++) {
00207 coltab[index].rgbBlue = index * 255 / (cols - 1);
00208 coltab[index].rgbGreen = coltab[index].rgbBlue;
00209 coltab[index].rgbRed = coltab[index].rgbBlue;
00210 coltab[index].rgbReserved = 0;
00211 }
00212
00213 nread = write (fd, &head0, sizeof (head0));
00214 if (nread != sizeof (head0))
00215 return -1;
00216 nread = write (fd, &head1, sizeof (head1));
00217 if (nread != sizeof (head1))
00218 return -1;
00219 nread = write (fd, &head2, sizeof (head2));
00220 if (nread != sizeof (head2))
00221 return -1;
00222 nread = write (fd, coltab, cols * sizeof (WIN32_RGBQUAD));
00223 if (nread != cols * sizeof (WIN32_RGBQUAD))
00224 return -1;
00225 for (index = 0; index < ysize; index++) {
00226 nread = write (fd, pixels + bpl * (ysize - 1 - index), wpl * 4);
00227 if (nread != wpl * 4)
00228 return -1;
00229 }
00230 close(fd);
00231 return 0;
00232 }