]> git.vomp.tv Git - vompclient.git/blob - wjpeg.cc
Initial import
[vompclient.git] / wjpeg.cc
1 /*
2     Copyright 2004-2005 Chris Tallon
3
4     This file is part of VOMP.
5
6     VOMP is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     VOMP is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with VOMP; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #include "wjpeg.h"
22
23 int WJpeg::init(char* fileName)
24 {
25   this->fileName = fileName;
26   return 1;
27 }
28
29 void WJpeg::overrideSurface(Surface* newSurface)
30 {
31   surface = newSurface;
32 }
33
34 void WJpeg::draw()
35 {
36   Log* logger = Log::getInstance();
37
38   logger->log("BJpeg", Log::DEBUG, "Here1");
39
40   FILE* infile = fopen(fileName, "r");
41   if (infile == NULL)
42   {
43     logger->log("BJpeg", Log::ERR, "Can't open JPEG");
44     return;
45   }
46   logger->log("BJpeg", Log::DEBUG, "File opened");
47
48   struct jpeg_decompress_struct cinfo;
49   struct jpeg_error_mgr jerr;
50   cinfo.err = jpeg_std_error(&jerr);
51   jpeg_create_decompress(&cinfo);
52   jpeg_stdio_src(&cinfo, infile);
53   jpeg_read_header(&cinfo, TRUE);
54   jpeg_start_decompress(&cinfo);
55
56   logger->log("BJpeg", Log::DEBUG, "JPEG startup done");
57
58   // Init the surface
59   setDimensions(cinfo.output_height, cinfo.output_width);
60
61
62   // MAKE THE 2D ARRAY
63
64   unsigned char* buffer = (unsigned char*)malloc(height * width * 3);
65   logger->log("BJpeg", Log::DEBUG, "Buffer allocated at %p", buffer);
66
67   unsigned char* bufferPointers[height];
68   for(int ps = 0; ps < height; ps++) bufferPointers[ps] = buffer + (ps * width * 3);
69
70   logger->log("BJpeg", Log::DEBUG, "done array check");
71
72   int rowsread = 0;
73   while (cinfo.output_scanline < cinfo.output_height)
74   {
75     rowsread += jpeg_read_scanlines(&cinfo, &bufferPointers[rowsread], height);
76   }
77
78   logger->log("BJpeg", Log::DEBUG, "Done all jpeg_read");
79
80   jpeg_finish_decompress(&cinfo);
81   jpeg_destroy_decompress(&cinfo);
82
83   fclose(infile);
84
85   logger->log("BJpeg", Log::DEBUG, "jpeg shutdown done, x, y %u %u", width, height);
86
87   unsigned int c;
88   int x, y, xoff;
89   unsigned char* p;
90
91   for (y = 0; y < height; y++)
92   {
93     p = bufferPointers[y];
94
95     for (x = 0; x < width; x++)
96     {
97       xoff = x * 3;
98
99       c = (  (0xFF000000        )
100            | (p[xoff    ]  << 16)
101            | (p[xoff + 1]  <<  8)
102            | (p[xoff + 2]       ) );
103
104       surface->drawPixel(screenX + x, screenY + y, c);
105     }
106   }
107
108   free(buffer);
109   logger->log("BJpeg", Log::DEBUG, "deleted buffer");
110
111 }
112