Changeset 3098 in orxonox.OLD for orxonox/branches/images/importer
- Timestamp:
- Dec 5, 2004, 4:05:57 AM (20 years ago)
- Location:
- orxonox/branches/images/importer
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
orxonox/branches/images/importer/material.cc
r3097 r3098 17 17 18 18 #include "material.h" 19 19 20 20 21 /** … … 401 402 printf ("Requested tga-image. Trying to Import\n"); 402 403 return loadTGA(imageName, texture); 404 } 405 else if (!strncmp(imageName+strlen(imageName)-4, ".png", 4)) 406 { 407 if (verbose >=2) 408 printf ("Requested png-image. Trying to Import\n"); 409 return loadPNG(imageName, texture); 403 410 } 404 411 else … … 933 940 } 934 941 942 943 /* 944 static int ST_is_power_of_two(unsigned int number) 945 { 946 return (number & (number - 1)) == 0; 947 } 948 */ 949 950 /** 951 \brief reads in a png-file 952 \param pngName the Name of the Image to load 953 \param texture a reference to the Texture to write the image to 954 */ 955 bool Material::loadPNG(const char* pngName, GLuint* texture) 956 { 957 Image* pImage = new Image; 958 959 FILE *PNG_file = fopen(pngName, "rb"); 960 if (PNG_file == NULL) 961 { 962 return 0; 963 } 964 965 GLubyte PNG_header[8]; 966 967 fread(PNG_header, 1, 8, PNG_file); 968 if (png_sig_cmp(PNG_header, 0, 8) != 0) 969 { 970 if (verbose >=2) 971 printf ("Not Recognized as a pngFile\n"); 972 fclose (PNG_file); 973 return 0; 974 } 975 976 png_structp PNG_reader = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); 977 if (PNG_reader == NULL) 978 { 979 fclose(PNG_file); 980 return 0; 981 } 982 983 png_infop PNG_info = png_create_info_struct(PNG_reader); 984 if (PNG_info == NULL) 985 { 986 png_destroy_read_struct(&PNG_reader, NULL, NULL); 987 fclose(PNG_file); 988 return 0; 989 } 990 991 png_infop PNG_end_info = png_create_info_struct(PNG_reader); 992 if (PNG_end_info == NULL) 993 { 994 png_destroy_read_struct(&PNG_reader, &PNG_info, NULL); 995 fclose(PNG_file); 996 return 0; 997 } 998 999 if (setjmp(png_jmpbuf(PNG_reader))) 1000 { 1001 png_destroy_read_struct(&PNG_reader, &PNG_info, &PNG_end_info); 1002 fclose(PNG_file); 1003 return (0); 1004 } 1005 1006 png_init_io(PNG_reader, PNG_file); 1007 png_set_sig_bytes(PNG_reader, 8); 1008 1009 png_read_info(PNG_reader, PNG_info); 1010 1011 pImage->width = png_get_image_width(PNG_reader, PNG_info); 1012 pImage->height = png_get_image_height(PNG_reader, PNG_info); 1013 1014 png_uint_32 bit_depth, color_type; 1015 bit_depth = png_get_bit_depth(PNG_reader, PNG_info); 1016 color_type = png_get_color_type(PNG_reader, PNG_info); 1017 1018 if (color_type == PNG_COLOR_TYPE_PALETTE) 1019 { 1020 png_set_palette_to_rgb(PNG_reader); 1021 } 1022 1023 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) 1024 { 1025 png_set_gray_1_2_4_to_8(PNG_reader); 1026 } 1027 1028 if (color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA) 1029 { 1030 png_set_gray_to_rgb(PNG_reader); 1031 } 1032 1033 if (png_get_valid(PNG_reader, PNG_info, PNG_INFO_tRNS)) 1034 { 1035 png_set_tRNS_to_alpha(PNG_reader); 1036 } 1037 else 1038 { 1039 png_set_filler(PNG_reader, 0xff, PNG_FILLER_AFTER); 1040 } 1041 1042 if (bit_depth == 16) 1043 { 1044 png_set_strip_16(PNG_reader); 1045 } 1046 1047 png_read_update_info(PNG_reader, PNG_info); 1048 1049 pImage->data = (png_byte*)malloc(4 * pImage->width * pImage->height); 1050 png_byte** PNG_rows = (png_byte**)malloc(pImage->height * sizeof(png_byte*)); 1051 1052 unsigned int row; 1053 for (row = 0; row < pImage->height; ++row) 1054 { 1055 PNG_rows[pImage->height - 1 - row] = pImage->data + (row * 4 * pImage->width); 1056 } 1057 1058 png_read_image(PNG_reader, PNG_rows); 1059 1060 free(PNG_rows); 1061 1062 png_destroy_read_struct(&PNG_reader, &PNG_info, &PNG_end_info); 1063 fclose(PNG_file); 1064 1065 /* if (!ST_is_power_of_two(pImage->width) || !ST_is_power_of_two(pImage->height)) 1066 { 1067 free(pImage->data); 1068 return 0; 1069 } 1070 */ 1071 loadTexToGL (pImage, texture); 1072 1073 free(pImage->data); 1074 1075 return true; 1076 } -
orxonox/branches/images/importer/material.h
r3096 r3098 19 19 #include <jpeglib.h> 20 20 } 21 #include <png.h> 21 22 //! Class to handle Materials. 22 23 class Material … … 102 103 bool loadCompressedTGA(const char * filename, FILE * fTGA, GLuint* texture); 103 104 105 bool loadPNG(const char* pngName, GLuint* texture); 104 106 }; 105 107 #endif
Note: See TracChangeset
for help on using the changeset viewer.