summaryrefslogtreecommitdiff
path: root/fpcsrc/packages/hermes/src/hermes_factory.inc
blob: 0eed2005bbc69fdeb52b59e5afe5adf1919b0d27 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
{
    Free Pascal port of the Hermes C library.
    Copyright (C) 2001-2003  Nikolay Nikolov (nickysn@users.sourceforge.net)
    Original C version by Christian Nentwich (c.nentwich@cs.ucl.ac.uk)

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version
    with the following modification:

    As a special exception, the copyright holders of this library give you
    permission to link this library with independent modules to produce an
    executable, regardless of the license terms of these independent modules,and
    to copy and distribute the resulting executable under terms of your choice,
    provided that you also meet, for each linked independent module, the terms
    and conditions of the license of that module. An independent module is a
    module which is not derived from or based on this library. If you modify
    this library, you may extend this exception to your version of the library,
    but you are not obligated to do so. If you do not wish to do so, delete this
    exception statement from your version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
}

var
  Processor: Integer;

procedure Hermes_Factory_Init;
var
  res: Integer;
begin
  res := 0;
  Processor := PROC_GENERIC;

  {$IFDEF I386_ASSEMBLER}
    Processor := Processor or PROC_X86_PENTIUM;{There are no others at the moment}
    res := Hermes_X86_CPU;
    if (res and $800000) <> 0 then
    begin
//      Writeln('mmx disabled for debugging');
      Processor := Processor or PROC_MMX_PENTIUM;
{      Writeln('mmx!');}
    end;
  {$ENDIF I386_ASSEMBLER}
end;

function Hermes_Factory_getClearer(bits: Uint32): PHermesClearer;
begin
  { Try different processors in order of priority..
    Note that for this to work, an MMX processor has to have both MMX and
    X86 flags }
  New(Result);
  Result^.bits := bits;

  {$IFDEF I386_ASSEMBLER}
    if (Processor and PROC_MMX_PENTIUM) <> 0 then
      case bits of
        32: begin
          Result^.func := @ClearMMX_32;
          exit;
        end;
        24: ;
        16: begin
          Result^.func := @ClearMMX_16;
          exit;
        end;
        8: begin
          Result^.func := @ClearMMX_8;
          exit;
        end;
      end;

    if (Processor and PROC_X86_PENTIUM) <> 0 then
      case bits of
        32: begin
          Result^.func := @ClearX86_32;
          exit;
        end;
        24: ;
        16: begin
          Result^.func := @ClearX86_16;
          exit;
        end;
        8: begin
          Result^.func := @ClearX86_8;
          exit;
        end;
      end;
  {$ENDIF I386_ASSEMBLER}

  case bits of
    32: begin
      Result^.func := @ClearP_32;
      exit;
    end;
    24: begin
      Result^.func := @ClearP_24;
      exit;
    end;
    16: begin
      Result^.func := @ClearP_16;
      exit;
    end;
    8: begin
      Result^.func := @ClearP_8;
      exit;
    end;
    else
    begin
      Dispose(Result);
      Result := nil;
    end;
  end;
end;

function Hermes_Factory_getConverter(source, dest: PHermesFormat): PHermesConverter;
var
  i: Integer;
  found: Boolean;
