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/16planar.h"
\r
29 *============================================================================
\r
32 static void loadPcxpbufStage1(FILE *file, planar_buf_t *result) {
\r
38 //fprintf(stderr, "\nplanarLoadPcx: ");
\r
39 //fprintf(stderr, "%u ", w++);
\r
40 /* read the header */
\r
41 fread(&head, sizeof(char), sizeof(struct pcxHeader), file);
\r
42 //fprintf(stderr, "%u ", w++);
\r
43 /* get the width and height */
\r
44 result->width = head.xmax - head.xmin + 1;
\r
45 result->height = head.ymax - head.ymin + 1;
\r
46 result->pwidth = result->width / 4 + (result->width%4 ? 1 : 0);
\r
47 //fprintf(stderr, "%u ", w++);
\r
48 /* make sure this is 8bpp */
\r
50 fprintf(stderr, "I only know how to handle 8bpp pcx files!\n");
\r
56 static void loadPcxpbufPalette(FILE *file, planar_buf_t *result) {
\r
60 /* handle the palette */
\r
61 fseek(file, -769, SEEK_END);
\r
63 result->palette = modexNewPal();
\r
64 if(head.version == 5 && val == 12) {
\r
65 /* use the vga palette */
\r
66 for(index=0; !feof(file) && index < PAL_SIZE; index++) {
\r
68 result->palette[index] = val >> 2;
\r
71 /* use the 16 color palette */
\r
72 for(index=0; index<48; index++) {
\r
73 result->palette[index] = head.pal16[index];
\r
78 /* allocates a planar buffer with specified dimensions */
\r
80 pbuf_alloc(word width, word height) {
\r
84 /* allocate the structure and populate sizes */
\r
85 //p=malloc(sizeof(planar_buf_t));
\r
88 p.pwidth = width / 4 + (width%4 ? 1 : 0);
\r
89 //p.pwidth = width / 4 + (width%4 ? 1 : 0);
\r
91 /* allocate the planes */
\r
92 for(i=0; i<4; i++) {
\r
93 p.plane[i] = _fmalloc(p.height * p.pwidth);
\r
99 /* allocates a planar buffer with specified dimensions */
\r
101 pbuf_alloc0(planar_buf_t *p, word width, word height) {
\r
104 /* allocate the structure and populate sizes */
\r
105 p=_fmalloc(sizeof(planar_buf_t));
\r
107 p->height = height;
\r
108 p->pwidth = width / 4 + (width%4 ? 1 : 0);
\r
109 //p.pwidth = width / 4 + (width%4 ? 1 : 0);
\r
111 /* allocate the planes */
\r
112 for(i=0; i<4; i++) {
\r
113 p->plane[i] = _fmalloc(p->height * p->pwidth);
\r
117 /* sparky4's functions~ */
\r
118 planar_buf_t planarLoadPcx(char *filename)
\r
121 planar_buf_t result;
\r
123 int index[4], plane;
\r
129 fprintf(stderr, "\nplanarLoadPcx: ");
\r
130 fprintf(stderr, "%u ", w++);
\r
131 /* open the PCX file for reading */
\r
132 file = fopen(filename, "rb");
\r
133 //fprintf(stderr, "%u ", w++);
\r
135 fprintf(stderr, "Could not open %s for reading.\n", filename);
\r
138 //fprintf(stderr, "%u ", w++);
\r
139 /* load the first part of the pcx file */
\r
140 loadPcxpbufStage1(file, &result);
\r
141 //fprintf(stderr, "%u ", w++);
\r
142 /* allocate the buffer */
\r
143 bufSize = ((dword)result.width * result.height);
\r
144 //result = pbuf_alloc(result.width, result.height);
\r
145 pbuf_alloc0(&result, result.width, result.height);
\r
146 //fprintf(stderr, "%u ", w++);
\r
147 printf("&bufSize=%p\n", &bufSize);
\r
148 printf("&result.data=%p\n", result.plane);
\r
149 printf("Size of block is %zu bytes\n", _msize(result.plane));
\r
150 printf("Size of bufSize is %zu bytes\n", bufSize);
\r
151 printf("Size of result.width is %zu \n", result.width);
\r
152 printf("Size of result.height is %zu \n", result.height);
\r
153 printf("Dimensions of result is %lu\n", (dword)result.width*result.height);
\r
155 if(!result.plane) {
\r
156 fprintf(stderr, "Could not allocate memory for bitmap data.");
\r
160 fprintf(stderr, "read the buffer? %u ", w++);
\r
162 /* read the buffer in */
\r
163 index[0] = 0,index[1]=0,index[2]=0,index[3]=0;
\r
164 /* start on the first plane */
\r
167 /* get the run length and the value */
\r
168 count = fgetc(file);
\r
169 if(0xC0 == (count & 0xC0)) { /* this is the run count */
\r
177 /* write the pixel the specified number of times */
\r
178 //fprintf(stderr, "\nputting in memory~ %u\n", w++);
\r
179 //if(index>=49152) getch();
\r
180 for(; count && (index[0]+index[1]+index[2]+index[3]) < bufSize; count--,index[plane]++) {
\r
181 //fprintf(stderr, "count=%d index=%d plane=%d\n", count, index, plane);
\r
193 //fprintf(stderr, "%d ", result.plane[plane][(index[0]+index[1]+index[2]+index[3])]);
\r
194 // copy to each plane
\r
195 result.plane[plane][index[plane]]=val;
\r
198 } while((index[0]+index[1]+index[2]+index[3]) < bufSize);
\r
199 //++++loadPcxpbufPalette(file, &result);
\r
203 for(pla=0; pla < 4; pla++) {
\r
205 printf("Plane %d\n", pla);
\r
206 for(py=0; py < result.height; py++) {
\r
207 for(px=0; px < result.pwidth; px++) {
\r
208 printf("%02X ", (int) result.plane[pla][i++]);
\r
213 printf("\n\n%s\n", *filename);
\r
214 fprintf(stdout, "count=%d index=%d plane=%d\n", count, index, pla);
\r
222 planarLoadPcxTiles(char *filename, word twidth, word theight) {
\r
225 planar_buf_t result;
\r
228 /* open the PCX file for reading */
\r
229 file = fopen(filename, "rb");
\r
231 printf("Could not open %s for reading.\n", filename);
\r
235 /* load the first part of the pcx file */
\r
236 loadPcxpbufStage1(file, &result);
\r
238 /* get the number of tiles and set up the result structure */
\r
239 ts.twidth = twidth;
\r
240 ts.theight = theight;
\r
241 ts.ntiles = (result.width/twidth) * (result.height/theight);
\r
242 ts.palette = result.palette;
\r
244 /* allocate the pixel storage for the tiles */
\r
245 /*ts.data = malloc(sizeof(byte*) * ts.ntiles);
\r
246 ts.data[0] = malloc(sizeof(byte) * ts.ntiles * twidth * theight);
\r
247 for(i=1; i < ts.ntiles; i++) {
\r
248 ts.data[i] = ts.data[i-1] + twidth * theight;
\r
251 /* finish off the file */
\r
252 //++++loadPcxPalette(file, &result);
\r