From 481e53fd77f50dc3f44c7368bbdafa9b49f9f697 Mon Sep 17 00:00:00 2001 From: David Rose Date: Thu, 21 Aug 2008 01:21:38 +0000 Subject: [PATCH] small dds optimization --- panda/src/gobj/texture.cxx | 65 +++++++++++++++----------------------- 1 file changed, 26 insertions(+), 39 deletions(-) diff --git a/panda/src/gobj/texture.cxx b/panda/src/gobj/texture.cxx index 4e8a0a495b..aab5d4d174 100644 --- a/panda/src/gobj/texture.cxx +++ b/panda/src/gobj/texture.cxx @@ -3652,16 +3652,8 @@ read_dds_level_bgr8(Texture *tex, const DDSHeader &header, PTA_uchar image = PTA_uchar::empty_array(size); for (int y = y_size - 1; y >= 0; --y) { unsigned char *p = image.p() + y * row_bytes; - for (int x = 0; x < x_size; ++x) { - unsigned int b = (unsigned char)in.get(); - unsigned int g = (unsigned char)in.get(); - unsigned int r = (unsigned char)in.get(); - - store_unscaled_byte(p, b); - store_unscaled_byte(p, g); - store_unscaled_byte(p, r); - } - nassertr(p <= image.p() + size, false); + nassertr(p + row_bytes <= image.p() + size, false); + in.read((char *)p, row_bytes); } tex->set_ram_mipmap_image(n, image); @@ -3686,14 +3678,15 @@ read_dds_level_rgb8(Texture *tex, const DDSHeader &header, PTA_uchar image = PTA_uchar::empty_array(size); for (int y = y_size - 1; y >= 0; --y) { unsigned char *p = image.p() + y * row_bytes; - for (int x = 0; x < x_size; ++x) { - unsigned int r = (unsigned char)in.get(); - unsigned int g = (unsigned char)in.get(); - unsigned int b = (unsigned char)in.get(); + nassertr(p + row_bytes <= image.p() + size, false); + in.read((char *)p, row_bytes); - store_unscaled_byte(p, b); - store_unscaled_byte(p, g); - store_unscaled_byte(p, r); + // Now reverse the r, g, b triples. + for (int x = 0; x < x_size; ++x) { + unsigned char r = p[0]; + p[0] = p[2]; + p[2] = r; + p += 3; } nassertr(p <= image.p() + size, false); } @@ -3720,18 +3713,22 @@ read_dds_level_abgr8(Texture *tex, const DDSHeader &header, PTA_uchar image = PTA_uchar::empty_array(size); for (int y = y_size - 1; y >= 0; --y) { unsigned char *p = image.p() + y * row_bytes; - for (int x = 0; x < x_size; ++x) { - unsigned int r = (unsigned char)in.get(); - unsigned int g = (unsigned char)in.get(); - unsigned int b = (unsigned char)in.get(); - unsigned int a = (unsigned char)in.get(); + in.read((char *)p, row_bytes); - store_unscaled_byte(p, b); - store_unscaled_byte(p, g); - store_unscaled_byte(p, r); - store_unscaled_byte(p, a); + PN_uint32 *pw = (PN_uint32 *)p; + for (int x = 0; x < x_size; ++x) { + PN_uint32 w = *pw; +#ifdef WORDS_BIGENDIAN + // bigendian: convert R, G, B, A to B, G, R, A. + w = ((w & 0xff00) << 16) | ((w & 0xff000000U) >> 16) | (w & 0xff00ff); +#else + // littendian: convert A, B, G, R to to A, R, G, B. + w = ((w & 0xff) << 16) | ((w & 0xff0000) >> 16) | (w & 0xff00ff00U); +#endif + *pw = w; + ++pw; } - nassertr(p <= image.p() + size, false); + nassertr((unsigned char *)pw <= image.p() + size, false); } tex->set_ram_mipmap_image(n, image); @@ -3756,18 +3753,8 @@ read_dds_level_rgba8(Texture *tex, const DDSHeader &header, PTA_uchar image = PTA_uchar::empty_array(size); for (int y = y_size - 1; y >= 0; --y) { unsigned char *p = image.p() + y * row_bytes; - for (int x = 0; x < x_size; ++x) { - unsigned int b = (unsigned char)in.get(); - unsigned int g = (unsigned char)in.get(); - unsigned int r = (unsigned char)in.get(); - unsigned int a = (unsigned char)in.get(); - - store_unscaled_byte(p, b); - store_unscaled_byte(p, g); - store_unscaled_byte(p, r); - store_unscaled_byte(p, a); - } - nassertr(p <= image.p() + size, false); + nassertr(p + row_bytes <= image.p() + size, false); + in.read((char *)p, row_bytes); } tex->set_ram_mipmap_image(n, image);