begin
  found := False;

  New(Result);

  { Set all conversion routines to nil }
  Result^.loopnormal := nil;
  Result^.loopstretch := nil;
  Result^.normal := nil;
  Result^.stretch := nil;
  Result^.dither := nil;
  Result^.ditherstretch := nil;
  Result^.flags := 0;

  if source^.indexed then
    { for 8 bit indexed, just look at the destination bit depth and check
      if the converter's processor is a subset of our processor }
    for i := 0 to Factory_NumConverters - 1 do
      if (Factory_Converters[i].d_bits = dest^.bits) and
        (Factory_Converters[i].s_idx and
        ((processor and Factory_Converters[i].processor) <> 0)) then
      begin
        { if any routines are unassigned, assign them now }

        if Result^.loopnormal = nil then
        begin
          Result^.loopnormal := Factory_Converters[i].loopnormal;
          found := True;
        end;

        if Result^.normal = nil then
        begin
          Result^.normal := Factory_Converters[i].normal;
          found := True;
        end;

        if Result^.loopstretch = nil then
        begin
          Result^.loopstretch := Factory_Converters[i].loopstretch;
          found := True;
        end;

        if Result^.stretch = nil then
        begin
          Result^.stretch := Factory_Converters[i].stretch;
          found := True;
        end;
      end else
  else
    { Otherwise we need to compare everything, including bitmasks }
    for i := 0 to Factory_NumConverters - 1 do
      if (Factory_Converters[i].d_bits = dest^.bits) and
         (Factory_Converters[i].d_r = dest^.r) and
         (Factory_Converters[i].d_g = dest^.g) and
         (Factory_Converters[i].d_b = dest^.b) and
         (Factory_Converters[i].d_a = dest^.a) and
         (Factory_Converters[i].d_idx = dest^.indexed) and
         (Factory_Converters[i].s_bits = source^.bits) and
         (Factory_Converters[i].s_r = source^.r) and
         (Factory_Converters[i].s_g = source^.g) and
         (Factory_Converters[i].s_b = source^.b) and
         (Factory_Converters[i].s_a = source^.a) and
         (Factory_Converters[i].s_idx = source^.indexed) and
         ((processor and Factory_Converters[i].processor) <> 0) then
      begin
        { if any routines are unassigned, assign them now }

        if (Result^.loopnormal = nil) and
           (Factory_Converters[i].loopnormal <> nil) then
        begin
          Result^.loopnormal := Factory_Converters[i].loopnormal;
          found := True;
        end;

        if (Result^.normal = nil) and
           (Factory_Converters[i].normal <> nil) then
        begin
          Result^.normal := Factory_Converters[i].normal;
          found := True;
        end;

        if (Result^.loopstretch = nil) and
           (Factory_Converters[i].loopstretch <> nil) then
        begin
          Result^.loopstretch := Factory_Converters[i].loopstretch;
          found := True;
        end;

        if (Result^.stretch = nil) and
           (Factory_Converters[i].stretch <> nil) then
        begin
          Result^.stretch := Factory_Converters[i].stretch;
          found := True;
        end;

        if (Result^.dither = nil) and
           (Factory_Converters[i].dither <> nil) then
        begin
          Result^.dither := Factory_Converters[i].dither;
          found := True;
        end;

        if (Result^.ditherstretch = nil) and
           (Factory_Converters[i].ditherstretch <> nil) then
        begin
          Result^.ditherstretch := Factory_Converters[i].ditherstretch;
          found := True;
        end;

        { In the rare event of having everything assigned, pull the emergency
          break. Otherwise we need to continue looking (might be stretching
          routines somewhere :)
          do I sound like a stewardess? }
        if (Result^.loopnormal <> nil) and (Result^.normal <> nil) and
           (Result^.loopstretch <> nil) and (Result^.stretch <> nil) and
           (Result^.dither <> nil) and (Result^.ditherstretch <> nil) then
          break;
      end;

  if found then
  begin
    Hermes_FormatCopy(source, @Result^.source);
    Hermes_FormatCopy(dest, @Result^.dest);
  end
  else
  begin
    Dispose(Result);
    Result := nil;
  end;
end;

function Hermes_Factory_getEqualConverter(bits: Integer): PHermesConverter;
var
  found: Boolean;
  asm_found: Integer;
  c_found: Integer;
begin
  found := False;
  New(Result);

  { Set all conversion routines to null }
  Result^.loopnormal := nil;
  Result^.loopstretch := nil;
  Result^.normal := nil;
  Result^.stretch := nil;
  Result^.dither := nil;
  Result^.ditherstretch := nil;

{$IFDEF I386_ASSEMBLER}

  { Try MMX routines }
  if (Result^.loopnormal = nil) or (Result^.normal = nil) or
     (Result^.loopstretch = nil) or (Result^.stretch = nil) then
    if (processor and PROC_MMX_PENTIUM) <> 0 then
{      case bits of
      end};

  { Try X86 routines }
  if (Result^.loopnormal = nil) or (Result^.normal = nil) or
     (Result^.loopstretch = nil) or (Result^.stretch = nil) then
    if (processor and PROC_X86_PENTIUM) <> 0 then
    begin
      asm_found := 0;
      case bits of
        32: begin
          Result^.normal := @CopyX86p_4byte; asm_found := 1;
        end;
        24: ;
        16: begin
          Result^.normal := @CopyX86p_2byte; asm_found := 1;
        end;
         8: begin
          Result^.normal := @CopyX86p_1byte; asm_found := 1;
        end;
      end;

      if (asm_found and 1) <> 0 then
      begin
        Result^.loopnormal := @ConvertX86;
        found := True;
      end;
    end;

{$ENDIF I386_ASSEMBLER}


  if (Result^.loopnormal = nil) or (Result^.normal = nil) or
     (Result^.loopstretch = nil) or (Result^.stretch = nil) then
  begin
    c_found := 0;

    case bits of
      32: begin
        if Result^.normal = nil then
        begin
          Result^.normal := @CopyP_4byte; c_found := c_found or 1;
        end;
        if Result^.stretch = nil then
        begin
          Result^.stretch := @CopyP_4byte_S; c_found := c_found or 2;
        end;
      end;
      24: begin
        if Result^.normal = nil then
        begin
          Result^.normal := @CopyP_3byte; c_found := c_found or 1;
        end;
        if Result^.stretch = nil then
        begin
          Result^.stretch := @CopyP_3byte_S; c_found := c_found or 2;
        end;
      end;
      16: begin
        if Result^.normal = nil then
        begin
          Result^.normal := @CopyP_2byte; c_found := c_found or 1;
        end;
        if Result^.stretch = nil then
        begin
          Result^.stretch := @CopyP_2byte_S; c_found := c_found or 2;
        end;
      end;
       8: begin
        if Result^.normal = nil then
        begin
          Result^.normal := @CopyP_1byte; c_found := c_found or 1;
        end;
        if Result^.stretch = nil then
        begin
          Result^.stretch := @CopyP_1byte_S; c_found := c_found or 2;
        end;
      end;
    end;

    if (c_found and 1) <> 0 then
    begin
      Result^.loopnormal := @ConvertP; found := True;
    end;
    if (c_found and 2) <> 0 then
    begin
      Result^.loopstretch := @ConvertPStretch; found := True;
    end;
  end;

  if not found then
  begin
    Dispose(Result);
    Result := nil;
  end;
end;