2007-11-04 03:51:54 +00:00
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
Copyright (C) 1999-2007 id Software, Inc. and contributors.
|
|
|
|
For a list of contributors, see the accompanying CONTRIBUTORS file.
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
This file is part of GtkRadiant.
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
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.
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
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.
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
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
|
|
|
|
*/
|
2007-11-04 03:51:54 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <glib.h>
|
|
|
|
#include "bmp.h"
|
|
|
|
|
|
|
|
#include "image.h"
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
static void BMPLineNone( FILE *f, char *sline, int pixbytes, int width ){
|
|
|
|
int nbytes, i, k, j;
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
switch ( pixbytes )
|
2007-11-04 03:51:54 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
case 1:
|
|
|
|
nbytes = ( width + 3 ) / 4;
|
|
|
|
nbytes *= 4;
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
fread( sline, width, 1, f );
|
|
|
|
nbytes -= width;
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
while ( nbytes-- > 0 ) fgetc( f );
|
|
|
|
return;
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
case 3:
|
|
|
|
nbytes = ( ( width * 3 ) + 3 ) / 4;
|
|
|
|
nbytes *= 4;
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
fread( sline, width, 3, f );
|
|
|
|
nbytes -= width * 3;
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
while ( nbytes-- > 0 ) fgetc( f );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
// reorder bgr to rgb
|
|
|
|
for ( i = 0, j = 0; i < width; i++, j += 3 )
|
|
|
|
{
|
|
|
|
k = sline[j];
|
|
|
|
sline[j] = sline[j + 2];
|
|
|
|
sline[j + 2] = k;
|
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
return;
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
Error( "BMPLineNone failed." );
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
static void BMPLineRLE8( FILE *f, char *sline, int pixbytes, int width ){
|
|
|
|
Error( "RLE8 not yet supported." );
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
static void BMPLineRLE4( FILE *f, char *sline, int pixbytes, int width ){
|
|
|
|
Error( "RLE4 not yet supported." );
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
static void BMPLine( FILE *f, char *scanline, int pixbytes, int width, int rle ){
|
|
|
|
switch ( rle )
|
|
|
|
{
|
|
|
|
case xBI_NONE: BMPLineNone( f, scanline, pixbytes, width ); return;
|
|
|
|
case xBI_RLE8: BMPLineRLE8( f, scanline, pixbytes, width ); return;
|
|
|
|
case xBI_RLE4: BMPLineRLE4( f, scanline, pixbytes, width ); return;
|
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
Error( "Unknown compression type." );
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
static void PrintHeader(binfo_t *b)
|
|
|
|
{
|
2007-11-04 03:51:54 +00:00
|
|
|
printf("biSize : %ld\n", b->biSize);
|
|
|
|
printf("biWidth : %ld\n", b->biWidth);
|
|
|
|
printf("biHeight : %ld\n", b->biHeight);
|
|
|
|
printf("biPlanes : %d\n", b->biPlanes);
|
|
|
|
printf("biBitCount : %d\n", b->biBitCount);
|
|
|
|
printf("biCompression : %ld\n", b->biCompression);
|
|
|
|
printf("biSizeImage : %ld\n", b->biSizeImage);
|
|
|
|
printf("biXPelsPerMeter: %ld\n", b->biXPelsPerMeter);
|
|
|
|
printf("biYPelsPerMeter: %ld\n", b->biYPelsPerMeter);
|
|
|
|
printf("biClrUsed : %ld\n", b->biClrUsed);
|
|
|
|
printf("biClrImportant : %ld\n", b->biClrImportant);
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
*/
|
2007-11-04 03:51:54 +00:00
|
|
|
|
|
|
|
// FIXME: calls to Error(const char *, ... ) are dependant on qe3.cpp
|
2012-03-17 20:01:54 +00:00
|
|
|
void LoadBMP( char *filename, bitmap_t *bit ){
|
|
|
|
FILE *f;
|
|
|
|
bmphd_t bhd;
|
|
|
|
binfo_t info;
|
|
|
|
// int pxlsize = 1;
|
|
|
|
int rowbytes, i, pixbytes;
|
|
|
|
char *scanline;
|
|
|
|
|
|
|
|
// open file
|
|
|
|
if ( ( f = fopen( filename, "rb" ) ) == NULL ) {
|
|
|
|
Error( "Unable to open file" ); // %s.", filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
// read in bitmap header
|
|
|
|
if ( fread( &bhd, sizeof( bhd ), 1, f ) != 1 ) {
|
|
|
|
fclose( f );
|
|
|
|
Error( "Unable to read in bitmap header." );
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure we have a valid bitmap file
|
|
|
|
if ( bhd.bfType != BMP_SIGNATURE_WORD ) {
|
|
|
|
fclose( f );
|
|
|
|
Error( "Invalid BMP file" ); //: %s", filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
// load in info header
|
|
|
|
if ( fread( &info, sizeof( info ), 1, f ) != 1 ) {
|
|
|
|
fclose( f );
|
|
|
|
Error( "Unable to read bitmap info header." );
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure this is an info type of bitmap
|
|
|
|
if ( info.biSize != sizeof( binfo_t ) ) {
|
|
|
|
fclose( f );
|
|
|
|
Error( "We only support the info bitmap type." );
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrintHeader(&info);
|
2007-11-04 03:51:54 +00:00
|
|
|
|
|
|
|
bit->bpp = info.biBitCount;
|
|
|
|
bit->width = info.biWidth;
|
2012-03-17 20:01:54 +00:00
|
|
|
bit->height = info.biHeight;
|
|
|
|
bit->data = NULL;
|
|
|
|
bit->palette = NULL;
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
//currently we only read in 8 and 24 bit bmp files
|
|
|
|
if ( info.biBitCount == 8 ) {
|
|
|
|
pixbytes = 1;
|
|
|
|
}
|
|
|
|
else if ( info.biBitCount == 24 ) {
|
|
|
|
pixbytes = 3;
|
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
else
|
2012-03-17 20:01:54 +00:00
|
|
|
{
|
|
|
|
Error( "Only 8BPP and 24BPP supported" );
|
2007-11-04 03:51:54 +00:00
|
|
|
//Error("BPP %d not supported.", info.biBitCount);
|
2017-02-24 21:07:29 +00:00
|
|
|
return;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
// if this is an eight bit image load palette
|
|
|
|
if ( pixbytes == 1 ) {
|
|
|
|
drgb_t q;
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
bit->palette = reinterpret_cast<rgb_t*>( g_malloc( sizeof( rgb_t ) * 256 ) );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0; i < 256; i++ )
|
|
|
|
{
|
|
|
|
if ( fread( &q, sizeof( drgb_t ), 1, f ) != 1 ) {
|
|
|
|
fclose( f ); g_free( bit->palette );
|
|
|
|
Error( "Unable to read palette." );
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
bit->palette[i].r = q.red;
|
|
|
|
bit->palette[i].g = q.green;
|
|
|
|
bit->palette[i].b = q.blue;
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
// position to start of bitmap
|
|
|
|
fseek( f, bhd.bfOffBits, SEEK_SET );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
// create scanline to read data into
|
|
|
|
rowbytes = ( ( info.biWidth * pixbytes ) + 3 ) / 4;
|
|
|
|
rowbytes *= 4;
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
scanline = reinterpret_cast<char*>( g_malloc( rowbytes ) );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
// alloc space for new bitmap
|
|
|
|
bit->data = reinterpret_cast<unsigned char*>( g_malloc( info.biWidth * pixbytes * info.biHeight ) );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
// read in image
|
|
|
|
for ( i = 0; i < info.biHeight; i++ )
|
|
|
|
{
|
|
|
|
BMPLine( f, scanline, pixbytes, info.biWidth, info.biCompression );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
// store line
|
|
|
|
memcpy( &bit->data[info.biWidth * pixbytes * ( info.biHeight - i - 1 )], scanline, info.biWidth * pixbytes );
|
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
g_free( scanline );
|
|
|
|
fclose( f );
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
static void BMPEncodeLine( FILE *f, unsigned char *data, int npxls, int pixbytes ){
|
|
|
|
int nbytes, i, j, k;
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
switch ( pixbytes )
|
2007-11-04 03:51:54 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
case 1:
|
|
|
|
nbytes = ( npxls + 3 ) / 4;
|
|
|
|
nbytes *= 4;
|
|
|
|
|
|
|
|
fwrite( data, npxls, 1, f );
|
|
|
|
nbytes -= npxls;
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
while ( nbytes-- > 0 ) fputc( 0, f );
|
|
|
|
return;
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
// reorder rgb to bgr
|
|
|
|
for ( i = 0, j = 0; i < npxls; i++, j += 3 )
|
|
|
|
{
|
|
|
|
k = data[j];
|
|
|
|
data[j] = data[j + 2];
|
|
|
|
data[j + 2] = k;
|
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
nbytes = ( ( npxls * 3 ) + 3 ) / 4;
|
|
|
|
nbytes *= 4;
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
fwrite( data, npxls, 3, f );
|
|
|
|
nbytes -= npxls * 3;
|
|
|
|
|
|
|
|
while ( nbytes-- > 0 ) fputc( 0, f );
|
|
|
|
return;
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
Error( "BMPEncodeLine Failed." );
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void WriteBMP( char *filename, bitmap_t *bit ){
|
|
|
|
FILE *f;
|
|
|
|
bmphd_t header;
|
|
|
|
binfo_t info;
|
|
|
|
drgb_t q; // palette that gets written
|
|
|
|
long bmofs;
|
|
|
|
int w, h, i;
|
|
|
|
int pixbytes;
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( bit->bpp == 8 ) {
|
|
|
|
pixbytes = 1;
|
|
|
|
}
|
|
|
|
else if ( bit->bpp == 24 ) {
|
|
|
|
pixbytes = 3;
|
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
|
|
|
|
else
|
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
Error( "Only 8BPP and 24BPP supported" );
|
|
|
|
//Error("BPP %d not supported.", bit->bpp);
|
2017-02-24 21:07:29 +00:00
|
|
|
return;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if ( ( f = fopen( filename, "wb" ) ) == NULL ) {
|
|
|
|
Error( "Unable to open file" ); //%s.", filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
// write out an empty header as a place holder
|
|
|
|
if ( fwrite( &header, sizeof( header ), 1, f ) != 1 ) {
|
|
|
|
Error( "Unable to fwrite." );
|
|
|
|
}
|
|
|
|
|
|
|
|
// init and write info header
|
|
|
|
info.biSize = sizeof( binfo_t );
|
|
|
|
info.biWidth = bit->width;
|
|
|
|
info.biHeight = bit->height;
|
|
|
|
info.biPlanes = 1;
|
|
|
|
info.biBitCount = bit->bpp;
|
|
|
|
info.biCompression = xBI_NONE;
|
|
|
|
info.biSizeImage = bit->width * bit->height;
|
|
|
|
info.biXPelsPerMeter = 0;
|
|
|
|
info.biYPelsPerMeter = 0;
|
|
|
|
info.biClrUsed = 256;
|
|
|
|
info.biClrImportant = 256;
|
|
|
|
|
|
|
|
if ( fwrite( &info, sizeof( binfo_t ), 1, f ) != 1 ) {
|
|
|
|
Error( "fwrite failed." );
|
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
// write out palette if we need to
|
|
|
|
if ( bit->bpp == 8 ) {
|
|
|
|
for ( i = 0; i < 256; i++ )
|
|
|
|
{
|
|
|
|
q.red = bit->palette[i].r;
|
|
|
|
q.green = bit->palette[i].g;
|
|
|
|
q.blue = bit->palette[i].b;
|
|
|
|
|
|
|
|
fwrite( &q, sizeof( q ), 1, f );
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
// save offset to start of bitmap
|
|
|
|
bmofs = ftell( f );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
// output bitmap
|
|
|
|
w = bit->width;
|
|
|
|
h = bit->height;
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = h - 1; i >= 0; i-- )
|
|
|
|
{
|
|
|
|
BMPEncodeLine( f, &bit->data[w * pixbytes * i], w, pixbytes );
|
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
// update and rewrite file header
|
|
|
|
header.bfType = BMP_SIGNATURE_WORD;
|
|
|
|
header.bfSize = ftell( f );
|
|
|
|
header.bfOffBits = bmofs;
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
fseek( f, 0L, SEEK_SET );
|
|
|
|
fwrite( &header, sizeof( header ), 1, f );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
fclose( f );
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void NewBMP( int width, int height, int bpp, bitmap_t *bit ){
|
2007-11-04 03:51:54 +00:00
|
|
|
int pixbytes;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( bpp == 8 ) {
|
|
|
|
pixbytes = 1;
|
|
|
|
}
|
|
|
|
else if ( bpp == 24 ) {
|
|
|
|
pixbytes = 3;
|
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
|
|
|
|
else
|
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
Error( "NewBMP: 8 or 24 bit only." );
|
2017-02-24 21:07:29 +00:00
|
|
|
return;
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bit->bpp = bpp;
|
|
|
|
bit->width = width;
|
|
|
|
bit->height = height;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
bit->data = reinterpret_cast<unsigned char*>( g_malloc( width * height * pixbytes ) );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( bit->data == NULL ) {
|
|
|
|
Error( "NewBMP: g_malloc failed." );
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// see if we need to create a palette
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( pixbytes == 1 ) {
|
|
|
|
bit->palette = (rgb_t *) g_malloc( 768 );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( bit->palette == NULL ) {
|
|
|
|
Error( "NewBMP: unable to g_malloc palette." );
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bit->palette = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void FreeBMP( bitmap_t *bitmap ){
|
|
|
|
if ( bitmap->palette ) {
|
|
|
|
g_free( bitmap->palette );
|
|
|
|
bitmap->palette = NULL;
|
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( bitmap->data ) {
|
|
|
|
g_free( bitmap->data );
|
|
|
|
bitmap->data = NULL;
|
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|