1 /* Project 16 Source Code~
\r
2 * Copyright (C) 2012-2015 sparky4 & pngwen & andrius4669
\r
4 * This file is part of Project 16.
\r
6 * Project 16 is free software; you can redistribute it and/or modify
\r
7 * it under the terms of the GNU General Public License as published by
\r
8 * the Free Software Foundation; either version 3 of the License, or
\r
9 * (at your option) any later version.
\r
11 * Project 16 is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with this program. If not, see <http://www.gnu.org/licenses/>, or
\r
18 * write to the Free Software Foundation, Inc., 51 Franklin Street,
\r
19 * Fifth Floor, Boston, MA 02110-1301 USA.
\r
26 #include "src/lib/modex16/planar16.h"
\r
27 #include "src/lib/modex16.h"
\r
29 static struct pcxHeader {
\r
50 static void loadPcxpbufStage1(FILE *file, planar_buf_t *result) {
\r
55 /* read the header */
\r
56 fread(&head, sizeof(char), sizeof(struct pcxHeader), file);
\r
58 /* get the width and height */
\r
59 result->width = head.xmax - head.xmin + 1;
\r
60 result->height = head.ymax - head.ymin + 1;
\r
62 /* make sure this is 8bpp */
\r
64 fprintf(stderr, "I only know how to handle 8bpp pcx files!\n");
\r
70 static void loadPcxpbufPalette(FILE *file, planar_buf_t *result) {
\r
74 /* handle the palette */
\r
75 fseek(file, -769, SEEK_END);
\r
77 result->palette = modexNewPal();
\r
78 if(head.version == 5 && val == 12) {
\r
79 /* use the vga palette */
\r
80 for(index=0; !feof(file) && index < PAL_SIZE; index++) {
\r
82 result->palette[index] = val >> 2;
\r
85 /* use the 16 color palette */
\r
86 for(index=0; index<48; index++) {
\r
87 result->palette[index] = head.pal16[index];
\r
92 /* sparky4's functions~ */
\r
93 planar_buf_t planarLoadPcx(char *filename)
\r
96 planar_buf_t result;
\r
98 int index, plane, x, y;
\r
102 /* open the PCX file for reading */
\r
103 file = fopen(filename, "rb");
\r
105 fprintf(stderr, "Could not open %s for reading.\n", filename);
\r
109 /* load the first part of the pcx file */
\r
110 loadPcxpbufStage1(file, &result);
\r
112 /* allocate the buffer */
\r
113 bufSize = (/*(dword)*/result.width * result.height);
\r
114 //result = pbuf_alloc(result.width, result.height);
\r
115 if(!result.plane[0]) {
\r
116 fprintf(stderr, "Could not allocate memory for bitmap data.");
\r
121 /* read the buffer in */
\r
124 /* get the run length and the value */
\r
125 count = fgetc(file);
\r
126 if(0xC0 == (count & 0xC0)) { /* this is the run count */
\r
134 // start on the first plane
\r
136 /* write the pixel the specified number of times */
\r
137 for(; count && index < bufSize; count--,index++) {
\r
144 // copy to each plane
\r
145 result.plane[plane++][index]=val;
\r
147 } while(index < bufSize);
\r
149 //++++loadPcxpbufPalette(file, &result);
\r
157 planarLoadPcxTiles(char *filename, word twidth, word theight) {
\r
160 planar_buf_t result;
\r
163 /* open the PCX file for reading */
\r
164 file = fopen(filename, "rb");
\r
166 printf("Could not open %s for reading.\n", filename);
\r
170 /* load the first part of the pcx file */
\r
171 loadPcxStage1(file, &result);
\r
173 /* get the number of tiles and set up the result structure */
\r
174 ts.twidth = twidth;
\r
175 ts.theight = theight;
\r
176 ts.ntiles = (result.width/twidth) * (result.height/theight);
\r
177 ts.palette = result.palette;
\r
179 /* allocate the pixel storage for the tiles */
\r
180 /*ts.data = malloc(sizeof(byte*) * ts.ntiles);
\r
181 ts.data[0] = malloc(sizeof(byte) * ts.ntiles * twidth * theight);
\r
182 for(i=1; i < ts.ntiles; i++) {
\r
183 ts.data[i] = ts.data[i-1] + twidth * theight;
\r
186 /* finish off the file */
\r
187 loadPcxPalette(file, &result);
\r