51 ReductionFactor(reduction_factor),
57 strncpy(Name,name,
sizeof(Name));
70 int result=file->
Open();
80 unsigned read_bytes=file->
Read(header,4);
83 WWASSERT(
"File loading failed trying to read header\n");
89 if (read_bytes==0 || read_bytes!=SurfaceDesc.Size)
92 tmp.
Format(
"File %s loading failed.\nTried to read %d bytes, got %d. (SurfDesc.size=%d)\n",name,
sizeof(
LegacyDDSURFACEDESC2),read_bytes,SurfaceDesc.Size);
105 MipLevels=SurfaceDesc.MipMapCount;
106 if (MipLevels==0) MipLevels=1;
108 if (MipLevels>ReductionFactor) MipLevels-=ReductionFactor;
111 ReductionFactor=ReductionFactor-MipLevels;
115 if (MipLevels>2) MipLevels-=2;
129 FullWidth=SurfaceDesc.Width;
130 FullHeight=SurfaceDesc.Height;
131 FullDepth=SurfaceDesc.Depth;
132 Width=SurfaceDesc.Width>>ReductionFactor;
133 Height=SurfaceDesc.Height>>ReductionFactor;
134 Depth=SurfaceDesc.Depth;
136 unsigned level_size=Calculate_DXTC_Surface_Size
142 unsigned level_offset=0;
144 unsigned level_mip_dec=4;
148 level_size*=SurfaceDesc.Depth;
154 for (
unsigned level=0;level<ReductionFactor;++level)
158 level_size/=level_mip_dec;
161 for (level=0;level<MipLevels;++level)
163 LevelSizes[level]=level_size;
164 LevelOffsets[level]=level_offset;
165 level_offset+=level_size;
168 level_size/=level_mip_dec;
174 for (level=0; level<MipLevels;++level)
176 CubeFaceSize+=LevelSizes[level];
382 unsigned dest_height,
383 unsigned char* dest_surface,
393 WWASSERT_PRINT(DDSMemory,
"Surface mip level pointer is missing\n");
398 bool has_hsv_shift = hsv_shift[0]!=0.0f || hsv_shift[1]!=0.0f || hsv_shift[2]!=0.0f;
399 if (dest_format==Format && dest_width==
Get_Width(level) && dest_height==
Get_Height(level)) {
404 unsigned* dest_ptr=
reinterpret_cast<unsigned*
>(dest_surface);
405 for (
unsigned y=0;y<dest_height;y+=4) {
406 for (
unsigned x=0;x<dest_width;x+=4) {
407 unsigned cols=*src_ptr++;
408 unsigned col0=RGB565_To_ARGB8888(
unsigned short(cols>>16));
409 unsigned col1=RGB565_To_ARGB8888(
unsigned short(cols&0xffff));
412 col0=ARGB8888_To_RGB565(col0);
413 col1=ARGB8888_To_RGB565(col1);
414 cols=unsigned(col0)<<16|col1;
417 *dest_ptr++=*src_ptr++;
423 unsigned* dest_ptr=
reinterpret_cast<unsigned*
>(dest_surface);
424 for (
unsigned y=0;y<dest_height;y+=4) {
425 for (
unsigned x=0;x<dest_width;x+=4) {
426 *dest_ptr++=*src_ptr++;
427 *dest_ptr++=*src_ptr++;
428 unsigned cols=*src_ptr++;
429 unsigned col0=RGB565_To_ARGB8888(
unsigned short(cols>>16));
430 unsigned col1=RGB565_To_ARGB8888(
unsigned short(cols&0xffff));
433 col0=ARGB8888_To_RGB565(col0);
434 col1=ARGB8888_To_RGB565(col1);
435 cols=unsigned(col0)<<16|col1;
438 *dest_ptr++=*src_ptr++;
462 unsigned* dest_ptr=
reinterpret_cast<unsigned*
>(dest_surface);
463 for (
unsigned y=0;y<dest_height;y+=4) {
464 for (
unsigned x=0;x<dest_width;x+=4) {
465 *dest_ptr++=0xffffffff;
466 *dest_ptr++=0xffffffff;
469 unsigned cols=*src_ptr++;
470 unsigned col0=RGB565_To_ARGB8888(
unsigned short(cols>>16));
471 unsigned col1=RGB565_To_ARGB8888(
unsigned short(cols&0xffff));
474 col0=ARGB8888_To_RGB565(col0);
475 col1=ARGB8888_To_RGB565(col1);
476 cols=unsigned(col0)<<16|col1;
479 *dest_ptr++=*src_ptr++;
485 unsigned* dest_ptr=
reinterpret_cast<unsigned*
>(dest_surface);
486 for (
unsigned y=0;y<dest_height;y+=4) {
487 for (
unsigned x=0;x<dest_width;x+=4) {
488 *dest_ptr++=0xffffffff;
489 *dest_ptr++=0xffffffff;
490 *dest_ptr++=*src_ptr++;
491 *dest_ptr++=*src_ptr++;
500 bool contains_alpha=
false;
501 for (
unsigned y=0;y<dest_height;y+=4) {
502 unsigned char* dest_ptr=dest_surface;
503 dest_ptr+=y*dest_pitch;
504 for (
unsigned x=0;x<dest_width;x+=4,dest_ptr+=dest_bpp*4) {
505 contains_alpha|=
Get_4x4_Block(dest_ptr,dest_pitch,dest_format,level,x,y,hsv_shift);
509 WWDEBUG_SAY((
"Warning: DXT1 format should not contain alpha information - file %s\n",Name));
533 unsigned dest_height,
534 unsigned char* dest_surface,
544 WWASSERT_PRINT(DDSMemory,
"Surface mip level pointer is missing\n");
549 bool has_hsv_shift = hsv_shift[0]!=0.0f || hsv_shift[1]!=0.0f || hsv_shift[2]!=0.0f;
558 unsigned* dest_ptr=
reinterpret_cast<unsigned*
>(dest_surface);
559 for (
unsigned y=0;y<dest_height;y+=4)
561 for (
unsigned x=0;x<dest_width;x+=4)
563 unsigned cols=*src_ptr++;
564 unsigned col0=RGB565_To_ARGB8888(
unsigned short(cols>>16));
565 unsigned col1=RGB565_To_ARGB8888(
unsigned short(cols&0xffff));
568 col0=ARGB8888_To_RGB565(col0);
569 col1=ARGB8888_To_RGB565(col1);
570 cols=unsigned(col0)<<16|col1;
573 *dest_ptr++=*src_ptr++;
580 unsigned* dest_ptr=
reinterpret_cast<unsigned*
>(dest_surface);
581 for (
unsigned y=0;y<dest_height;y+=4)
583 for (
unsigned x=0;x<dest_width;x+=4)
585 *dest_ptr++=*src_ptr++;
586 *dest_ptr++=*src_ptr++;
587 unsigned cols=*src_ptr++;
588 unsigned col0=RGB565_To_ARGB8888(
unsigned short(cols>>16));
589 unsigned col1=RGB565_To_ARGB8888(
unsigned short(cols&0xffff));
592 col0=ARGB8888_To_RGB565(col0);
593 col1=ARGB8888_To_RGB565(col1);
594 cols=unsigned(col0)<<16|col1;
597 *dest_ptr++=*src_ptr++;
627 unsigned* dest_ptr=
reinterpret_cast<unsigned*
>(dest_surface);
628 for (
unsigned y=0;y<dest_height;y+=4)
630 for (
unsigned x=0;x<dest_width;x+=4)
632 *dest_ptr++=0xffffffff;
633 *dest_ptr++=0xffffffff;
636 unsigned cols=*src_ptr++;
637 unsigned col0=RGB565_To_ARGB8888(
unsigned short(cols>>16));
638 unsigned col1=RGB565_To_ARGB8888(
unsigned short(cols&0xffff));
641 col0=ARGB8888_To_RGB565(col0);
642 col1=ARGB8888_To_RGB565(col1);
643 cols=unsigned(col0)<<16|col1;
646 *dest_ptr++=*src_ptr++;
653 unsigned* dest_ptr=
reinterpret_cast<unsigned*
>(dest_surface);
654 for (
unsigned y=0;y<dest_height;y+=4)
656 for (
unsigned x=0;x<dest_width;x+=4)
658 *dest_ptr++=0xffffffff;
659 *dest_ptr++=0xffffffff;
660 *dest_ptr++=*src_ptr++;
661 *dest_ptr++=*src_ptr++;
671 bool contains_alpha=
false;
672 for (
unsigned y=0;y<dest_height;y+=4)
674 unsigned char* dest_ptr=dest_surface;
675 dest_ptr+=y*dest_pitch;
676 for (
unsigned x=0;x<dest_width;x+=4,dest_ptr+=dest_bpp*4)
678 contains_alpha|=
Get_4x4_Block(dest_ptr,dest_pitch,dest_format,level,x,y,hsv_shift);
683 WWDEBUG_SAY((
"Warning: DXT1 format should not contain alpha information - file %s\n",Name));
702 unsigned dest_height,
703 unsigned char* dest_surface,
705 unsigned slice_pitch,
714 WWASSERT_PRINT(DDSMemory,
"Surface mip level pointer is missing\n");
722 bool has_hsv_shift = hsv_shift[0]!=0.0f || hsv_shift[1]!=0.0f || hsv_shift[2]!=0.0f;
731 unsigned* dest_ptr=
reinterpret_cast<unsigned*
>(dest_surface);
732 for (
unsigned y=0;y<dest_height;y+=4)
734 for (
unsigned x=0;x<dest_width;x+=4)
736 unsigned cols=*src_ptr++;
737 unsigned col0=RGB565_To_ARGB8888(
unsigned short(cols>>16));
738 unsigned col1=RGB565_To_ARGB8888(
unsigned short(cols&0xffff));
741 col0=ARGB8888_To_RGB565(col0);
742 col1=ARGB8888_To_RGB565(col1);
743 cols=unsigned(col0)<<16|col1;
746 *dest_ptr++=*src_ptr++;
753 unsigned* dest_ptr=
reinterpret_cast<unsigned*
>(dest_surface);
754 for (
unsigned y=0;y<dest_height;y+=4)
756 for (
unsigned x=0;x<dest_width;x+=4)
758 *dest_ptr++=*src_ptr++;
759 *dest_ptr++=*src_ptr++;
760 unsigned cols=*src_ptr++;
761 unsigned col0=RGB565_To_ARGB8888(
unsigned short(cols>>16));
762 unsigned col1=RGB565_To_ARGB8888(
unsigned short(cols&0xffff));
765 col0=ARGB8888_To_RGB565(col0);
766 col1=ARGB8888_To_RGB565(col1);
767 cols=unsigned(col0)<<16|col1;
770 *dest_ptr++=*src_ptr++;
800 unsigned* dest_ptr=
reinterpret_cast<unsigned*
>(dest_surface);
801 for (
unsigned y=0;y<dest_height;y+=4)
803 for (
unsigned x=0;x<dest_width;x+=4)
805 *dest_ptr++=0xffffffff;
806 *dest_ptr++=0xffffffff;
809 unsigned cols=*src_ptr++;
810 unsigned col0=RGB565_To_ARGB8888(
unsigned short(cols>>16));
811 unsigned col1=RGB565_To_ARGB8888(
unsigned short(cols&0xffff));
814 col0=ARGB8888_To_RGB565(col0);
815 col1=ARGB8888_To_RGB565(col1);
816 cols=unsigned(col0)<<16|col1;
819 *dest_ptr++=*src_ptr++;
826 unsigned* dest_ptr=
reinterpret_cast<unsigned*
>(dest_surface);
827 for (
unsigned y=0;y<dest_height;y+=4)
829 for (
unsigned x=0;x<dest_width;x+=4)
831 *dest_ptr++=0xffffffff;
832 *dest_ptr++=0xffffffff;
833 *dest_ptr++=*src_ptr++;
834 *dest_ptr++=*src_ptr++;
929 unsigned col0=RGB565_To_ARGB8888(*(
unsigned short*)&block_memory[0]);
930 unsigned col1=RGB565_To_ARGB8888(*(
unsigned short*)&block_memory[2]);
931 unsigned char line=block_memory[4+(y%4)];
936 case 0:
return col0|0xff000000;
937 case 1:
return col1|0xff000000;
938 case 2:
return Combine_Colors(col1,col0,85)|0xff000000;
939 case 3:
return Combine_Colors(col0,col1,85)|0xff000000;
944 case 0:
return col0|0xff000000;
945 case 1:
return col1|0xff000000;
946 case 2:
return Combine_Colors(col1,col0,128)|0xff000000;
947 case 3:
return 0x00000000;
962 unsigned alpha0=alpha_block[0];
963 unsigned alpha1=alpha_block[1];
965 unsigned bit_idx=((x%4)+4*(y%4))*3;
966 unsigned byte_idx=bit_idx/8;
968 unsigned alpha_index=0;
969 for (
int i=0;i<3;++i) {
971 unsigned alpha_bit=(alpha_block[2+byte_idx]>>(bit_idx))&1;
972 alpha_index|=alpha_bit<<(i);
982 unsigned alpha_value=0;
986 switch (alpha_index) {
987 case 0: alpha_value=alpha0;
break;
988 case 1: alpha_value=alpha1;
break;
989 case 2: alpha_value=(6*alpha0+1*alpha1+3) / 7;
break;
990 case 3: alpha_value=(5*alpha0+2*alpha1+3) / 7;
break;
991 case 4: alpha_value=(4*alpha0+3*alpha1+3) / 7;
break;
992 case 5: alpha_value=(3*alpha0+4*alpha1+3) / 7;
break;
993 case 6: alpha_value=(2*alpha0+5*alpha1+3) / 7;
break;
994 case 7: alpha_value=(1*alpha0+6*alpha1+3) / 7;
break;
1000 switch (alpha_index) {
1001 case 0: alpha_value=alpha0;
break;
1002 case 1: alpha_value=alpha1;
break;
1003 case 2: alpha_value=(4*alpha0+1*alpha1+2) / 5;
break;
1004 case 3: alpha_value=(3*alpha0+2*alpha1+2) / 5;
break;
1005 case 4: alpha_value=(2*alpha0+3*alpha1+2) / 5;
break;
1006 case 5: alpha_value=(1*alpha0+4*alpha1+2) / 5;
break;
1007 case 6: alpha_value=0;
break;
1008 case 7: alpha_value=255;
break;
1015 const unsigned char* color_block=alpha_block+8;
1016 unsigned col0=RGB565_To_ARGB8888(*(
unsigned short*)&color_block[0]);
1017 unsigned col1=RGB565_To_ARGB8888(*(
unsigned short*)&color_block[2]);
1018 unsigned char line=color_block[4+(y%4)];
1022 case 0:
return col0|alpha_value;
1023 case 1:
return col1|alpha_value;
1024 case 2:
return Combine_Colors(col1,col0,85)|alpha_value;
1025 case 3:
return Combine_Colors(col0,col1,85)|alpha_value;
1044 unsigned char* dest_ptr,
1045 unsigned dest_pitch,
1050 const Vector3& hsv_shift)
const
1063 bool has_hsv_shift = hsv_shift[0]!=0.0f || hsv_shift[1]!=0.0f || hsv_shift[2]!=0.0f;
1074 unsigned col0=RGB565_To_ARGB8888(*(
unsigned short*)&block_memory[0]);
1075 unsigned col1=RGB565_To_ARGB8888(*(
unsigned short*)&block_memory[2]);
1077 unsigned dest_pixel=0;
1079 if (has_hsv_shift) {
1084 for (
int y=0;y<4;++y) {
1085 unsigned char* tmp_dest_ptr=dest_ptr;
1086 dest_ptr+=dest_pitch;
1087 unsigned char line=block_memory[4+y];
1088 for (
int x=0;x<4;++x) {
1090 case 0: dest_pixel=col0|0xff000000;
break;
1091 case 1: dest_pixel=col1|0xff000000;
break;
1092 case 2: dest_pixel=Combine_Colors(col1,col0,85)|0xff000000;
break;
1093 case 3: dest_pixel=Combine_Colors(col0,col1,85)|0xff000000;
break;
1098 tmp_dest_ptr+=dest_bpp;
1104 if (has_hsv_shift) {
1108 bool contains_alpha=
false;
1109 for (
int y=0;y<4;++y) {
1110 unsigned char* tmp_dest_ptr=dest_ptr;
1111 dest_ptr+=dest_pitch;
1112 unsigned char line=block_memory[4+y];
1113 for (
int x=0;x<4;++x) {
1115 case 0: dest_pixel=col0|0xff000000;
break;
1116 case 1: dest_pixel=col1|0xff000000;
break;
1117 case 2: dest_pixel=Combine_Colors(col1,col0,128)|0xff000000;
break;
1118 case 3: dest_pixel=0x00000000; contains_alpha=
true;
break;
1123 tmp_dest_ptr+=dest_bpp;
1126 return contains_alpha;
1142 alphas[0]=alpha_block[0];
1143 alphas[1]=alpha_block[1];
1146 if (alphas[0]>alphas[1]) {
1147 alphas[2]=(6*alphas[0]+1*alphas[1]+3) / 7;
1148 alphas[3]=(5*alphas[0]+2*alphas[1]+3) / 7;
1149 alphas[4]=(4*alphas[0]+3*alphas[1]+3) / 7;
1150 alphas[5]=(3*alphas[0]+4*alphas[1]+3) / 7;
1151 alphas[6]=(2*alphas[0]+5*alphas[1]+3) / 7;
1152 alphas[7]=(1*alphas[0]+6*alphas[1]+3) / 7;
1155 alphas[2]=(4*alphas[0]+1*alphas[1]+2) / 5;
1156 alphas[3]=(3*alphas[0]+2*alphas[1]+2) / 5;
1157 alphas[4]=(2*alphas[0]+3*alphas[1]+2) / 5;
1158 alphas[5]=(1*alphas[0]+4*alphas[1]+2) / 5;
1164 const unsigned char* color_block=alpha_block+8;
1165 unsigned col0=RGB565_To_ARGB8888(*(
unsigned short*)&color_block[0]);
1166 unsigned col1=RGB565_To_ARGB8888(*(
unsigned short*)&color_block[2]);
1167 if (has_hsv_shift) {
1172 unsigned dest_pixel=0;
1174 unsigned contains_alpha=0xff;
1176 unsigned alpha_indices[16];
1177 unsigned* ai_ptr=alpha_indices;
1178 for (
int a=0;a<2;++a) {
1179 ai_ptr[0]=alpha_block[2]&0x7;
1180 ai_ptr[1]=(alpha_block[2]>>3)&0x7;
1181 ai_ptr[2]=(alpha_block[2]>>6)|((alpha_block[3]&1)<<2);
1182 ai_ptr[3]=(alpha_block[3]>>1)&0x7;
1183 ai_ptr[4]=(alpha_block[3]>>4)&0x7;
1184 ai_ptr[5]=(alpha_block[3]>>7)|((alpha_block[4]&3)<<1);
1185 ai_ptr[6]=(alpha_block[4]>>2)&0x7;
1186 ai_ptr[7]=(alpha_block[4]>>5);
1192 for (
int y=0;y<4;++y) {
1193 unsigned char* tmp_dest_ptr=dest_ptr;
1194 dest_ptr+=dest_pitch;
1195 unsigned char line=color_block[4+y];
1196 for (
int x=0;x<4;++x,bit_idx+=3) {
1197 unsigned alpha_value=alphas[alpha_indices[aii++]];
1198 contains_alpha&=alpha_value;
1204 case 0: dest_pixel=col0|alpha_value;
break;
1205 case 1: dest_pixel=col1|alpha_value;
break;
1206 case 2: dest_pixel=Combine_Colors(col1,col0,85)|alpha_value;
break;
1207 case 3: dest_pixel=Combine_Colors(col0,col1,85)|alpha_value;
break;
1212 tmp_dest_ptr+=dest_bpp;
1258 return contains_alpha!=0xff;
bool Get_4x4_Block(unsigned char *dest_ptr, unsigned dest_pitch, WW3DFormat dest_format, unsigned level, unsigned source_x, unsigned source_y, const Vector3 &hsv_shift=Vector3(0.0f, 0.0f, 0.0f)) const
void Copy_CubeMap_Level_To_Surface(unsigned face, unsigned level, WW3DFormat dest_format, unsigned width, unsigned height, unsigned char *surf, unsigned pitch, const Vector3 &hsv_shift=Vector3(0.0f, 0.0f, 0.0f))
void Copy_Volume_Level_To_Surface(unsigned level, unsigned depth, WW3DFormat dest_format, unsigned width, unsigned height, unsigned char *vol, unsigned row_pitch, unsigned slice_pitch, const Vector3 &hsv_shift=Vector3(0.0f, 0.0f, 0.0f))