comparison os2/dw.c @ 1698:e31c7e389b2f

More fixes to the GBM code on OS/2... if loading a file fails... Keep trying all the different file types, and all the extensions. This works for most files now but seems to be failing with PNG for some reason with the error unable to read file.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Thu, 03 May 2012 08:31:22 +0000
parents f5b187156cc0
children df7ca5c4fa5b
comparison
equal deleted inserted replaced
1697:f5b187156cc0 1698:e31c7e389b2f
74 } GBM; 74 } GBM;
75 typedef struct { unsigned char r, g, b; } GBMRGB; 75 typedef struct { unsigned char r, g, b; } GBMRGB;
76 #pragma pack() 76 #pragma pack()
77 int (API_FUNC _gbm_init)(void) = 0; 77 int (API_FUNC _gbm_init)(void) = 0;
78 int (API_FUNC _gbm_deinit)(void) = 0; 78 int (API_FUNC _gbm_deinit)(void) = 0;
79 int (API_FUNC _gbm_guess_filetype)(const char *fn, int *type) = 0; 79 int (API_FUNC _gbm_query_n_filetypes)(int *count) = 0;
80 int (API_FUNC _gbm_io_open)(const char *fn, int mode) = 0; 80 int (API_FUNC _gbm_io_open)(const char *fn, int mode) = 0;
81 int (API_FUNC _gbm_io_close)(int fd) = 0; 81 int (API_FUNC _gbm_io_close)(int fd) = 0;
82 int (API_FUNC _gbm_read_header)(const char *fn, int fd, int ft, GBM *gbm, const char *info) = 0; 82 int (API_FUNC _gbm_read_header)(const char *fn, int fd, int ft, GBM *gbm, const char *info) = 0;
83 int (API_FUNC _gbm_read_palette)(int fd, int ft, GBM *gbm, GBMRGB *gbmrgb) = 0; 83 int (API_FUNC _gbm_read_palette)(int fd, int ft, GBM *gbm, GBMRGB *gbmrgb) = 0;
84 int (API_FUNC _gbm_read_data)(int fd, int ft, GBM *gbm, unsigned char *data) = 0; 84 int (API_FUNC _gbm_read_data)(int fd, int ft, GBM *gbm, unsigned char *data) = 0;
85 85 const char * (API_FUNC _gbm_err)(int rc) = 0;
86 /* 86 /*
87 * List those icons that have transparency first 87 * GBM List of supported formats: BMP, PNG, JPEG, Targa, TIFF and XPM.
88 * GDI+ List of supported formats: BMP, ICON, GIF, JPEG, Exif, PNG, TIFF, WMF, and EMF.
89 * Not sure if we should include all these or not... maybe we should add TIFF and GIF?
90 */ 88 */
91 #define NUM_EXTS 8 89 #define NUM_EXTS 8
92 char *image_exts[NUM_EXTS] = 90 char *image_exts[NUM_EXTS] =
93 { 91 {
94 ".bmp", 92 ".bmp",
4147 if(!DosLoadModule((PSZ)objnamebuf, sizeof(objnamebuf), (PSZ)"PMMERGE", &pmmerge)) 4145 if(!DosLoadModule((PSZ)objnamebuf, sizeof(objnamebuf), (PSZ)"PMMERGE", &pmmerge))
4148 DosQueryProcAddr(pmmerge, 5469, NULL, (PFN*)&_WinQueryDesktopWorkArea); 4146 DosQueryProcAddr(pmmerge, 5469, NULL, (PFN*)&_WinQueryDesktopWorkArea);
4149 if(!DosLoadModule((PSZ)objnamebuf, sizeof(objnamebuf), (PSZ)"GBM", &gbm)) 4147 if(!DosLoadModule((PSZ)objnamebuf, sizeof(objnamebuf), (PSZ)"GBM", &gbm))
4150 { 4148 {
4151 /* Load the _System versions of the functions from the library */ 4149 /* Load the _System versions of the functions from the library */
4150 DosQueryProcAddr(gbm, 0, (PSZ)"Gbm_err", (PFN*)&_gbm_err);
4152 DosQueryProcAddr(gbm, 0, (PSZ)"Gbm_init", (PFN*)&_gbm_init); 4151 DosQueryProcAddr(gbm, 0, (PSZ)"Gbm_init", (PFN*)&_gbm_init);
4153 DosQueryProcAddr(gbm, 0, (PSZ)"Gbm_deinit", (PFN*)&_gbm_deinit); 4152 DosQueryProcAddr(gbm, 0, (PSZ)"Gbm_deinit", (PFN*)&_gbm_deinit);
4154 DosQueryProcAddr(gbm, 0, (PSZ)"Gbm_io_open", (PFN*)&_gbm_io_open); 4153 DosQueryProcAddr(gbm, 0, (PSZ)"Gbm_io_open", (PFN*)&_gbm_io_open);
4155 DosQueryProcAddr(gbm, 0, (PSZ)"Gbm_io_close", (PFN*)&_gbm_io_close); 4154 DosQueryProcAddr(gbm, 0, (PSZ)"Gbm_io_close", (PFN*)&_gbm_io_close);
4156 DosQueryProcAddr(gbm, 0, (PSZ)"Gbm_read_data", (PFN*)&_gbm_read_data); 4155 DosQueryProcAddr(gbm, 0, (PSZ)"Gbm_read_data", (PFN*)&_gbm_read_data);
4157 DosQueryProcAddr(gbm, 0, (PSZ)"Gbm_read_header", (PFN*)&_gbm_read_header); 4156 DosQueryProcAddr(gbm, 0, (PSZ)"Gbm_read_header", (PFN*)&_gbm_read_header);
4158 DosQueryProcAddr(gbm, 0, (PSZ)"Gbm_read_palette", (PFN*)&_gbm_read_palette); 4157 DosQueryProcAddr(gbm, 0, (PSZ)"Gbm_read_palette", (PFN*)&_gbm_read_palette);
4159 DosQueryProcAddr(gbm, 0, (PSZ)"Gbm_guess_filetype", (PFN*)&_gbm_guess_filetype); 4158 DosQueryProcAddr(gbm, 0, (PSZ)"Gbm_query_n_filetypes", (PFN*)&_gbm_query_n_filetypes);
4160 /* If we got the functions, try to initialize the library */ 4159 /* If we got the functions, try to initialize the library */
4161 if(!_gbm_init || _gbm_init()) 4160 if(!_gbm_init || _gbm_init())
4162 { 4161 {
4163 /* Otherwise clear out the function pointers */ 4162 /* Otherwise clear out the function pointers */
4164 _gbm_init=0;_gbm_deinit=0;_gbm_io_open=0;_gbm_io_close=0;_gbm_guess_filetype=0; 4163 _gbm_init=0;_gbm_deinit=0;_gbm_io_open=0;_gbm_io_close=0;_gbm_query_n_filetypes=0;
4165 _gbm_read_header=0;_gbm_read_palette=0;_gbm_read_data=0; 4164 _gbm_read_header=0;_gbm_read_palette=0;_gbm_read_data=0;_gbm_err=0;
4166 } 4165 }
4167 } 4166 }
4168 return rc; 4167 return rc;
4169 } 4168 }
4170 4169
6754 HDC hdc1; 6753 HDC hdc1;
6755 6754
6756 /* If we have GBM support open the file using GBM */ 6755 /* If we have GBM support open the file using GBM */
6757 if(_gbm_init) 6756 if(_gbm_init)
6758 { 6757 {
6759 int fd, ft = 0; 6758 int fd, z, err = -1, ft = 0;
6760 GBM gbm; 6759 GBM gbm;
6761 GBMRGB *gbmrgb; 6760 GBMRGB *gbmrgb;
6762 ULONG byteswidth; 6761 ULONG byteswidth;
6763 6762
6764 /* Try to open the file */ 6763 /* Try to open the file */
6765 if((fd = _gbm_io_open(file, O_RDONLY|O_BINARY)) == -1) 6764 if((fd = _gbm_io_open(file, O_RDONLY|O_BINARY)) == -1)
6766 return 0; 6765 return 0;
6767 6766
6768 /* guess the source file type from the source filename */ 6767 /* guess the source file type from the source filename */
6769 _gbm_guess_filetype(file, &ft); 6768 _gbm_query_n_filetypes(&ft);
6770 6769
6771 /* Read the file header */ 6770 for(z=0;z<ft;z++)
6772 if(_gbm_read_header(file, fd, ft, &gbm, ""))
6773 { 6771 {
6772 /* Read the file header */
6773 if((err = _gbm_read_header(file, fd, z, &gbm, "")) == 0 && gbm.bpp > 1)
6774 break;
6775 }
6776
6777 /* If we failed to load the header */
6778 if(err)
6779 {
6780 dw_debug("GBM: Read header type %d \"%s\" %d %s\n", z, file, err, _gbm_err(err));
6774 _gbm_io_close(fd); 6781 _gbm_io_close(fd);
6775 return 0; 6782 return 0;
6776 } 6783 }
6777 6784
6778 /* if less than 24-bit, then have palette */ 6785 /* if less than 24-bit, then have palette */
6779 if(gbm.bpp < 24) 6786 if(gbm.bpp < 24)
6780 { 6787 {
6781 gbmrgb = alloca(sizeof(GBMRGB)); 6788 gbmrgb = alloca(sizeof(GBMRGB));
6782 /* Read the palette from the file */ 6789 /* Read the palette from the file */
6783 if(_gbm_read_palette(fd, ft, &gbm, gbmrgb)) 6790 if((err = _gbm_read_palette(fd, z, &gbm, gbmrgb)) != 0)
6784 { 6791 {
6792 dw_debug("GBM: Read palette type %d \"%s\" %d %s\n", z, file, err, _gbm_err(err));
6785 _gbm_io_close(fd); 6793 _gbm_io_close(fd);
6786 return 0; 6794 return 0;
6787 } 6795 }
6788 } 6796 }
6789 else 6797 else
6791 6799
6792 /* Save the dimension for return */ 6800 /* Save the dimension for return */
6793 *width = gbm.w; 6801 *width = gbm.w;
6794 *height = gbm.h; 6802 *height = gbm.h;
6795 byteswidth = (((gbm.w*gbm.bpp + 31)/32)*4); 6803 byteswidth = (((gbm.w*gbm.bpp + 31)/32)*4);
6804 /*dw_debug("Read header %dx%d bpp %d bytes wide %d bytes total %d \"%s\"\n", *width, *height, gbm.bpp, byteswidth, byteswidth * gbm.h, file);*/
6805
6796 /* Allocate a buffer to store the image */ 6806 /* Allocate a buffer to store the image */
6797 DosAllocMem((PPVOID)&BitmapFileBegin, (ULONG)byteswidth * gbm.h, 6807 DosAllocMem((PPVOID)&BitmapFileBegin, (ULONG)byteswidth * gbm.h,
6798 PAG_READ | PAG_WRITE | PAG_COMMIT); 6808 PAG_READ | PAG_WRITE | PAG_COMMIT);
6799 6809
6800 /* Read the data into our buffer */ 6810 /* Read the data into our buffer */
6801 if(_gbm_read_data(fd, ft, &gbm, BitmapFileBegin)) 6811 if((err = _gbm_read_data(fd, z, &gbm, BitmapFileBegin)) != 0)
6802 { 6812 {
6813 dw_debug("GBM: Read data type %d \"%s\" %d %s\n", z, file, err, _gbm_err(err));
6803 _gbm_io_close(fd); 6814 _gbm_io_close(fd);
6815 DosFreeMem(BitmapFileBegin);
6804 return 0; 6816 return 0;
6805 } 6817 }
6806 6818
6807 /* Close the file */ 6819 /* Close the file */
6808 _gbm_io_close(fd); 6820 _gbm_io_close(fd);
6944 hps = WinGetPS( handle ); 6956 hps = WinGetPS( handle );
6945 hbm = GpiLoadBitmap( hps, NULLHANDLE, id, 0, 0 ); 6957 hbm = GpiLoadBitmap( hps, NULLHANDLE, id, 0, 0 );
6946 } 6958 }
6947 else if ( filename ) 6959 else if ( filename )
6948 { 6960 {
6949 HDC hdc; 6961 HDC hdc = 0;
6950 unsigned long width, height; 6962 unsigned long width, height;
6951 char *file = alloca(strlen(filename) + 6); 6963 char *file = alloca(strlen(filename) + 6);
6952 6964
6953 if(!file) 6965 if(!file)
6954 return; 6966 return;
6963 /* Try with supported extensions */ 6975 /* Try with supported extensions */
6964 for(z=0;z<(_gbm_init?NUM_EXTS:1);z++) 6976 for(z=0;z<(_gbm_init?NUM_EXTS:1);z++)
6965 { 6977 {
6966 strcpy(file, filename); 6978 strcpy(file, filename);
6967 strcat(file, image_exts[z]); 6979 strcat(file, image_exts[z]);
6968 if(access(file, 04) == 0) 6980 if(access(file, 04) == 0 &&
6981 _load_bitmap_file(file, handle, &hbm, &hdc, &hps, &width, &height))
6969 break; 6982 break;
6970 } 6983 }
6971 } 6984 }
6972 6985
6973 if(!_load_bitmap_file(file, handle, &hbm, &hdc, &hps, &width, &height)) 6986 if(!hdc)
6974 return; 6987 return;
6975 6988
6976 dw_window_set_data(handle, "_dw_hps", (void *)hps); 6989 dw_window_set_data(handle, "_dw_hps", (void *)hps);
6977 dw_window_set_data(handle, "_dw_hdc", (void *)hdc); 6990 dw_window_set_data(handle, "_dw_hdc", (void *)hdc);
6978 dw_window_set_data(handle, "_dw_width", (void *)width); 6991 dw_window_set_data(handle, "_dw_width", (void *)width);
10409 /* Try with supported extensions */ 10422 /* Try with supported extensions */
10410 for(z=0;z<(_gbm_init?NUM_EXTS:1);z++) 10423 for(z=0;z<(_gbm_init?NUM_EXTS:1);z++)
10411 { 10424 {
10412 strcpy(file, filename); 10425 strcpy(file, filename);
10413 strcat(file, image_exts[z]); 10426 strcat(file, image_exts[z]);
10414 if(access(file, 04) == 0) 10427 if(access(file, 04) == 0 &&
10428 _load_bitmap_file(file, handle, &pixmap->hbm, &pixmap->hdc, &pixmap->hps, &pixmap->width, &pixmap->height))
10415 break; 10429 break;
10416 } 10430 }
10417 } 10431 }
10418 10432
10419 /* Try to load the bitmap from file */ 10433 /* Try to load the bitmap from file */
10420 if ( !_load_bitmap_file(file, handle, &pixmap->hbm, &pixmap->hdc, &pixmap->hps, &pixmap->width, &pixmap->height) ) 10434 if(!pixmap->hbm)
10421 { 10435 {
10422 free(pixmap); 10436 free(pixmap);
10423 return NULL; 10437 return NULL;
10424 } 10438 }
10425 10439