Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. /*
  2.     SDL - Simple DirectMedia Layer
  3.     Copyright (C) 1997, 1998, 1999, 2000, 2001  Sam Lantinga
  4.  
  5.     This library is free software; you can redistribute it and/or
  6.     modify it under the terms of the GNU Library General Public
  7.     License as published by the Free Software Foundation; either
  8.     version 2 of the License, or (at your option) any later version.
  9.  
  10.     This library is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.     Library General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU Library General Public
  16.     License along with this library; if not, write to the Free
  17.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18.  
  19.     Sam Lantinga
  20.     slouken@devolution.com
  21. */
  22.  
  23.  
  24. #ifndef DISABLE_FILE
  25.  
  26. /*
  27.    Code to load and save surfaces in Windows BMP format.
  28.  
  29.    Why support BMP format?  Well, it's a native format for Windows, and
  30.    most image processing programs can read and write it.  It would be nice
  31.    to be able to have at least one image format that we can natively load
  32.    and save, and since PNG is so complex that it would bloat the library,
  33.    BMP is a good alternative.
  34.  
  35.    This code currently supports Win32 DIBs in uncompressed 8 and 24 bpp.
  36. */
  37.  
  38. #include <string.h>
  39.  
  40. #include "SDL_error.h"
  41. #include "SDL_video.h"
  42. #include "SDL_endian.h"
  43.  
  44. /* Compression encodings for BMP files */
  45. #ifndef BI_RGB
  46. #define BI_RGB          0
  47. #define BI_RLE8         1
  48. #define BI_RLE4         2
  49. #define BI_BITFIELDS    3
  50. #endif
  51.  
  52.  
  53. SDL_Surface * SDL_LoadBMP_RW (SDL_RWops *src, int freesrc)
  54. {
  55.         int was_error;
  56.         long fp_offset;
  57.         int bmpPitch;
  58.         int i, pad;
  59.         SDL_Surface *surface;
  60.         Uint32 Rmask;
  61.         Uint32 Gmask;
  62.         Uint32 Bmask;
  63.         SDL_Palette *palette;
  64.         Uint8 *bits;
  65.         int ExpandBMP;
  66.  
  67.         /* The Win32 BMP file header (14 bytes) */
  68.         char   magic[2];
  69.         Uint32 bfSize;
  70.         Uint16 bfReserved1;
  71.         Uint16 bfReserved2;
  72.         Uint32 bfOffBits;
  73.  
  74.         /* The Win32 BITMAPINFOHEADER struct (40 bytes) */
  75.         Uint32 biSize;
  76.         Sint32 biWidth;
  77.         Sint32 biHeight;
  78.         Uint16 biPlanes;
  79.         Uint16 biBitCount;
  80.         Uint32 biCompression;
  81.         Uint32 biSizeImage;
  82.         Sint32 biXPelsPerMeter;
  83.         Sint32 biYPelsPerMeter;
  84.         Uint32 biClrUsed;
  85.         Uint32 biClrImportant;
  86.  
  87.         /* Make sure we are passed a valid data source */
  88.         surface = NULL;
  89.         was_error = 0;
  90.         if ( src == NULL ) {
  91.                 was_error = 1;
  92.                 goto done;
  93.         }
  94.  
  95.         /* Read in the BMP file header */
  96.         fp_offset = SDL_RWtell(src);
  97.         SDL_ClearError();
  98.         if ( SDL_RWread(src, magic, 1, 2) != 2 ) {
  99.                 SDL_Error(SDL_EFREAD);
  100.                 was_error = 1;
  101.                 goto done;
  102.         }
  103.         if ( strncmp(magic, "BM", 2) != 0 ) {
  104.                 SDL_SetError("File is not a Windows BMP file");
  105.                 was_error = 1;
  106.                 goto done;
  107.         }
  108.         bfSize          = SDL_ReadLE32(src);
  109.         bfReserved1     = SDL_ReadLE16(src);
  110.         bfReserved2     = SDL_ReadLE16(src);
  111.         bfOffBits       = SDL_ReadLE32(src);
  112.  
  113.         /* Read the Win32 BITMAPINFOHEADER */
  114.         biSize          = SDL_ReadLE32(src);
  115.         if ( biSize == 12 ) {
  116.                 biWidth         = (Uint32)SDL_ReadLE16(src);
  117.                 biHeight        = (Uint32)SDL_ReadLE16(src);
  118.                 biPlanes        = SDL_ReadLE16(src);
  119.                 biBitCount      = SDL_ReadLE16(src);
  120.                 biCompression   = BI_RGB;
  121.                 biSizeImage     = 0;
  122.                 biXPelsPerMeter = 0;
  123.                 biYPelsPerMeter = 0;
  124.                 biClrUsed       = 0;
  125.                 biClrImportant  = 0;
  126.         } else {
  127.                 biWidth         = SDL_ReadLE32(src);
  128.                 biHeight        = SDL_ReadLE32(src);
  129.                 biPlanes        = SDL_ReadLE16(src);
  130.                 biBitCount      = SDL_ReadLE16(src);
  131.                 biCompression   = SDL_ReadLE32(src);
  132.                 biSizeImage     = SDL_ReadLE32(src);
  133.                 biXPelsPerMeter = SDL_ReadLE32(src);
  134.                 biYPelsPerMeter = SDL_ReadLE32(src);
  135.                 biClrUsed       = SDL_ReadLE32(src);
  136.                 biClrImportant  = SDL_ReadLE32(src);
  137.         }
  138.  
  139.         /* Check for read error */
  140.         if ( strcmp(SDL_GetError(), "") != 0 ) {
  141.                 was_error = 1;
  142.                 goto done;
  143.         }
  144.  
  145.         /* Expand 1 and 4 bit bitmaps to 8 bits per pixel */
  146.         switch (biBitCount) {
  147.                 case 1:
  148.                 case 4:
  149.                         ExpandBMP = biBitCount;
  150.                         biBitCount = 8;
  151.                         break;
  152.                 default:
  153.                         ExpandBMP = 0;
  154.                         break;
  155.         }
  156.  
  157.         /* We don't support any BMP compression right now */
  158.         Rmask = Gmask = Bmask = 0;
  159.         switch (biCompression) {
  160.                 case BI_RGB:
  161.                         /* If there are no masks, use the defaults */
  162.                         if ( bfOffBits == (14+biSize) ) {
  163.                                 /* Default values for the BMP format */
  164.                                 switch (biBitCount) {
  165.                                         case 15:
  166.                                         case 16:
  167.                                                 Rmask = 0x7C00;
  168.                                                 Gmask = 0x03E0;
  169.                                                 Bmask = 0x001F;
  170.                                                 break;
  171.                                         case 24:
  172. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  173.                                                 Rmask = 0x000000FF;
  174.                                                 Gmask = 0x0000FF00;
  175.                                                 Bmask = 0x00FF0000;
  176.                                                 break;
  177. #endif
  178.                                         case 32:
  179.                                                 Rmask = 0x00FF0000;
  180.                                                 Gmask = 0x0000FF00;
  181.                                                 Bmask = 0x000000FF;
  182.                                                 break;
  183.                                         default:
  184.                                                 break;
  185.                                 }
  186.                                 break;
  187.                         }
  188.                         /* Fall through -- read the RGB masks */
  189.  
  190.                 case BI_BITFIELDS:
  191.                         switch (biBitCount) {
  192.                                 case 15:
  193.                                 case 16:
  194.                                 case 32:
  195.                                         Rmask = SDL_ReadLE32(src);
  196.                                         Gmask = SDL_ReadLE32(src);
  197.                                         Bmask = SDL_ReadLE32(src);
  198.                                         break;
  199.                                 default:
  200.                                         break;
  201.                         }
  202.                         break;
  203.                 default:
  204.                         SDL_SetError("Compressed BMP files not supported");
  205.                         was_error = 1;
  206.                         goto done;
  207.         }
  208.  
  209.         /* Create a compatible surface, note that the colors are RGB ordered */
  210.         surface = SDL_CreateRGBSurface(SDL_SWSURFACE,
  211.                         biWidth, biHeight, biBitCount, Rmask, Gmask, Bmask, 0);
  212.         if ( surface == NULL ) {
  213.                 was_error = 1;
  214.                 goto done;
  215.         }
  216.  
  217.         /* Load the palette, if any */
  218.         palette = (surface->format)->palette;
  219.         if ( palette ) {
  220.                 if ( biClrUsed == 0 ) {
  221.                         biClrUsed = 1 << biBitCount;
  222.                 }
  223.                 if ( biSize == 12 ) {
  224.                         for ( i = 0; i < (int)biClrUsed; ++i ) {
  225.                                 SDL_RWread(src, &palette->colors[i].b, 1, 1);
  226.                                 SDL_RWread(src, &palette->colors[i].g, 1, 1);
  227.                                 SDL_RWread(src, &palette->colors[i].r, 1, 1);
  228.                                 palette->colors[i].unused = 0;
  229.                         }      
  230.                 } else {
  231.                         for ( i = 0; i < (int)biClrUsed; ++i ) {
  232.                                 SDL_RWread(src, &palette->colors[i].b, 1, 1);
  233.                                 SDL_RWread(src, &palette->colors[i].g, 1, 1);
  234.                                 SDL_RWread(src, &palette->colors[i].r, 1, 1);
  235.                                 SDL_RWread(src, &palette->colors[i].unused, 1, 1);
  236.                         }      
  237.                 }
  238.                 palette->ncolors = biClrUsed;
  239.         }
  240.  
  241.         /* Read the surface pixels.  Note that the bmp image is upside down */
  242.         if ( SDL_RWseek(src, fp_offset+bfOffBits, SEEK_SET) < 0 ) {
  243.                 SDL_Error(SDL_EFSEEK);
  244.                 was_error = 1;
  245.                 goto done;
  246.         }
  247.         bits = (Uint8 *)surface->pixels+(surface->h*surface->pitch);
  248.         switch (ExpandBMP) {
  249.                 case 1:
  250.                         bmpPitch = (biWidth + 7) >> 3;
  251.                         pad  = (((bmpPitch)%4) ? (4-((bmpPitch)%4)) : 0);
  252.                         break;
  253.                 case 4:
  254.                         bmpPitch = (biWidth + 1) >> 1;
  255.                         pad  = (((bmpPitch)%4) ? (4-((bmpPitch)%4)) : 0);
  256.                         break;
  257.                 default:
  258.                         pad  = ((surface->pitch%4) ?
  259.                                         (4-(surface->pitch%4)) : 0);
  260.                         break;
  261.         }
  262.         while ( bits > (Uint8 *)surface->pixels ) {
  263.                 bits -= surface->pitch;
  264.                 switch (ExpandBMP) {
  265.                         case 1:
  266.                         case 4: {
  267.                         Uint8 pixel = 0;
  268.                         int   shift = (8-ExpandBMP);
  269.                         for ( i=0; i<surface->w; ++i ) {
  270.                                 if ( i%(8/ExpandBMP) == 0 ) {
  271.                                         if ( !SDL_RWread(src, &pixel, 1, 1) ) {
  272.                                                 SDL_SetError(
  273.                                         "Error reading from BMP");
  274.                                                 was_error = 1;
  275.                                                 goto done;
  276.                                         }
  277.                                 }
  278.                                 *(bits+i) = (pixel>>shift);
  279.                                 pixel <<= ExpandBMP;
  280.                         } }
  281.                         break;
  282.  
  283.                         default:
  284.                         if ( SDL_RWread(src, bits, 1, surface->pitch)
  285.                                                          != surface->pitch ) {
  286.                                 SDL_Error(SDL_EFREAD);
  287.                                 was_error = 1;
  288.                                 goto done;
  289.                         }
  290. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  291.                         /* Byte-swap the pixels if needed. Note that the 24bpp
  292.                            case has already been taken care of above. */
  293.                         switch(biBitCount) {
  294.                                 case 15:
  295.                                 case 16: {
  296.                                         Uint16 *pix = (Uint16 *)bits;
  297.                                         for(i = 0; i < surface->w; i++)
  298.                                                 pix[i] = SDL_Swap16(pix[i]);
  299.                                         break;
  300.                                 }
  301.  
  302.                                 case 32: {
  303.                                         Uint32 *pix = (Uint32 *)bits;
  304.                                         for(i = 0; i < surface->w; i++)
  305.                                                 pix[i] = SDL_Swap32(pix[i]);
  306.                                         break;
  307.                                 }
  308.                         }
  309. #endif
  310.                         break;
  311.                 }
  312.                 /* Skip padding bytes, ugh */
  313.                 if ( pad ) {
  314.                         Uint8 padbyte;
  315.                         for ( i=0; i<pad; ++i ) {
  316.                                 SDL_RWread(src, &padbyte, 1, 1);
  317.                         }
  318.                 }
  319.         }
  320. done:
  321.         if ( was_error ) {
  322.                 if ( surface ) {
  323.                         SDL_FreeSurface(surface);
  324.                 }
  325.                 surface = NULL;
  326.         }
  327.         if ( freesrc && src ) {
  328.                 SDL_RWclose(src);
  329.         }
  330.         return(surface);
  331. }
  332.  
  333. int SDL_SaveBMP_RW (SDL_Surface *saveme, SDL_RWops *dst, int freedst)
  334. {
  335.         long fp_offset;
  336.         int i, pad;
  337.         SDL_Surface *surface;
  338.         Uint8 *bits;
  339.  
  340.         /* The Win32 BMP file header (14 bytes) */
  341.         char   magic[2] = { 'B', 'M' };
  342.         Uint32 bfSize;
  343.         Uint16 bfReserved1;
  344.         Uint16 bfReserved2;
  345.         Uint32 bfOffBits;
  346.  
  347.         /* The Win32 BITMAPINFOHEADER struct (40 bytes) */
  348.         Uint32 biSize;
  349.         Sint32 biWidth;
  350.         Sint32 biHeight;
  351.         Uint16 biPlanes;
  352.         Uint16 biBitCount;
  353.         Uint32 biCompression;
  354.         Uint32 biSizeImage;
  355.         Sint32 biXPelsPerMeter;
  356.         Sint32 biYPelsPerMeter;
  357.         Uint32 biClrUsed;
  358.         Uint32 biClrImportant;
  359.  
  360.         /* Make sure we have somewhere to save */
  361.         surface = NULL;
  362.         if ( dst ) {
  363.                 if ( saveme->format->palette ) {
  364.                         if ( saveme->format->BitsPerPixel == 8 ) {
  365.                                 surface = saveme;
  366.                         } else {
  367.                                 SDL_SetError("%d bpp BMP files not supported",
  368.                                                 saveme->format->BitsPerPixel);
  369.                         }
  370.                 }
  371.                 else if ( (saveme->format->BitsPerPixel == 24) &&
  372. #if SDL_BYTEORDER == SDL_LIL_ENDIAN
  373.                                 (saveme->format->Rmask == 0x00FF0000) &&
  374.                                 (saveme->format->Gmask == 0x0000FF00) &&
  375.                                 (saveme->format->Bmask == 0x000000FF)
  376. #else
  377.                                 (saveme->format->Rmask == 0x000000FF) &&
  378.                                 (saveme->format->Gmask == 0x0000FF00) &&
  379.                                 (saveme->format->Bmask == 0x00FF0000)
  380. #endif
  381.                           ) {
  382.                         surface = saveme;
  383.                 } else {
  384.                         SDL_Rect bounds;
  385.  
  386.                         /* Convert to 24 bits per pixel */
  387.                         surface = SDL_CreateRGBSurface(SDL_SWSURFACE,
  388.                                         saveme->w, saveme->h, 24,
  389. #if SDL_BYTEORDER == SDL_LIL_ENDIAN
  390.                                         0x00FF0000, 0x0000FF00, 0x000000FF,
  391. #else
  392.                                         0x000000FF, 0x0000FF00, 0x00FF0000,
  393. #endif
  394.                                         0);
  395.                         if ( surface != NULL ) {
  396.                                 bounds.x = 0;
  397.                                 bounds.y = 0;
  398.                                 bounds.w = saveme->w;
  399.                                 bounds.h = saveme->h;
  400.                                 if ( SDL_LowerBlit(saveme, &bounds, surface,
  401.                                                         &bounds) < 0 ) {
  402.                                         SDL_FreeSurface(surface);
  403.                                         SDL_SetError(
  404.                                         "Couldn't convert image to 24 bpp");
  405.                                         surface = NULL;
  406.                                 }
  407.                         }
  408.                 }
  409.         }
  410.  
  411.         if ( surface && (SDL_LockSurface(surface) == 0) ) {
  412.                 /* Set the BMP file header values */
  413.                 bfSize = 0;              /* We'll write this when we're done */
  414.                 bfReserved1 = 0;
  415.                 bfReserved2 = 0;
  416.                 bfOffBits = 0;          /* We'll write this when we're done */
  417.  
  418.                 /* Write the BMP file header values */
  419.                 fp_offset = SDL_RWtell(dst);
  420.                 SDL_ClearError();
  421.                 SDL_RWwrite(dst, magic, 2, 1);
  422.                 SDL_WriteLE32(dst, bfSize);
  423.                 SDL_WriteLE16(dst, bfReserved1);
  424.                 SDL_WriteLE16(dst, bfReserved2);
  425.                 SDL_WriteLE32(dst, bfOffBits);
  426.  
  427.                 /* Set the BMP info values */
  428.                 biSize = 40;
  429.                 biWidth = surface->w;
  430.                 biHeight = surface->h;
  431.                 biPlanes = 1;
  432.                 biBitCount = surface->format->BitsPerPixel;
  433.                 biCompression = BI_RGB;
  434.                 biSizeImage = surface->h*surface->pitch;
  435.                 biXPelsPerMeter = 0;
  436.                 biYPelsPerMeter = 0;
  437.                 if ( surface->format->palette ) {
  438.                         biClrUsed = surface->format->palette->ncolors;
  439.                 } else {
  440.                         biClrUsed = 0;
  441.                 }
  442.                 biClrImportant = 0;
  443.  
  444.                 /* Write the BMP info values */
  445.                 SDL_WriteLE32(dst, biSize);
  446.                 SDL_WriteLE32(dst, biWidth);
  447.                 SDL_WriteLE32(dst, biHeight);
  448.                 SDL_WriteLE16(dst, biPlanes);
  449.                 SDL_WriteLE16(dst, biBitCount);
  450.                 SDL_WriteLE32(dst, biCompression);
  451.                 SDL_WriteLE32(dst, biSizeImage);
  452.                 SDL_WriteLE32(dst, biXPelsPerMeter);
  453.                 SDL_WriteLE32(dst, biYPelsPerMeter);
  454.                 SDL_WriteLE32(dst, biClrUsed);
  455.                 SDL_WriteLE32(dst, biClrImportant);
  456.  
  457.                 /* Write the palette (in BGR color order) */
  458.                 if ( surface->format->palette ) {
  459.                         SDL_Color *colors;
  460.                         int       ncolors;
  461.  
  462.                         colors = surface->format->palette->colors;
  463.                         ncolors = surface->format->palette->ncolors;
  464.                         for ( i=0; i<ncolors; ++i ) {
  465.                                 SDL_RWwrite(dst, &colors[i].b, 1, 1);
  466.                                 SDL_RWwrite(dst, &colors[i].g, 1, 1);
  467.                                 SDL_RWwrite(dst, &colors[i].r, 1, 1);
  468.                                 SDL_RWwrite(dst, &colors[i].unused, 1, 1);
  469.                         }
  470.                 }
  471.  
  472.                 /* Write the bitmap offset */
  473.                 bfOffBits = SDL_RWtell(dst)-fp_offset;
  474.                 if ( SDL_RWseek(dst, fp_offset+10, SEEK_SET) < 0 ) {
  475.                         SDL_Error(SDL_EFSEEK);
  476.                 }
  477.                 SDL_WriteLE32(dst, bfOffBits);
  478.                 if ( SDL_RWseek(dst, fp_offset+bfOffBits, SEEK_SET) < 0 ) {
  479.                         SDL_Error(SDL_EFSEEK);
  480.                 }
  481.  
  482.                 /* Write the bitmap image upside down */
  483.                 bits = (Uint8 *)surface->pixels+(surface->h*surface->pitch);
  484.                 pad  = ((surface->pitch%4) ? (4-(surface->pitch%4)) : 0);
  485.                 while ( bits > (Uint8 *)surface->pixels ) {
  486.                         bits -= surface->pitch;
  487.                         if ( SDL_RWwrite(dst, bits, 1, surface->pitch)
  488.                                                         != surface->pitch) {
  489.                                 SDL_Error(SDL_EFWRITE);
  490.                                 break;
  491.                         }
  492.                         if ( pad ) {
  493.                                 const Uint8 padbyte = 0;
  494.                                 for ( i=0; i<pad; ++i ) {
  495.                                         SDL_RWwrite(dst, &padbyte, 1, 1);
  496.                                 }
  497.                         }
  498.                 }
  499.  
  500.                 /* Write the BMP file size */
  501.                 bfSize = SDL_RWtell(dst)-fp_offset;
  502.                 if ( SDL_RWseek(dst, fp_offset+2, SEEK_SET) < 0 ) {
  503.                         SDL_Error(SDL_EFSEEK);
  504.                 }
  505.                 SDL_WriteLE32(dst, bfSize);
  506.                 if ( SDL_RWseek(dst, fp_offset+bfSize, SEEK_SET) < 0 ) {
  507.                         SDL_Error(SDL_EFSEEK);
  508.                 }
  509.  
  510.                 /* Close it up.. */
  511.                 SDL_UnlockSurface(surface);
  512.                 if ( surface != saveme ) {
  513.                         SDL_FreeSurface(surface);
  514.                 }
  515.         }
  516.  
  517.         if ( freedst && dst ) {
  518.                 SDL_RWclose(dst);
  519.         }
  520.         return((strcmp(SDL_GetError(), "") == 0) ? 0 : -1);
  521. }
  522.  
  523. #endif /* ENABLE_FILE */
  524.