00001
00017
00018
00019 #include <stdio.h>
00020 #include <stdlib.h>
00021 #include <string.h>
00022 #include "lbfileio.h"
00023
00024 #include "bulcommn.h"
00025
00029 char const *RNC_SIGNATURE_STR="RNC\001";
00030
00044 short write_bmp_fn_idx (const char *fname, int width, int height,
00045 const unsigned char *pal, const char *data,
00046 int red, int green, int blue, int mult)
00047 {
00048 FILE *out;
00049 out = fopen (fname, "wb");
00050 if (out==NULL)
00051 {
00052 printf ("\nCan't open file %s.\n", fname);
00053 return 1;
00054 }
00055
00056 short result;
00057 result=write_bmp_fp_idx(out,width,height,pal,data,red,green,blue,mult);
00058 fclose (out);
00059 return result;
00060 }
00061
00075 short write_bmp_fp_idx (FILE *out, int width, int height,
00076 const unsigned char *pal, const char *data,
00077 int red, int green, int blue, int mult)
00078 {
00079 int pwidth, pheight;
00080 long data_len,pal_len;
00081 int i, j;
00082
00083
00084 if (width>=0)
00085 pwidth=width;
00086 else
00087 pwidth=-width;
00088 if (height>=0)
00089 pheight=height;
00090 else
00091 pheight=-height;
00092 int padding_size=4-(pwidth&3);
00093
00094 data_len = (pwidth+padding_size)*pheight;
00095
00096 pal_len = 256*4;
00097 fputs("BM",out);
00098 write_int32_le_file(out, data_len+pal_len+0x36);
00099 write_int32_le_file(out, 0);
00100 write_int32_le_file(out, pal_len+0x36);
00101 write_int32_le_file(out, 40);
00102 write_int32_le_file(out, width);
00103 write_int32_le_file(out, height);
00104 write_int16_le_file(out, 1);
00105 write_int16_le_file(out, 8);
00106 write_int32_le_file(out, 0);
00107 write_int32_le_file(out, 0);
00108 write_int32_le_file(out, 0);
00109 write_int32_le_file(out, 0);
00110 write_int32_le_file(out, 0);
00111 write_int32_le_file(out, 0);
00112
00113 for (i=0; i < 256; i++)
00114 {
00115 unsigned int cval;
00116 cval=(unsigned int)pal[i*3+blue]*mult;
00117 if (cval>255) cval=255;
00118 fputc(cval, out);
00119 cval=(unsigned int)pal[i*3+green]*mult;
00120 if (cval>255) cval=255;
00121 fputc(cval, out);
00122 cval=(unsigned int)pal[i*3+red]*mult;
00123 if (cval>255) cval=255;
00124 fputc(cval, out);
00125 fputc(0, out);
00126 }
00127
00128 for (i=1; i <= pheight; i++)
00129 {
00130 fwrite (data+(pheight-i)*pwidth, pwidth, 1, out);
00131 if ((padding_size&3) > 0)
00132 for (j=0; j < padding_size; j++)
00133 fputc (0, out);
00134 }
00135
00136 return 0;
00137 }
00138
00147 short write_bmp_fn_24b (const char *fname, int width, int height, const char *data)
00148 {
00149 FILE *out;
00150 out = fopen (fname, "wb");
00151 if (out==NULL)
00152 {
00153 printf ("\nCan't open file %s.\n", fname);
00154 return 1;
00155 }
00156
00157 short result;
00158 result=write_bmp_fp_24b(out,width,height,data);
00159 fclose (out);
00160 return result;
00161 }
00162
00171 short write_bmp_fp_24b(FILE *out, int width, int height, const char *data)
00172 {
00173 int pwidth, pheight;
00174 long data_len;
00175 int i, j;
00176
00177
00178 if (width>=0)
00179 pwidth=width;
00180 else
00181 pwidth=-width;
00182 if (height>=0)
00183 pheight=height;
00184 else
00185 pheight=-height;
00186
00187 int datawidth=width*3;
00188 int padding_size=4-(datawidth&3);
00189
00190 data_len = (pwidth+padding_size)*pheight;
00191 fputs("BM",out);
00192 write_int32_le_file (out, 3*data_len+0x36);
00193 write_int32_le_file (out, 0);
00194 write_int32_le_file (out, 0x36);
00195 write_int32_le_file (out, 40);
00196 write_int32_le_file (out, width);
00197 write_int32_le_file (out, height);
00198 write_int16_le_file (out, 1);
00199 write_int16_le_file (out, 24);
00200 write_int32_le_file (out, 0);
00201 write_int32_le_file (out, 0);
00202 write_int32_le_file (out, 0);
00203 write_int32_le_file (out, 0);
00204 write_int32_le_file (out, 0);
00205 write_int32_le_file (out, 0);
00206
00207 for (i=1; i <= pheight; i++)
00208 {
00209 fwrite (data+(pheight-i)*datawidth, datawidth, 1, out);
00210 if ((padding_size&3) > 0)
00211 {
00212 int cntr;
00213 for (cntr=0;cntr<padding_size;cntr++)
00214 fputc (0, out);
00215 }
00216 }
00217
00218 fclose (out);
00219 return 0;
00220 }
00221
00222
00228 int read_palette_rgb(unsigned char *palette, const char *fname, unsigned int nColors)
00229 {
00230 FILE *palfp;
00231 palfp = fopen (fname, "rb");
00232 if (!palfp) return 1;
00233 int palSize=3*nColors;
00234 int readed=fread (palette, 1, palSize, palfp);
00235 fclose(palfp);
00236 if (palSize!=readed) return 2;
00237 return 0;
00238 }
00239
00243 unsigned int rnd(const unsigned int range)
00244 {
00245 return (rand()%(range));
00246 }
00247
00255 int rnc_compressed_buf (unsigned char *buff)
00256 {
00257 if (strncmp(buff,RNC_SIGNATURE_STR,3)!=0)
00258 return 0;
00259 int rncver=buff[3];
00260 if (rncver==0) rncver=255;
00261 return rncver;
00262 }
00263
00272 int rnc_compressed_file (FILE *fp)
00273 {
00274 unsigned char buff[5];
00275 long lastpos = ftell (fp);
00276 fseek (fp, 0, SEEK_SET);
00277 int readed=fread (buff, 1, 4, fp);
00278 fseek (fp, lastpos, SEEK_SET);
00279 if (readed<4) return 0;
00280 return rnc_compressed_buf(buff);
00281 }