OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2009, Google Inc. | 2 * Copyright 2009, Google Inc. |
3 * All rights reserved. | 3 * All rights reserved. |
4 * | 4 * |
5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
6 * modification, are permitted provided that the following conditions are | 6 * modification, are permitted provided that the following conditions are |
7 * met: | 7 * met: |
8 * | 8 * |
9 * * Redistributions of source code must retain the above copyright | 9 * * Redistributions of source code must retain the above copyright |
10 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
59 // [0-1] color0. | 59 // [0-1] color0. |
60 // [2-3] color1. | 60 // [2-3] color1. |
61 // [4-7] color bitmap, 2 bits per pixel. | 61 // [4-7] color bitmap, 2 bits per pixel. |
62 // So each of the 4-7 bytes represents one line, flipping a block is just | 62 // So each of the 4-7 bytes represents one line, flipping a block is just |
63 // flipping those bytes. | 63 // flipping those bytes. |
64 unsigned char tmp = block[4]; | 64 unsigned char tmp = block[4]; |
65 block[4] = block[7]; | 65 block[4] = block[7]; |
66 block[7] = tmp; | 66 block[7] = tmp; |
67 tmp = block[5]; | 67 tmp = block[5]; |
68 block[5] = block[6]; | 68 block[5] = block[6]; |
69 block[6] = block[5]; | 69 block[6] = block[5]; |
ruudvangaal
2009/09/01 12:09:49
should be: block[6]=tmp
| |
70 } | 70 } |
71 | 71 |
72 // Flips the first 2 lines of a DXT1 block in the y direction. | 72 // Flips the first 2 lines of a DXT1 block in the y direction. |
73 static void FlipDXT1BlockHalf(unsigned char *block) { | 73 static void FlipDXT1BlockHalf(unsigned char *block) { |
74 // See layout above. | 74 // See layout above. |
75 unsigned char tmp = block[4]; | 75 unsigned char tmp = block[4]; |
76 block[4] = block[5]; | 76 block[4] = block[5]; |
77 block[5] = tmp; | 77 block[5] = tmp; |
78 } | 78 } |
79 | 79 |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
140 unsigned int line_0_1 = block[2] + 256 * (block[3] + 256 * block[4]); | 140 unsigned int line_0_1 = block[2] + 256 * (block[3] + 256 * block[4]); |
141 unsigned int line_2_3 = block[5] + 256 * (block[6] + 256 * block[7]); | 141 unsigned int line_2_3 = block[5] + 256 * (block[6] + 256 * block[7]); |
142 // swap lines 0 and 1 in line_0_1. | 142 // swap lines 0 and 1 in line_0_1. |
143 unsigned int line_1_0 = ((line_0_1 & 0x000fff) << 12) | | 143 unsigned int line_1_0 = ((line_0_1 & 0x000fff) << 12) | |
144 ((line_0_1 & 0xfff000) >> 12); | 144 ((line_0_1 & 0xfff000) >> 12); |
145 // swap lines 2 and 3 in line_2_3. | 145 // swap lines 2 and 3 in line_2_3. |
146 unsigned int line_3_2 = ((line_2_3 & 0x000fff) << 12) | | 146 unsigned int line_3_2 = ((line_2_3 & 0x000fff) << 12) | |
147 ((line_2_3 & 0xfff000) >> 12); | 147 ((line_2_3 & 0xfff000) >> 12); |
148 block[2] = line_3_2 & 0xff; | 148 block[2] = line_3_2 & 0xff; |
149 block[3] = (line_3_2 & 0xff00) >> 8; | 149 block[3] = (line_3_2 & 0xff00) >> 8; |
150 block[4] = (line_3_2 & 0xff0000) >> 8; | 150 block[4] = (line_3_2 & 0xff0000) >> 8; |
ruudvangaal
2009/09/01 13:28:10
Strips all bits to 0; use >>16 instead.
| |
151 block[5] = line_1_0 & 0xff; | 151 block[5] = line_1_0 & 0xff; |
152 block[6] = (line_1_0 & 0xff00) >> 8; | 152 block[6] = (line_1_0 & 0xff00) >> 8; |
153 block[7] = (line_1_0 & 0xff0000) >> 8; | 153 block[7] = (line_1_0 & 0xff0000) >> 8; |
ruudvangaal
2009/09/01 13:28:10
Strips all bits to 0; use >>16 instead.
| |
154 | 154 |
155 // And flip the DXT1 block using the above function. | 155 // And flip the DXT1 block using the above function. |
156 FlipDXT1BlockFull(block + 8); | 156 FlipDXT1BlockFull(block + 8); |
157 } | 157 } |
158 | 158 |
159 // Flips the first 2 lines of a DXT5 block in the y direction. | 159 // Flips the first 2 lines of a DXT5 block in the y direction. |
160 static void FlipDXT5BlockHalf(unsigned char *block) { | 160 static void FlipDXT5BlockHalf(unsigned char *block) { |
161 // See layout above. | 161 // See layout above. |
162 unsigned int line_0_1 = block[2] + 256 * (block[3] + 256 * block[4]); | 162 unsigned int line_0_1 = block[2] + 256 * (block[3] + 256 * block[4]); |
163 unsigned int line_1_0 = ((line_0_1 & 0x000fff) << 12) | | 163 unsigned int line_1_0 = ((line_0_1 & 0x000fff) << 12) | |
164 ((line_0_1 & 0xfff000) >> 12); | 164 ((line_0_1 & 0xfff000) >> 12); |
165 block[2] = line_1_0 & 0xff; | 165 block[2] = line_1_0 & 0xff; |
166 block[3] = (line_1_0 & 0xff00) >> 8; | 166 block[3] = (line_1_0 & 0xff00) >> 8; |
167 block[4] = (line_1_0 & 0xff0000) >> 8; | 167 block[4] = (line_1_0 & 0xff0000) >> 8; |
168 FlipDXT1BlockHalf(block + 8); | 168 FlipDXT1BlockHalf(block + 8); |
169 } | 169 } |
170 | 170 |
171 // Flips a DXTC image, by flipping and swapping DXTC blocks as appropriate. | 171 // Flips a DXTC image, by flipping and swapping DXTC blocks as appropriate. |
172 static void FlipDXTCImage(unsigned int width, | 172 static void FlipDXTCImage(unsigned int width, |
173 unsigned int height, | 173 unsigned int height, |
174 unsigned int levels, | 174 unsigned int levels, |
175 Texture::Format format, | 175 Texture::Format format, |
176 unsigned char *data) { | 176 unsigned char *data) { |
177 DCHECK(Bitmap::CheckImageDimensions(width, height)); | 177 DCHECK(Bitmap::CheckImageDimensions(width, height)); |
178 // Height must be a power-of-two. | 178 // Height must be a power-of-two. |
179 DCHECK_EQ(height & (height - 1), 0); | 179 DCHECK_EQ(height & (height - 1), 0u); |
180 FlipBlockFunction full_block_function = NULL; | 180 FlipBlockFunction full_block_function = NULL; |
181 FlipBlockFunction half_block_function = NULL; | 181 FlipBlockFunction half_block_function = NULL; |
182 unsigned int block_bytes = 0; | 182 unsigned int block_bytes = 0; |
183 switch (format) { | 183 switch (format) { |
184 case Texture::DXT1: | 184 case Texture::DXT1: |
185 full_block_function = FlipDXT1BlockFull; | 185 full_block_function = FlipDXT1BlockFull; |
186 half_block_function = FlipDXT1BlockHalf; | 186 half_block_function = FlipDXT1BlockHalf; |
187 block_bytes = 8; | 187 block_bytes = 8; |
188 break; | 188 break; |
189 case Texture::DXT3: | 189 case Texture::DXT3: |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
334 } | 334 } |
335 if (dds_width != dds_height) { | 335 if (dds_width != dds_height) { |
336 DLOG(ERROR) << "DDS file \"" << filename | 336 DLOG(ERROR) << "DDS file \"" << filename |
337 << "\" is a cube map but doesn't have square dimensions."; | 337 << "\" is a cube map but doesn't have square dimensions."; |
338 return false; | 338 return false; |
339 } | 339 } |
340 } | 340 } |
341 | 341 |
342 // The size of the buffer needed to hold four-component per pixel | 342 // The size of the buffer needed to hold four-component per pixel |
343 // image data, including MIPMaps | 343 // image data, including MIPMaps |
344 int components_per_pixel = 0; | 344 unsigned int components_per_pixel = 0; |
345 bool add_filler_alpha = false; | 345 bool add_filler_alpha = false; |
346 bool rgb_to_bgr = false; | 346 bool rgb_to_bgr = false; |
347 | 347 |
348 Texture::Format format = Texture::UNKNOWN_FORMAT; | 348 Texture::Format format = Texture::UNKNOWN_FORMAT; |
349 bool is_dxtc = false; | 349 bool is_dxtc = false; |
350 | 350 |
351 DDPIXELFORMAT &pixel_format = dd_surface_descriptor.ddpfPixelFormat; | 351 DDPIXELFORMAT &pixel_format = dd_surface_descriptor.ddpfPixelFormat; |
352 | 352 |
353 if (pixel_format.dwFlags & DDPF_FOURCC) { | 353 if (pixel_format.dwFlags & DDPF_FOURCC) { |
354 switch (pixel_format.dwFourCC) { | 354 switch (pixel_format.dwFourCC) { |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
471 } | 471 } |
472 | 472 |
473 // Do pixel conversions on non-DXT images. | 473 // Do pixel conversions on non-DXT images. |
474 if (!is_dxtc) { | 474 if (!is_dxtc) { |
475 DCHECK(components_per_pixel == 3 || components_per_pixel == 4); | 475 DCHECK(components_per_pixel == 3 || components_per_pixel == 4); |
476 unsigned int pixel_count = disk_face_size / components_per_pixel; | 476 unsigned int pixel_count = disk_face_size / components_per_pixel; |
477 for (unsigned int face = 0; face < num_faces; ++face) { | 477 for (unsigned int face = 0; face < num_faces; ++face) { |
478 unsigned char *data = image_data.get() + face_size * face; | 478 unsigned char *data = image_data.get() + face_size * face; |
479 // convert to four components per pixel if necessary | 479 // convert to four components per pixel if necessary |
480 if (add_filler_alpha) { | 480 if (add_filler_alpha) { |
481 DCHECK_EQ(components_per_pixel, 3); | 481 DCHECK_EQ(components_per_pixel, 3u); |
482 XYZToXYZA(data, pixel_count); | 482 XYZToXYZA(data, pixel_count); |
483 } else { | 483 } else { |
484 DCHECK_EQ(components_per_pixel, 4); | 484 DCHECK_EQ(components_per_pixel, 4u); |
485 } | 485 } |
486 if (rgb_to_bgr) | 486 if (rgb_to_bgr) |
487 RGBAToBGRA(data, pixel_count); | 487 RGBAToBGRA(data, pixel_count); |
488 } | 488 } |
489 } | 489 } |
490 | 490 |
491 if (!is_cubemap) { | 491 if (!is_cubemap) { |
492 // NOTE: we flip the images to respect max/maya's UV orientation. | 492 // NOTE: we flip the images to respect max/maya's UV orientation. |
493 if (is_dxtc) { | 493 if (is_dxtc) { |
494 FlipDXTCImage(dds_width, dds_height, mip_count, format, image_data.get()); | 494 FlipDXTCImage(dds_width, dds_height, mip_count, format, image_data.get()); |
(...skipping 24 matching lines...) Expand all Loading... | |
519 image_data_.swap(image_data); | 519 image_data_.swap(image_data); |
520 format_ = format; | 520 format_ = format; |
521 width_ = dds_width; | 521 width_ = dds_width; |
522 height_ = dds_height; | 522 height_ = dds_height; |
523 num_mipmaps_ = final_mip_count; | 523 num_mipmaps_ = final_mip_count; |
524 is_cubemap_ = is_cubemap; | 524 is_cubemap_ = is_cubemap; |
525 return true; | 525 return true; |
526 } | 526 } |
527 | 527 |
528 } // namespace o3d | 528 } // namespace o3d |
OLD | NEW |