]> git.vomp.tv Git - vompclient.git/blob - wjpeg.cc
Remove manual sync buttons
[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* tfileName)
24 {
25   fileName = tfileName;
26   return 1;
27 }
28
29 void WJpeg::draw()
30 {
31 #ifndef WIN32
32   Log* logger = Log::getInstance();
33
34   FILE* infile = fopen(fileName, "r");
35   if (infile == NULL)
36   {
37     logger->log("BJpeg", Log::ERR, "Can't open JPEG");
38     return;
39   }
40   logger->log("BJpeg", Log::DEBUG, "File opened");
41
42   struct jpeg_decompress_struct cinfo;
43   struct jpeg_error_mgr jerr;
44   cinfo.err = jpeg_std_error(&jerr);
45   jpeg_create_decompress(&cinfo);
46   jpeg_stdio_src(&cinfo, infile);
47   jpeg_read_header(&cinfo, TRUE);
48   jpeg_start_decompress(&cinfo);
49
50   logger->log("BJpeg", Log::DEBUG, "JPEG startup done %i %i", cinfo.output_width, cinfo.output_height);
51
52   // Init the surface
53   setDimensions(cinfo.output_width, cinfo.output_height);
54
55
56   // MAKE THE 2D ARRAY
57
58   unsigned char* buffer = (unsigned char*)malloc(area.w * area.h * 3);
59   logger->log("BJpeg", Log::DEBUG, "Buffer allocated at %p, width = %i height = %i", buffer, area.w, area.h);
60
61   unsigned char* bufferPointers[area.h];
62   for(UINT ps = 0; ps < area.h; ps++) bufferPointers[ps] = buffer + (ps * area.w * 3);
63
64   logger->log("BJpeg", Log::DEBUG, "done array check");
65
66   int rowsread = 0;
67   while (cinfo.output_scanline < cinfo.output_height)
68   {
69 //  logger->log("BJpeg", Log::DEBUG, "%i", rowsread);
70     rowsread += jpeg_read_scanlines(&cinfo, &bufferPointers[rowsread], area.h);
71   }
72
73   logger->log("BJpeg", Log::DEBUG, "Done all jpeg_read");
74
75   jpeg_finish_decompress(&cinfo);
76   jpeg_destroy_decompress(&cinfo);
77
78   fclose(infile);
79
80   logger->log("BJpeg", Log::DEBUG, "jpeg shutdown done, x, y %u %u", area.w, area.h);
81
82   Colour c;
83   UINT x, y, xoff;
84   unsigned char* p;
85
86   for (y = 0; y < area.h; y++)
87   {
88     p = bufferPointers[y];
89
90     for (x = 0; x < area.w; x++)
91     {
92       xoff = x * 3;
93
94 //      c = (  (0xFF000000        )
95 //           | (p[xoff    ]  << 16)
96 //           | (p[xoff + 1]  <<  8)
97 //           | (p[xoff + 2]       ) );
98
99       c.red = p[xoff];
100       c.green = p[xoff + 1];
101       c.blue = p[xoff + 2];
102
103       drawPixel(x, y, c);
104     }
105   }
106
107   free(buffer);
108   logger->log("BJpeg", Log::DEBUG, "deleted buffer");
109 #else
110   DWORD width,height;
111   width=height=1;
112   ((SurfaceWin*)surface)->drawJpeg(fileName+1,offsetX,offsetY,&width,&height);//This should went into the abstract base classes?
113   //Windows has a problem with the leading / fixme
114
115   setDimensions(width, height);
116 #endif
117 }
118