source: abuse/tags/pd/imlib/mdlread.c @ 161

Last change on this file since 161 was 49, checked in by Sam Hocevar, 15 years ago
  • Imported original public domain release, for future reference.
  • Property svn:keywords set to Id
File size: 5.3 KB
Line 
1#include "mdlread.hpp"
2#include "macs.hpp"
3#include "linked.hpp"
4#include "system.h"
5#include <stdio.h>
6
7
8//  write_mdl take an array of pointer to images and a palette
9// and the generates a Moving DeLight file format containing all the
10// images.  Note, only the mode 320x200x256 is sopprted here for saving
11// images.  All images should be sized so they will fit on an mdl screen
12// but no checking of that is done hhere.
13void write_mdl(image **images, short total_images, palette *pal,char *fn,
14                short firstpage, short images_per_page)
15{
16  FILE *fp;
17  char buf[18];
18  unsigned short xy[2],x;
19  char name[13],page;
20  unsigned char *c;
21  short i;
22  palette *np;
23  clear_errors();
24  CONDITION(images && pal && fn && total_images>0,"bad parms");
25  CONDITION(pal->pal_size()==256,"MDL write only support 256 color images");
26
27  fp=fopen(fn,"wb");
28  if (!fp) set_error(imWRITE_ERROR);
29  else
30  { strcpy(buf,"JC20");            // Signature for mdl file
31    buf[4]=255;                    // 255 is graph driver 320x200x256
32    buf[5]=0;                      // graph mode 0 for this graph driver
33    buf[6]=19;                     // the BIOS mode is 19
34    fwrite(buf,7,1,fp);
35    np=pal->copy();                // make a copy before we change
36    np->shift(-COLOR_SHIFT);       // PC palette don't have 8 bit color regs
37    fwrite(np->addr(),1,768,fp);
38    delete np;                     // destroy the copy me made
39    memset(buf,0,11);              // 11 reserved bytes (0) follow
40    fwrite(buf,11,1,fp);
41    for (i=0;i<total_images;i++)
42    {
43      memset(buf,0,6);            // each image has 6 bytes of reserved 0
44      fwrite(buf,6,1,fp);
45      xy[0]=int_to_intel(i%100+20); xy[1]=int_to_intel(30);  // the x and y position on the screen
46      fwrite(xy,4,1,fp);
47      sprintf(name,"JC%-10d",i);  // set the name of the image
48      fwrite(name,12,1,fp);
49
50      page=firstpage+i/images_per_page;
51
52      fwrite(&page,1,1,fp);         // put all of the image on the first page
53      xy[0]=int_to_intel(images[i]->width()*images[i]->height()+4);  // calc the size of the image
54   
55      fwrite(xy,2,1,fp);
56      xy[0]=int_to_intel(images[i]->width());
57      fwrite(xy,2,1,fp);
58      xy[0]=int_to_intel(images[i]->height());
59      fwrite(xy,2,1,fp);
60      for (x=0;x<(unsigned short)images[i]->height();x++)   // write all the scan_lines for the
61      { c=images[i]->scan_line(x);            // image
62        fwrite(c,images[i]->width(),1,fp);
63      }
64    }
65    fclose(fp);                // close the file and make sure buffers empty
66  }
67}
68
69short mdl_total_images(char *fn)
70{
71  char buf[800];
72  unsigned short xy[2],t;
73  FILE *fp;
74  fp=fopen(fn,"rb");
75  if (!fp)
76  { set_error(imFILE_NOT_FOUND);
77    return 0;
78  }
79  if (fread(buf,2,1,fp)!=1)
80    set_error(imFILE_CORRUPTED);
81  else if (buf[0]!='J' || buf[1]!='C')
82    set_error(imINCORRECT_FILETYPE);
83  else if (fread(buf,5,1,fp)!=1)
84    set_error(imFILE_CORRUPTED);
85  else if (buf[4]!=0x13)
86    set_error(imNOT_SUPPORTED);
87  if (current_error()) { fclose(fp); return 0;}
88  fread(buf,1,768+11,fp);
89  t=0;
90  while (!feof(fp))
91  { if (fread(buf,1,23,fp)==23)
92    {
93      fread(xy,2,1,fp);
94      xy[0]=int_to_local(xy[0]);
95      fseek(fp,xy[0],SEEK_CUR);
96      t++;
97    }
98  }
99  fclose(fp);
100  return t;
101}
102
103// read_mdl returns an array containing pointers to all the desired images
104// and a palette that is read form the file
105// to load image numbers 4 through 9 let start =4, end=9
106image **read_mdl(char *fn, palette *&pal, short startn, short endn, short &total)
107{
108  FILE *fp;
109  image **im;
110  char buf[50];
111  unsigned short xy[2],i,j;
112  clear_errors();
113  make_block(sizeof(FILE));
114  im=NULL;
115  total=0;
116  startn--;
117  CHECK(fn && (startn<=endn || endn==-1) && startn>=0);
118  fp=fopen(fn,"rb");
119  if (!fp)
120  { set_error(imFILE_NOT_FOUND);
121    return NULL;
122  }
123  if (fread(buf,2,1,fp)!=1)
124    set_error(imFILE_CORRUPTED);
125  else if (buf[0]!='J' || buf[1]!='C')
126    set_error(imINCORRECT_FILETYPE);
127  else if (fread(buf,5,1,fp)!=1)
128    set_error(imFILE_CORRUPTED);
129  else if (buf[4]!=0x13)
130    set_error(imNOT_SUPPORTED);
131  else
132  {
133    make_block(sizeof(palette));
134    pal=new palette(256);
135    if (!pal)
136    {  set_error(imMEMORY_ERROR); return NULL; }
137    if (fread(pal->addr(),1,768,fp)!=768)
138      set_error(imFILE_CORRUPTED);
139    else if (fread(buf,1,11,fp)!=11)
140      set_error(imFILE_CORRUPTED);
141    else
142    {
143      pal->shift(2);
144      pal->set_all_used();
145      while (startn && !current_error())
146      { if (fread(buf,1,23,fp)!=23)
147          set_error(imFILE_CORRUPTED);
148        fread(xy,2,1,fp);
149        xy[0]=int_to_local(xy[0]);
150        fseek(fp,xy[0],SEEK_CUR);
151        startn--; if (endn>0) endn--;
152      }
153      if (!current_error())
154        im=(image **)jmalloc(sizeof(image *)*endn,"mdl_read::image * array");
155
156      while ((startn<endn || endn==-1) && !feof(fp) && !current_error())
157      {
158        if (fread(buf,1,23,fp)==23)
159        {
160          if (fread(&j,1,2,fp)!=2) set_error(imFILE_CORRUPTED);
161          else
162          {
163            j=int_to_local(j);
164            j-=4;
165            xy[0]=5; xy[1]=5;
166            if (fread(xy,1,4,fp)!=4) set_error(imFILE_CORRUPTED);
167            make_block(sizeof(image));
168            xy[0]=int_to_local(xy[0]);
169            xy[1]=int_to_local(xy[1]);
170            im[startn]=new image(xy[0],xy[1]);
171            total++;
172            for (i=0;i<xy[1];i++)
173              if (fread(im[startn]->scan_line(i),xy[0],1,fp)!=1)
174                set_error(imFILE_CORRUPTED);
175              else j-=xy[0];
176            if (j)
177              fseek(fp,j,SEEK_CUR);
178          }
179          startn++;
180        }
181      }
182    }
183  }
184  fclose(fp);
185  return im;
186}
Note: See TracBrowser for help on using the repository browser.