]> de.git.xonotic.org Git - xonotic/netradiant.git/blobdiff - plugins/image/bmp.cpp
Revert partially (auto) "reformat code! now the code is only ugly on the *inside*"
[xonotic/netradiant.git] / plugins / image / bmp.cpp
index 24f05dda2a719de77518534d6e1508fb5f511e75..63ce4a053ebc5896b7dda02d17b8bc55b4b689b0 100644 (file)
-/*\r
-Copyright (C) 1999-2007 id Software, Inc. and contributors.\r
-For a list of contributors, see the accompanying CONTRIBUTORS file.\r
-\r
-This file is part of GtkRadiant.\r
-\r
-GtkRadiant is free software; you can redistribute it and/or modify\r
-it under the terms of the GNU General Public License as published by\r
-the Free Software Foundation; either version 2 of the License, or\r
-(at your option) any later version.\r
-\r
-GtkRadiant is distributed in the hope that it will be useful,\r
-but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
-GNU General Public License for more details.\r
-\r
-You should have received a copy of the GNU General Public License\r
-along with GtkRadiant; if not, write to the Free Software\r
-Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA\r
-*/\r
-\r
-#include <stdio.h>\r
-#include <string.h>\r
-#include <glib.h>\r
-#include "bmp.h"\r
-\r
-#include "image.h"\r
-\r
-static void BMPLineNone(FILE *f, char *sline, int pixbytes, int width)\r
-{\r
-    int nbytes, i, k, j;\r
-\r
-       switch (pixbytes)\r
-       {\r
-           case 1 :\r
-            nbytes = (width + 3) / 4;\r
-            nbytes *= 4;\r
-\r
-            fread(sline, width, 1, f);\r
-            nbytes -= width;\r
-\r
-            while (nbytes-- > 0) fgetc(f);\r
-                       return;\r
-\r
-               case 3 :\r
-                       nbytes = ((width * 3) + 3) / 4;\r
-                       nbytes *= 4;\r
-\r
-                       fread(sline, width, 3, f);\r
-                       nbytes -= width * 3;\r
-\r
-                       while (nbytes-- > 0) fgetc(f);\r
-\r
-                       // reorder bgr to rgb\r
-                       for (i = 0, j = 0; i < width; i++, j += 3)\r
-                       {\r
-                               k = sline[j];\r
-                               sline[j] = sline[j+2];\r
-                               sline[j+2] = k;\r
-                       }\r
-\r
-                       return;\r
-       }\r
-\r
-       Error("BMPLineNone failed.");\r
-}\r
-\r
-\r
-static void BMPLineRLE8(FILE *f, char *sline, int pixbytes, int width)\r
-{\r
-    Error("RLE8 not yet supported.");\r
-}\r
-\r
-\r
-static void BMPLineRLE4(FILE *f, char *sline, int pixbytes, int width)\r
-{\r
-    Error("RLE4 not yet supported.");\r
-}\r
-\r
-\r
-static void BMPLine(FILE *f, char *scanline, int pixbytes, int width, int rle)\r
-{\r
-    switch (rle)\r
-    {\r
-           case xBI_NONE : BMPLineNone(f, scanline, pixbytes, width); return;\r
-           case xBI_RLE8 : BMPLineRLE8(f, scanline, pixbytes, width); return;\r
-           case xBI_RLE4 : BMPLineRLE4(f, scanline, pixbytes, width); return;\r
-    }\r
-\r
-    Error("Unknown compression type.");\r
-}\r
-\r
-\r
-/*\r
-static void PrintHeader(binfo_t *b)\r
-{\r
-    printf("biSize         : %ld\n", b->biSize);\r
-    printf("biWidth        : %ld\n", b->biWidth);\r
-    printf("biHeight       : %ld\n", b->biHeight);\r
-    printf("biPlanes       : %d\n", b->biPlanes);\r
-    printf("biBitCount     : %d\n", b->biBitCount);\r
-    printf("biCompression  : %ld\n", b->biCompression);\r
-    printf("biSizeImage    : %ld\n", b->biSizeImage);\r
-    printf("biXPelsPerMeter: %ld\n", b->biXPelsPerMeter);\r
-    printf("biYPelsPerMeter: %ld\n", b->biYPelsPerMeter);\r
-    printf("biClrUsed      : %ld\n", b->biClrUsed);\r
-    printf("biClrImportant : %ld\n", b->biClrImportant);\r
-}\r
-*/\r
-\r
-// FIXME: calls to Error(const char *, ... ) are dependant on qe3.cpp\r
-void LoadBMP(char *filename, bitmap_t *bit)\r
-{\r
-    FILE    *f;\r
-    bmphd_t  bhd;\r
-    binfo_t  info;\r
-    //    int      pxlsize = 1;\r
-    int      rowbytes, i, pixbytes;\r
-    char    *scanline;\r
-\r
-    // open file\r
-    if ((f = fopen(filename, "rb")) == NULL)\r
-    {\r
-           Error("Unable to open file");// %s.", filename);\r
-    }\r
-\r
-    // read in bitmap header\r
-    if (fread(&bhd, sizeof(bhd), 1, f) != 1)\r
-    {\r
-               fclose(f);\r
-           Error("Unable to read in bitmap header.");\r
-    }\r
-\r
-    // make sure we have a valid bitmap file\r
-    if (bhd.bfType != BMP_SIGNATURE_WORD)\r
-    {\r
-               fclose(f);\r
-           Error("Invalid BMP file");//: %s", filename);\r
-    }\r
-\r
-    // load in info header\r
-    if (fread(&info, sizeof(info), 1, f) != 1)\r
-    {\r
-               fclose(f);\r
-           Error("Unable to read bitmap info header.");\r
-    }\r
-\r
-    // make sure this is an info type of bitmap\r
-    if (info.biSize != sizeof(binfo_t))\r
-    {\r
-               fclose(f);\r
-           Error("We only support the info bitmap type.");\r
-    }\r
-\r
-    // PrintHeader(&info);\r
-\r
-       bit->bpp      = info.biBitCount;\r
-       bit->width    = info.biWidth;\r
-    bit->height   = info.biHeight;\r
-    bit->data     = NULL;\r
-    bit->palette  = NULL;\r
-\r
-    //currently we only read in 8 and 24 bit bmp files\r
-       if      (info.biBitCount == 8)  pixbytes = 1;\r
-       else if (info.biBitCount == 24) pixbytes = 3;\r
-       else\r
-    {\r
-      Error("Only 8BPP and 24BPP supported");\r
-               //Error("BPP %d not supported.", info.biBitCount);\r
-    }\r
-\r
-    // if this is an eight bit image load palette\r
-       if (pixbytes == 1)\r
-    {\r
-           drgb_t q;\r
-\r
-           bit->palette = reinterpret_cast<rgb_t*>(g_malloc(sizeof(rgb_t) * 256));\r
-\r
-           for (i = 0; i < 256; i++)\r
-           {\r
-               if (fread(&q, sizeof(drgb_t), 1, f) != 1)\r
-               {\r
-                               fclose(f); g_free(bit->palette);\r
-                       Error("Unable to read palette.");\r
-                       }\r
-\r
-               bit->palette[i].r   = q.red;\r
-               bit->palette[i].g   = q.green;\r
-               bit->palette[i].b   = q.blue;\r
-               }\r
-    }\r
-\r
-    // position to start of bitmap\r
-    fseek(f, bhd.bfOffBits, SEEK_SET);\r
-\r
-    // create scanline to read data into\r
-    rowbytes = ((info.biWidth * pixbytes) + 3) / 4;\r
-    rowbytes *= 4;\r
-\r
-    scanline = reinterpret_cast<char*>(g_malloc(rowbytes));\r
-\r
-    // alloc space for new bitmap\r
-    bit->data = reinterpret_cast<unsigned char*>(g_malloc(info.biWidth * pixbytes * info.biHeight));\r
-\r
-    // read in image\r
-    for (i = 0; i < info.biHeight; i++)\r
-    {\r
-           BMPLine(f, scanline, pixbytes, info.biWidth, info.biCompression);\r
-\r
-           // store line\r
-           memcpy(&bit->data[info.biWidth * pixbytes * (info.biHeight - i - 1)], scanline, info.biWidth * pixbytes);\r
-    }\r
-\r
-    g_free(scanline);\r
-    fclose(f);\r
-}\r
-\r
-\r
-\r
-static void BMPEncodeLine(FILE *f, unsigned char *data, int npxls, int pixbytes)\r
-{\r
-    int nbytes, i, j, k;\r
-\r
-       switch (pixbytes)\r
-       {\r
-           case 1 :\r
-            nbytes = (npxls + 3) / 4;\r
-            nbytes *= 4;\r
-\r
-            fwrite(data, npxls, 1, f);\r
-            nbytes -= npxls;\r
-\r
-            while (nbytes-- > 0) fputc(0, f);\r
-                       return;\r
-\r
-        case 3 :\r
-                       // reorder rgb to bgr\r
-                       for (i = 0, j = 0; i < npxls; i++, j+= 3)\r
-                       {\r
-                               k = data[j];\r
-                               data[j] = data[j + 2];\r
-                               data[j + 2] = k;\r
-                       }\r
-\r
-                       nbytes = ((npxls * 3) + 3) / 4;\r
-                       nbytes *= 4;\r
-\r
-                       fwrite(data, npxls, 3, f);\r
-                       nbytes -= npxls * 3;\r
-\r
-                       while (nbytes-- > 0) fputc(0, f);\r
-                       return;\r
-       }\r
-\r
-       Error("BMPEncodeLine Failed.");\r
-}\r
-\r
-\r
-\r
-void WriteBMP(char *filename, bitmap_t *bit)\r
-{\r
-    FILE    *f;\r
-    bmphd_t  header;\r
-    binfo_t  info;\r
-    drgb_t   q;        // palette that gets written\r
-    long     bmofs;\r
-    int      w, h, i;\r
-       int      pixbytes;\r
-\r
-    if      (bit->bpp == 8)  pixbytes = 1;\r
-       else if (bit->bpp == 24) pixbytes = 3;\r
-\r
-       else\r
-    {\r
-    Error("Only 8BPP and 24BPP supported");\r
-               //Error("BPP %d not supported.", bit->bpp);\r
-    }\r
-\r
-\r
-    if ((f = fopen(filename, "wb")) == NULL)\r
-    {\r
-           Error("Unable to open file");//%s.", filename);\r
-    }\r
-\r
-    // write out an empty header as a place holder\r
-    if (fwrite(&header, sizeof(header), 1, f) != 1)\r
-    {\r
-           Error("Unable to fwrite.");\r
-    }\r
-\r
-    // init and write info header\r
-    info.biSize          = sizeof(binfo_t);\r
-    info.biWidth         = bit->width;\r
-    info.biHeight        = bit->height;\r
-    info.biPlanes        = 1;\r
-    info.biBitCount      = bit->bpp;\r
-    info.biCompression   = xBI_NONE;\r
-    info.biSizeImage     = bit->width * bit->height;\r
-    info.biXPelsPerMeter = 0;\r
-    info.biYPelsPerMeter = 0;\r
-    info.biClrUsed       = 256;\r
-    info.biClrImportant  = 256;\r
-\r
-    if (fwrite(&info, sizeof(binfo_t), 1, f) != 1)\r
-    {\r
-           Error("fwrite failed.");\r
-    }\r
-\r
-    // write out palette if we need to\r
-       if (bit->bpp == 8)\r
-       {\r
-        for (i = 0; i < 256; i++)\r
-        {\r
-               q.red   = bit->palette[i].r;\r
-               q.green = bit->palette[i].g;\r
-               q.blue  = bit->palette[i].b;\r
-\r
-               fwrite(&q, sizeof(q), 1, f);\r
-               }\r
-    }\r
-\r
-    // save offset to start of bitmap\r
-    bmofs = ftell(f);\r
-\r
-    // output bitmap\r
-    w = bit->width;\r
-    h = bit->height;\r
-\r
-    for (i = h - 1; i >= 0; i--)\r
-    {\r
-           BMPEncodeLine(f, &bit->data[w * pixbytes * i], w, pixbytes);\r
-    }\r
-\r
-    // update and rewrite file header\r
-    header.bfType    = BMP_SIGNATURE_WORD;\r
-    header.bfSize    = ftell(f);\r
-    header.bfOffBits = bmofs;\r
-\r
-    fseek(f, 0L, SEEK_SET);\r
-    fwrite(&header, sizeof(header), 1, f);\r
-\r
-    fclose(f);\r
-}\r
-\r
-\r
-void NewBMP(int width, int height, int bpp, bitmap_t *bit)\r
-{\r
-       int pixbytes;\r
-\r
-       if      (bpp == 8)  pixbytes = 1;\r
-       else if (bpp == 24) pixbytes = 3;\r
-\r
-       else\r
-       {\r
-               Error("NewBMP: 8 or 24 bit only.");\r
-       }\r
-\r
-       bit->bpp    = bpp;\r
-       bit->width  = width;\r
-       bit->height = height;\r
-\r
-       bit->data = reinterpret_cast<unsigned char*>(g_malloc(width * height * pixbytes));\r
-\r
-       if (bit->data == NULL)\r
-       {\r
-               Error("NewBMP: g_malloc failed.");\r
-       }\r
-\r
-       // see if we need to create a palette\r
-       if (pixbytes == 1)\r
-       {\r
-               bit->palette = (rgb_t *) g_malloc(768);\r
-\r
-               if (bit->palette == NULL)\r
-               {\r
-                       Error("NewBMP: unable to g_malloc palette.");\r
-               }\r
-       }\r
-       else\r
-       {\r
-               bit->palette = NULL;\r
-       }\r
-}\r
-\r
-\r
-\r
-void FreeBMP(bitmap_t *bitmap)\r
-{\r
-    if (bitmap->palette)\r
-    {\r
-           g_free(bitmap->palette);\r
-           bitmap->palette = NULL;\r
-    }\r
-\r
-    if (bitmap->data)\r
-    {\r
-           g_free(bitmap->data);\r
-           bitmap->data = NULL;\r
-    }\r
-}\r
-\r
-\r
+/*
+   Copyright (C) 2001-2006, William Joseph.
+   All Rights Reserved.
+
+   This file is part of GtkRadiant.
+
+   GtkRadiant is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2 of the License, or
+   (at your option) any later version.
+
+   GtkRadiant is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with GtkRadiant; if not, write to the Free Software
+   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#include "bmp.h"
+
+#include "ifilesystem.h"
+
+typedef unsigned char byte;
+
+#include "imagelib.h"
+#include "bytestreamutils.h"
+
+
+typedef unsigned char PaletteEntry[4];
+typedef struct
+{
+       char id[2];
+       unsigned long fileSize;
+       unsigned long reserved0;
+       unsigned long bitmapDataOffset;
+       unsigned long bitmapHeaderSize;
+       unsigned long width;
+       unsigned long height;
+       unsigned short planes;
+       unsigned short bitsPerPixel;
+       unsigned long compression;
+       unsigned long bitmapDataSize;
+       unsigned long hRes;
+       unsigned long vRes;
+       unsigned long colors;
+       unsigned long importantColors;
+       PaletteEntry palette[256];
+} BMPHeader_t;
+
+class ReadPixel8
+{
+PaletteEntry* m_palette;
+public:
+ReadPixel8( PaletteEntry* palette ) : m_palette( palette ){
+}
+void operator()( PointerInputStream& inputStream, byte*& pixbuf ) const {
+       byte palIndex;
+       inputStream.read( &palIndex, 1 );
+       *pixbuf++ = m_palette[palIndex][2];
+       *pixbuf++ = m_palette[palIndex][1];
+       *pixbuf++ = m_palette[palIndex][0];
+       *pixbuf++ = 0xff;
+}
+};
+
+class ReadPixel16
+{
+public:
+void operator()( PointerInputStream& inputStream, byte*& pixbuf ) const {
+       unsigned short shortPixel;
+       inputStream.read( reinterpret_cast<byte*>( &shortPixel ), sizeof( unsigned short ) ); //!\todo Is this endian safe?
+       *pixbuf++ = static_cast<byte>( shortPixel & ( 31 << 10 ) ) >> 7;
+       *pixbuf++ = static_cast<byte>( shortPixel & ( 31 << 5 ) ) >> 2;
+       *pixbuf++ = static_cast<byte>( shortPixel & ( 31 ) ) << 3;
+       *pixbuf++ = 0xff;
+}
+};
+
+class ReadPixel24
+{
+public:
+void operator()( PointerInputStream& inputStream, byte*& pixbuf ) const {
+       byte bgr[3];
+       inputStream.read( bgr, 3 );
+       *pixbuf++ = bgr[2];
+       *pixbuf++ = bgr[1];
+       *pixbuf++ = bgr[0];
+       *pixbuf++ = 255;
+}
+};
+
+class ReadPixel32
+{
+public:
+void operator()( PointerInputStream& inputStream, byte*& pixbuf ) const {
+       byte bgra[4];
+       inputStream.read( bgra, 4 );
+       *pixbuf++ = bgra[2];
+       *pixbuf++ = bgra[1];
+       *pixbuf++ = bgra[0];
+       *pixbuf++ = bgra[3];
+}
+};
+
+template<typename ReadPixel>
+void ReadBMP( PointerInputStream& inputStream, byte* bmpRGBA, int rows, int columns, ReadPixel readPixel ){
+       for ( int row = rows - 1; row >= 0; row-- )
+       {
+               byte* pixbuf = bmpRGBA + row * columns * 4;
+
+               for ( int column = 0; column < columns; column++ )
+               {
+                       readPixel( inputStream, pixbuf );
+               }
+       }
+}
+
+Image* LoadBMPBuff( PointerInputStream& inputStream, std::size_t length ){
+       BMPHeader_t bmpHeader;
+       inputStream.read( reinterpret_cast<byte*>( bmpHeader.id ), 2 );
+       bmpHeader.fileSize = istream_read_uint32_le( inputStream );
+       bmpHeader.reserved0 = istream_read_uint32_le( inputStream );
+       bmpHeader.bitmapDataOffset = istream_read_uint32_le( inputStream );
+       bmpHeader.bitmapHeaderSize = istream_read_uint32_le( inputStream );
+       bmpHeader.width = istream_read_uint32_le( inputStream );
+       bmpHeader.height = istream_read_uint32_le( inputStream );
+       bmpHeader.planes = istream_read_uint16_le( inputStream );
+       bmpHeader.bitsPerPixel = istream_read_uint16_le( inputStream );
+       bmpHeader.compression = istream_read_uint32_le( inputStream );
+       bmpHeader.bitmapDataSize = istream_read_uint32_le( inputStream );
+       bmpHeader.hRes = istream_read_uint32_le( inputStream );
+       bmpHeader.vRes = istream_read_uint32_le( inputStream );
+       bmpHeader.colors = istream_read_uint32_le( inputStream );
+       bmpHeader.importantColors = istream_read_uint32_le( inputStream );
+
+       if ( bmpHeader.bitsPerPixel == 8 ) {
+               int paletteSize = bmpHeader.colors * 4;
+               inputStream.read( reinterpret_cast<byte*>( bmpHeader.palette ), paletteSize );
+       }
+
+       if ( bmpHeader.id[0] != 'B' && bmpHeader.id[1] != 'M' ) {
+               globalErrorStream() << "LoadBMP: only Windows-style BMP files supported\n";
+               return 0;
+       }
+       if ( bmpHeader.fileSize != length ) {
+               globalErrorStream() << "LoadBMP: header size does not match file size (" << Unsigned( bmpHeader.fileSize ) << " vs. " << Unsigned( length ) << ")\n";
+               return 0;
+       }
+       if ( bmpHeader.compression != 0 ) {
+               globalErrorStream() << "LoadBMP: only uncompressed BMP files supported\n";
+               return 0;
+       }
+       if ( bmpHeader.bitsPerPixel < 8 ) {
+               globalErrorStream() << "LoadBMP: monochrome and 4-bit BMP files not supported\n";
+               return 0;
+       }
+
+       int columns = bmpHeader.width;
+       int rows = bmpHeader.height;
+       if ( rows < 0 ) {
+               rows = -rows;
+       }
+
+       RGBAImage* image = new RGBAImage( columns, rows );
+
+       switch ( bmpHeader.bitsPerPixel )
+       {
+       case 8:
+               ReadBMP( inputStream, image->getRGBAPixels(), rows, columns, ReadPixel8( bmpHeader.palette ) );
+               break;
+       case 16:
+               ReadBMP( inputStream, image->getRGBAPixels(), rows, columns, ReadPixel16() );
+               break;
+       case 24:
+               ReadBMP( inputStream, image->getRGBAPixels(), rows, columns, ReadPixel24() );
+               break;
+       case 32:
+               ReadBMP( inputStream, image->getRGBAPixels(), rows, columns, ReadPixel32() );
+               break;
+       default:
+               globalErrorStream() << "LoadBMP: illegal pixel_size '" << bmpHeader.bitsPerPixel << "'\n";
+               image->release();
+               return 0;
+       }
+       return image;
+}
+
+Image* LoadBMP( ArchiveFile& file ){
+       ScopedArchiveBuffer buffer( file );
+       PointerInputStream inputStream( buffer.buffer );
+       return LoadBMPBuff( inputStream, buffer.length );
+}