forked from exadel-inc/CompreFace
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request exadel-inc#416 from exadel-inc/feature/EFRS-954-vi…
…ew-images-with-extension(.tiff.tif) EFRS-954 View images with the extension.tiff.tif
- Loading branch information
Showing
7 changed files
with
177 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright (c) 2020 the original author or authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
* or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'; | ||
import { TestBed } from '@angular/core/testing'; | ||
|
||
import { LoadingPhotoService } from './photo-loader.service'; | ||
|
||
describe('LoadingPhotoService', () => { | ||
let service: LoadingPhotoService; | ||
let httpMock: HttpTestingController; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [HttpClientTestingModule], | ||
providers: [LoadingPhotoService], | ||
}); | ||
|
||
service = TestBed.inject(LoadingPhotoService); | ||
httpMock = TestBed.inject(HttpTestingController); | ||
}); | ||
|
||
it('should be created', () => { | ||
expect(service).toBeTruthy(); | ||
}); | ||
|
||
it('should be types check', () => { | ||
service.imageType.forEach((value: string) => { | ||
const file = new File([''], 'image', { type: value }); | ||
|
||
expect(service.loader(file)).toBeTruthy(); | ||
}); | ||
}); | ||
|
||
it('should be type check that is not included in list for example: image/x-jg', () => { | ||
const file = new File([''], 'image', { type: 'image/x-jg' }); | ||
|
||
expect(service.loader(file)).toBeUndefined(); | ||
}); | ||
|
||
it('should be type check that is not included in list for example: text/html', () => { | ||
const file = new File([''], 'text', { type: 'text/html' }); | ||
|
||
expect(service.loader(file)).toBeUndefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Copyright (c) 2020 the original author or authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
* or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
import { Injectable } from '@angular/core'; | ||
import { HttpClient } from '@angular/common/http'; | ||
|
||
import { map, switchMap } from 'rxjs/operators'; | ||
import { Observable } from 'rxjs'; | ||
|
||
import { decode } from 'tiff'; | ||
import TiffIfd from 'tiff/lib/tiffIfd'; | ||
|
||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class LoadingPhotoService { | ||
private type: string[] = [ | ||
'image/bmp', | ||
'image/gif', | ||
'image/jpeg', | ||
'image/png', | ||
'image/tiff', | ||
'image/vnd.wap.wbmp', | ||
'image/webp', | ||
'image/x-icon', | ||
'image/x-jng', | ||
]; | ||
|
||
get imageType(): string[] { | ||
return this.type; | ||
} | ||
|
||
constructor(private http: HttpClient) {} | ||
|
||
tiffConvertor(url: string): Observable<ImageBitmap> { | ||
return this.http.get(url, { responseType: 'arraybuffer' }).pipe( | ||
map((array: ArrayBuffer) => { | ||
const ifd: TiffIfd = decode(array)[0]; | ||
const imageData: ImageData = new ImageData(ifd.width, ifd.height); | ||
|
||
for (let i = 0; i < ifd.data.length; i++) { | ||
imageData.data[i] = ifd.data[i]; | ||
} | ||
|
||
return imageData as ImageData; | ||
}), | ||
switchMap(async (imageData: ImageData) => (await createImageBitmap(imageData)) as ImageBitmap) | ||
); | ||
} | ||
|
||
createImage(url: string): Observable<ImageBitmap> { | ||
return this.http | ||
.get(url, { responseType: 'blob' }) | ||
.pipe(switchMap(async (blob: Blob) => (await createImageBitmap(blob)) as ImageBitmap)); | ||
} | ||
|
||
loader(file: File): Observable<ImageBitmap> { | ||
const checkImageType: boolean = this.imageType.includes(file.type); | ||
|
||
if (!checkImageType) return; | ||
|
||
const url: string = URL.createObjectURL(file); | ||
const type = 'image/tiff'; | ||
|
||
return file.type === type ? this.tiffConvertor(url) : this.createImage(url); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export interface ImageSize { | ||
width: any; | ||
height: any; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters