Newer
Older
ubixfs-2 / OBJIMAGE.PAS
@flameshadow flameshadow on 10 Jun 2005 6 KB UbixFS
(********************************************************************)
(*                                                                  *)
(*                               OBJIMAGE.PAS                       *)
(*                                                                  *)
(*                 Object Oriented Graphics Image Support           *)
(*                                                                  *)
(*                            Copyright (c) 2002                    *)
(*                              Mark J Iuzzolino                    *)
(*                                                                  *)
(*                                                                  *)
(********************************************************************)

unit objImage;

interface

uses ObjGfx40;

type ogImageType = (NOGRAPHIC,BMP);

const ogImageTypeStr:array[ogImageType] of string[3]=('','BMP');
type
  ogImagePtr = ^ogImage;
  ogImage = object
   private
     tmpBuf:^ogSurface;
     c_buf:pointer;
     c_buf_size:uInt32;
     c_buf_idx:uInt32;
   public
     constructor init;
     function SaveGfx(var SrcObject:ogSurface; const gfxfile:string;GfxType:ogImageType):boolean; virtual;
     function SaveGfxTo(var SrcObject:ogSurface; const gfxfile:string;
                        GfxType:ogImageType;offset:longint):boolean; virtual;
     destructor done;
  end;

type
  ogImageFmt = record
    pixFmt             : ogPixelFmt;
    xRes               : uInt32;
    yRes               : uInt32;
  end;

implementation

uses dos;

type
  BitMapHeaDer = packed record
    width,height       : uInt16;
    left,top           : uInt16;
    BitPlanes          : byte;
    Masking            : byte;
    Compress           : byte;
    Paddington         : byte;
    Transparency       : word;
    XAspectRatio       : byte;
    YAspectRatio       : byte;
    PageWidth          : word;
    PageHeight         : word;
  end;

type
  WinBMPFileHeader = packed record
    FileType           : uInt16;
    FileSize           : uInt32;
    Reserved1          : uInt16;   {always 0}
    Reserved2          : uInt16;   {always 0}
    BitmapOffset       : uInt32;
  end;

type
  Win3xBitmapHeader = packed record
    FileHeader         : WinBMPFileHeader;
    Size               : uInt32;
    Width,Height       : Int32;
    Planes             : uInt16;
    BitsPerPixel       : uInt16;
    { Fields added for Windows 3.x follow this line}
    Compression        : uInt32;
    SizeOfBitmap       : uInt32;
    HorzResolution     : Int32;
    VertResolution     : Int32;
    ColoursUsed        : UInt32;
    ColoursImportant   : UInt32;
  end;

type
  Win3xPaletteElement = packed record
    blue,green,red,pad : byte;
  end;

function bswapl(input:uInt32):uInt32; assembler;
asm
        mov     eax,input
        bswap   eax
        mov     result, eax
(*
 *       mov     ax,word ptr input+2
 *       xchg    ah,al
 *       mov     dx,word ptr input
 *       xchg    dh,dl
 *)
end;

function bswapw(input:uInt16):uInt16; assembler;
asm
        mov     ax,input
        xchg    ah,al
        mov     result, ax
end;

constructor ogImage.Init;
begin
  tmpBuf:=NIL;
  c_buf:=NIL;
  c_buf_size:=0;
  c_buf_idx:=0;
end;

function ogImage.saveGfx;
begin
  saveGfx:=saveGfxTo(SrcObject,gfxfile,gfxtype,0);
end;

function ogImage.saveGfxTo;
const ID_S:array[0..7] of char='MOOISHI!';
var header:pointer;
    ColourUsage:array[byte] of byte;
    x,y,index,count,tmp,linesize:word;
    cbuf:pointer;
    colour:byte;
    result:dword;
    plte:Win3xPaletteElement;
    ID_Chunk:array[0..3] of char;
    bm:UInt32;
    data:pointer;
    f:file;
label SafeExit;
begin
   SaveGfxTo:=FALSE;
   if not(srcObject.ogAvail) then exit;
   bm := (SrcObject.ogGetBPP+7) shr 3;
   getmem(header,256);
   fillchar(header^,256,0);
   assign(f,gfxfile);
   if (offset=0) then
     rewrite(f,1)
   else
     begin
       if (fsearch(gfxfile,'')='') then exit;
       reset(f,1);
       seek(f,offset);
     end;

   with Win3xBitmapHeader(header^) do
     begin
{       fillchar(ColourUsage,sizeof(ColourUsage),0);

       for y:=0 to SrcObject.GetMaxY do
         for x:=0 to SrcObject.GetMaxX do
           ColourUsage[SrcObject.getpixel(x,y)]:=1;
       colour:=0;

       for x:=0 to high(ColourUsage) do
         inc(colour,ColourUsage[x]);}

       Size:=sizeof(Win3xBitmapHeader)-sizeof(FileHeader);
       width:=SrcObject.ogGetMaxX+1;
       linesize:=((width*bm+3) shr 2) shl 2;
       Height:=(SrcObject.ogGetMaxY+1);
       with FileHeader do
          begin
           FileType:=$4D42;
           BitMapOffset:=sizeof(Win3xBitmapHeader){+sizeof(Win3xPaletteElement)*256};
           FileSize:=BitMapOffset+LineSize*dword(height);
           Reserved1:=0;
           Reserved2:=0;
          end;
          Planes:=1;
          BitsPerPixel:=SrcObject.ogGetBPP;
          Compression:=0;
          HorzResolution:=0;
          VertResolution:=0;
          ColoursUsed:=0;
          ColoursImportant:=0;
          blockwrite(f,header^,sizeof(Win3xBitMapHeader));
          plte.pad:=0;
          plte.red:=0;
          plte.green:=0;
          plte.blue:=0;
          tmp:=linesize-width*bm;
          with SrcObject, plte do
          case ogGetBPP of
           24:for y:=ogGetMaxY downto 0 do
                begin
                  data:=ogGetPtr(0,y);
                  blockwrite(f,data^,width*bm);
{                  for x:=0 to GetMaxX do
                    begin
                      unpackRGB(getPixel(x,y),red, green, blue);
                      blockwrite(f,blue,1);
                      blockwrite(f,green,1);
                      blockwrite(f,red,1);
                    end;
                  plte.red:=0;
                   plte.green:=0;
                  plte.blue:=0;}
                 if tmp<>0 then blockwrite(f,plte,tmp)
                end;
           end; {case }
{          for y:=GetMaxY downto 0 do
           begin
            blockwrite(f,pointer(dword(Buffer)+LineOfs^[y])^,width);
            if tmp<>0 then blockwrite(f,plte,tmp)
           end}
        end;
 SaveGfxTo:=TRUE;
SafeExit:
 close(f);
 freemem(header,256);
end;

destructor ogImage.done;
begin
{  if (tmpBuf<>NIL) then dispose(tmpBuf,ogDone);}
end;

end.