Ticket #38: GrafX2_deluxe_paint_ST_iff.patch

File GrafX2_deluxe_paint_ST_iff.patch, 13.0 KB (added by miniupnp, 7 years ago)

patch for support of "VERT WORD" IFF Files

  • fileformats.c

     
    231231  word  X_org;       // Inutile
    232232  word  Y_org;       // Inutile
    233233  byte  BitPlanes;
    234   byte  Mask;
    235   byte  Compression;
    236   byte  Pad1;       // Inutile
    237   word  Transp_col;
     234  byte  Mask;        // 0=none, 1=mask, 2=transp color, 3=Lasso
     235  byte  Compression; // 0=none, 1=packbits, 2=vertical RLE
     236  byte  Pad1;        // Inutile
     237  word  Transp_col;  // transparent color for masking mode 2
    238238  byte  X_aspect;    // Inutile
    239239  byte  Y_aspect;    // Inutile
    240240  word  X_screen;
     
    438438}
    439439
    440440// ------------------------- Attendre une section -------------------------
    441 byte IFF_Wait_for(byte * expected_section)
     441byte IFF_Wait_for(const char * expected_section)
    442442{
    443443  // Valeur retournée: 1=Section trouvée, 0=Section non trouvée (erreur)
    444444  byte section_read[4];
     
    584584  short y_pos;
    585585  short counter;
    586586  short line_size;       // Taille d'une ligne en octets
     587  short plane_line_size;  // Size of line in bytes for 1 plane
    587588  short real_line_size; // Taille d'une ligne en pixels
    588589  byte  color;
    589590  long  file_size;
    590591  dword dummy;
    591592  byte  is_anim=0;
    592593  int iff_format;
     594  int plane;
    593595
    594596  Get_full_filename(filename, context->File_name, context->File_directory);
    595597
     
    617619    else
    618620      iff_format = FORMAT_PBM;
    619621   
    620     if (!IFF_Wait_for((byte *)"BMHD"))
     622    if (!IFF_Wait_for("BMHD"))
    621623      File_error=1;
    622624    Read_dword_be(IFF_file,&dummy);
    623625
     
    637639      && (Read_word_be(IFF_file,&header.Y_screen))
    638640      && header.Width && header.Height)
    639641    {
    640       if ( (header.BitPlanes) && (IFF_Wait_for((byte *)"CMAP")) )
     642      if ( (header.BitPlanes) && (IFF_Wait_for("CMAP")) )
    641643      {
    642644        Read_dword_be(IFF_file,&nb_colors);
    643645        nb_colors/=3;
     
    662664
    663665        if ( (!File_error) && (nb_colors>=2) && (nb_colors<=256) )
    664666        {
     667          byte real_bit_planes = header.BitPlanes;
     668
    665669          if (header.Mask==1)
    666670            header.BitPlanes++;
    667671
     
    769773                if (!memcmp(format,"ILBM",4))    // "ILBM": InterLeaved BitMap
    770774                {
    771775                  // Calcul de la taille d'une ligne ILBM (pour les images ayant des dimensions exotiques)
    772                   if (context->Width & 15)
     776                  real_line_size = (context->Width+15) & ~15;
     777                  plane_line_size = real_line_size >> 3;  // 8bits per byte
     778                  line_size = plane_line_size * header.BitPlanes;
     779
     780                  switch(header.Compression)
    773781                  {
    774                     real_line_size=( (context->Width+16) >> 4 ) << 4;
    775                     line_size=( (context->Width+16) >> 4 )*(header.BitPlanes<<1);
    776                   }
    777                   else
    778                   {
    779                     real_line_size=context->Width;
    780                     line_size=(context->Width>>3)*header.BitPlanes;
    781                   }
     782                    case 0:            // non compressé
     783                      IFF_buffer=(byte *)malloc(line_size);
     784                      for (y_pos=0; ((y_pos<context->Height) && (!File_error)); y_pos++)
     785                      {
     786                        if (Read_bytes(IFF_file,IFF_buffer,line_size))
     787                          Draw_IFF_line(context, y_pos,real_line_size, header.BitPlanes);
     788                        else
     789                          File_error=21;
     790                      }
     791                      free(IFF_buffer);
     792                      IFF_buffer = NULL;
     793                      break;
     794                    case 1:          // compressé packbits (Amiga)
     795                      /*Init_lecture();*/
    782796
    783                   if (!header.Compression)
    784                   {                                           // non compressé
    785                     IFF_buffer=(byte *)malloc(line_size);
    786                     for (y_pos=0; ((y_pos<context->Height) && (!File_error)); y_pos++)
    787                     {
    788                       if (Read_bytes(IFF_file,IFF_buffer,line_size))
    789                         Draw_IFF_line(context, y_pos,real_line_size, header.BitPlanes);
    790                       else
    791                         File_error=21;
    792                     }
    793                     free(IFF_buffer);
    794                     IFF_buffer = NULL;
    795                   }
    796                   else
    797                   {                                               // compressé
    798                     /*Init_lecture();*/
     797                      IFF_buffer=(byte *)malloc(line_size);
    799798
    800                     IFF_buffer=(byte *)malloc(line_size);
     799                      for (y_pos=0; ((y_pos<context->Height) && (!File_error)); y_pos++)
     800                      {
     801                        for (x_pos=0; ((x_pos<line_size) && (!File_error)); )
     802                        {
     803                          if(Read_byte(IFF_file, &temp_byte)!=1)
     804                          {
     805                            File_error=22;
     806                            break;
     807                          }
     808                          // Si temp_byte > 127 alors il faut répéter 256-'temp_byte' fois la couleur de l'octet suivant
     809                          // Si temp_byte <= 127 alors il faut afficher directement les 'temp_byte' octets suivants
     810                          if (temp_byte>127)
     811                          {
     812                            if(Read_byte(IFF_file, &color)!=1)
     813                            {
     814                              File_error=23;
     815                              break;
     816                            }
     817                            b256=(short)(256-temp_byte);
     818                            for (counter=0; counter<=b256; counter++)
     819                              if (x_pos<line_size)
     820                                IFF_buffer[x_pos++]=color;
     821                              else
     822                                File_error=24;
     823                          }
     824                          else
     825                            for (counter=0; counter<=(short)(temp_byte); counter++)
     826                              if (x_pos>=line_size || Read_byte(IFF_file, &(IFF_buffer[x_pos++]))!=1)
     827                                File_error=25;
     828                        }
     829                        if (!File_error)
     830                          Draw_IFF_line(context, y_pos,real_line_size,header.BitPlanes);
     831                      }
    801832
    802                     for (y_pos=0; ((y_pos<context->Height) && (!File_error)); y_pos++)
    803                     {
    804                       for (x_pos=0; ((x_pos<line_size) && (!File_error)); )
     833                      free(IFF_buffer);
     834                      IFF_buffer = NULL;
     835                      /*Close_lecture();*/
     836                      break;
     837                    case 2:     // compressé vertical RLE (Atari ST)
     838                      IFF_buffer=(byte *)malloc(line_size*context->Height);
     839                      plane_line_size = real_line_size >> 3;
     840                      for (plane = 0; plane < header.BitPlanes && !File_error; plane++)
    805841                      {
    806                         if(Read_byte(IFF_file, &temp_byte)!=1)
     842                        word cmd_count;
     843                        word cmd;
     844                        signed char * commands;
     845                        word count;
     846
     847                        y_pos = 0; x_pos = 0;
     848                        if (!IFF_Wait_for("VDAT"))
    807849                        {
    808                           File_error=22;
     850                          File_error = 30;
    809851                          break;
    810852                        }
    811                         // Si temp_byte > 127 alors il faut répéter 256-'temp_byte' fois la couleur de l'octet suivant
    812                         // Si temp_byte <= 127 alors il faut afficher directement les 'temp_byte' octets suivants
    813                         if (temp_byte>127)
     853                        Read_dword_be(IFF_file,&section_size);
     854                        Read_word_be(IFF_file,&cmd_count);
     855                        cmd_count -= 2;
     856                        commands = (signed char *)malloc(cmd_count);
     857                        if (!Read_bytes(IFF_file,commands,cmd_count))
    814858                        {
    815                           if(Read_byte(IFF_file, &color)!=1)
    816                           {
    817                             File_error=23;
    818                             break;
     859                          File_error = 31;
     860                          break;
     861                        }
     862                        for (cmd = 0; cmd < cmd_count && x_pos < plane_line_size; cmd++)
     863                        {
     864                          if (commands[cmd] <= 0)
     865                          { // cmd=0 : load count from data, COPY
     866                            // cmd < 0 : count = -cmd, COPY
     867                            if (commands[cmd] == 0)
     868                              Read_word_be(IFF_file,&count);
     869                            else
     870                              count = -commands[cmd];
     871                            while (count-- > 0 && x_pos < plane_line_size)
     872                            {
     873                              Read_bytes(IFF_file,IFF_buffer+x_pos+y_pos*line_size+plane*plane_line_size,2);
     874                              if(++y_pos >= context->Height)
     875                              {
     876                                y_pos = 0;
     877                                x_pos += 2;
     878                              }
     879                            }
    819880                          }
    820                           b256=(short)(256-temp_byte);
    821                           for (counter=0; counter<=b256; counter++)
    822                             if (x_pos<line_size)
    823                               IFF_buffer[x_pos++]=color;
     881                          else if (commands[cmd] >= 1)
     882                          { // cmd=1 : load count from data, RLE
     883                            // cmd >1 : count = cmd, RLE
     884                            byte data[2];
     885                            if (commands[cmd] == 1)
     886                              Read_word_be(IFF_file,&count);
    824887                            else
    825                               File_error=24;
     888                              count = (word)commands[cmd];
     889                            Read_bytes(IFF_file,data,2);
     890                            while (count-- > 0 && x_pos < plane_line_size)
     891                            {
     892                              memcpy(IFF_buffer+x_pos+y_pos*line_size+plane*plane_line_size,data,2);
     893                              if (++y_pos >= context->Height)
     894                              {
     895                                y_pos = 0;
     896                                x_pos += 2;
     897                              }
     898                            }
     899                          }
    826900                        }
    827                         else
    828                           for (counter=0; counter<=(short)(temp_byte); counter++)
    829                             if (x_pos>=line_size || Read_byte(IFF_file, &(IFF_buffer[x_pos++]))!=1)
    830                               File_error=25;
     901                        free(commands);
    831902                      }
    832903                      if (!File_error)
    833                         Draw_IFF_line(context, y_pos,real_line_size,header.BitPlanes);
    834                     }
    835 
    836                     free(IFF_buffer);
    837                     IFF_buffer = NULL;
    838                     /*Close_lecture();*/
     904                      {
     905                        byte * save_IFF_buffer = IFF_buffer;
     906                        for (y_pos = 0; y_pos < context->Height; y_pos++)
     907                        {
     908                          Draw_IFF_line(context,y_pos,real_line_size,real_bit_planes);
     909                          IFF_buffer += line_size;
     910                        }
     911                        IFF_buffer = save_IFF_buffer;
     912                      }
     913                      free(IFF_buffer);
     914                      IFF_buffer = NULL;
     915                      break;
     916                    default: // compression non reconnue
     917                      File_error = 32;
    839918                  }
    840919                }
    841920                else                               // "PBM ": Planar(?) BitMap
     
    909988
    910989                  // ILBM, hopefully
    911990                  Read_bytes(IFF_file,format,4);
    912                   if (!IFF_Wait_for((byte *)"DLTA"))
     991                  if (!IFF_Wait_for("DLTA"))
    913992                  {
    914993                    File_error=1;
    915994                    break;
     
    11421221    if (context->Format == FORMAT_LBM)
    11431222    {
    11441223      short line_size; // Size of line in bytes
     1224      short plane_line_size;  // Size of line in bytes for 1 plane
    11451225      short real_line_size; // Size of line in pixels
    11461226     
    11471227      // Calcul de la taille d'une ligne ILBM (pour les images ayant des dimensions exotiques)
    1148       if (context->Width & 15)
    1149       {
    1150         real_line_size=( (context->Width+16) >> 4 ) << 4;
    1151         line_size=( (context->Width+16) >> 4 )*(header.BitPlanes<<1);
    1152       }
    1153       else
    1154       {
    1155         real_line_size=context->Width;
    1156         line_size=(context->Width>>3)*header.BitPlanes;
    1157       }
     1228      real_line_size = (context->Width+15) & ~15;
     1229      plane_line_size = real_line_size >> 3;  // 8bits per byte
     1230      line_size = plane_line_size * header.BitPlanes;
    11581231      IFF_buffer=(byte *)malloc(line_size);
    11591232     
    11601233      // Start encoding